Advertisement
Guest User

Untitled

a guest
Feb 19th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Fruits App</title>
  5.         <link rel="stylesheet" type="text/css" href="css/main.css" />
  6.     </head>
  7.     <body>
  8.         <?php
  9.             $user = "root"; #variable to store database username
  10.             $pass = "root"; #variable to store database password
  11.  
  12.             $dbh = new PDO('mysql:host=127.0.0.1;dbname=SSL;port=8889', $user, $pass); #connects to the datbase and logs in
  13.  
  14.             $fruitId = $_GET['id']; #stores the id value that was passed into the url from fruits.php
  15.             $stmt = $dbh->prepare('SELECT * FROM fruits ORDER BY :fruitId ASC;'); #prepares a sql statment to select all the rows from the fruits table
  16.             $stmt->bindParam(':fruitId', $fruitId); #binds the information from the $fruitId varaible to :fruitId to select rows from the database
  17.             $stmt->execute(); #executes the sql statement
  18.             $result = $stmt->fetchAll(PDO::FETCH_ASSOC); #stores the results of the sql stament in an array called $result
  19.  
  20.             if(isset($_POST['submit'])){ #conditional to run of the user has submitted the form
  21.  
  22.                 $fruitId = $_GET['id']; #stores the id value that was passed into the url from fruits.php
  23.                 $fruitname = $_POST['name']; #stores the data for name from form after submission
  24.                 $fruitcolor = $_POST['color']; #stores the data for name from form after submission
  25.                 $stmt = $dbh->prepare('UPDATE fruits SET fruitName = :fruitname, fruitColor = :fruitcolor WHERE fruitId = :fruitid;'); #prepares a sql statment to update a row
  26.                 $stmt->bindParam(':fruitid', $fruidId); #binds the information from the $fruitid varaible to :fruitId to update to the database
  27.                 $stmt->bindParam(':fruitname', $fruitname); #binds the information from the $fruitname varaible to :fruitname to update to the database
  28.                 $stmt->bindParam(':fruitcolor', $fruitcolor); #binds the information from the $fruitcolor varaible to :fruitcolor to update to the database
  29.                 $stmt->execute();
  30.  
  31.                 header('Location: fruits.php'); #returns to the main page
  32.         }
  33.         ?>
  34.     <section>
  35.         <h2>Please update the fruit!</h2>
  36.  
  37.         <form action="" method="post">
  38.             <label for="name">Fruit Name:</label><input type="text" name="name" id="name" value=""/>
  39.             <label for="color">Fruit Color:</label><input type="text" name="color" id="color"  value=""/>
  40.             <input type="submit" name="submit" value="Update"/>
  41.         </form>
  42.     </section>
  43.     </body>
  44. </hmtl>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement