Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Fruits App</title>
- <link rel="stylesheet" type="text/css" href="css/main.css" />
- </head>
- <body>
- <?php
- $user = "root"; #variable to store database username
- $pass = "root"; #variable to store database password
- $dbh = new PDO('mysql:host=127.0.0.1;dbname=SSL;port=8889', $user, $pass); #connects to the datbase and logs in
- $fruitId = $_GET['id']; #stores the id value that was passed into the url from fruits.php
- $stmt = $dbh->prepare('SELECT * FROM fruits ORDER BY :fruitId ASC;'); #prepares a sql statment to select all the rows from the fruits table
- $stmt->bindParam(':fruitId', $fruitId); #binds the information from the $fruitId varaible to :fruitId to select rows from the database
- $stmt->execute(); #executes the sql statement
- $result = $stmt->fetchAll(PDO::FETCH_ASSOC); #stores the results of the sql stament in an array called $result
- if(isset($_POST['submit'])){ #conditional to run of the user has submitted the form
- $fruitId = $_GET['id']; #stores the id value that was passed into the url from fruits.php
- $fruitname = $_POST['name']; #stores the data for name from form after submission
- $fruitcolor = $_POST['color']; #stores the data for color from form after submission
- $fruitimage = $_POST['image']; #stores the data for image from the form after submission
- $stmt = $dbh->prepare('UPDATE fruits SET fruitName = :fruitname, fruitColor = :fruitcolor, fruitImage = :fruitimage WHERE fruitId = :fruitid;'); #prepares a sql statment to update a row
- $stmt->bindParam(':fruitid', $fruidId); #binds the information from the $fruitid varaible to :fruitId to update to the database
- $stmt->bindParam(':fruitname', $fruitname); #binds the information from the $fruitname varaible to :fruitname to update to the database
- $stmt->bindParam(':fruitcolor', $fruitcolor); #binds the information from the $fruitcolor varaible to :fruitcolor to update to the database
- $stmt->bindParam(':fruitimage', $fruitimage); #binds the information from the $fruitimage variable to :fruitimage to update the database
- $stmt->execute();
- header('Location: fruits.php'); #returns to the main page
- }
- ?>
- <section>
- <h2>Please update the fruit!</h2>
- <form action="" method="post">
- <label for="name">Fruit Name:</label><input type="text" name="name" id="name" value="" placeholder="Fruit Name" required/>
- <label for="color">Fruit Color:</label><input type="text" name="color" id="color" value="" placeholder="Fruit Color" required/>
- <label for="image">Fruit Image:</label><input type="text" name="image" id="image" value="" placeholder="Image url" required/>
- <input type="submit" name="submit" value="Update"/>
- </form>
- </section>
- </body>
- </hmtl>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement