Advertisement
Guest User

Untitled

a guest
Feb 19th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. <?php
  2.  
  3. session_start(); #starts session
  4.  
  5. $jsoncontent = file_get_contents("http://localhost:8888/ads.php");
  6. $jsoncontents = json_decode($jsoncontent);
  7.  
  8. $user = "root"; #variable to store database username
  9. $pass = "root"; #variable to store database password
  10.  
  11. $dbh = new PDO('mysql:host=127.0.0.1;dbname=SSL;port=8889', $user, $pass); #connects to the datbase and logs in
  12.  
  13. if($_SERVER['REQUEST_METHOD']=='POST') { #conditional to run when user submits form
  14.     $fName = $_POST['fName']; #stores fruit name from form in variable
  15.     $fColor = $_POST['fColor']; #stores fruit color from form in variable
  16.     $fImage = $_POST['fImage']; #stores fruit image from form in variable
  17.     $stmt = $dbh->prepare('INSERT INTO fruits (fruitName, fruitColor, fruitImage) VALUES (:fruitname, :fruitcolor, :fruitimage'); #prepares sql statement that will be executed
  18.     $stmt->bindParam(':fruitname', $fName); # binds the information from the $fName varaible to :fruitname to be added to the database
  19.     $stmt->bindParam(':fruitcolor', $fColor); # binds the information from the $fColor varaible to :fruitcolor to be added to the database
  20.     $stmt->bindParam(':fruitimage', $fImage); # binds the information from the $fImage varaible to :fruitimage to be added to the database
  21.     $stmt->execute(); #executes the SQL statement
  22. }
  23. ?>
  24. <!DOCTYPE html>
  25. <html>
  26.     <head>
  27.         <title>Fruits App</title>
  28.         <link rel="stylesheet" type="text/css" href="css/main.css" />
  29.     </head>
  30.     <body>
  31.     <section>
  32.         <h2>Fruit Database App</h2>
  33.         <?php
  34.             foreach ($jsoncontents->fruits as $fruits) {
  35.                 echo "<h3>Our fruit recommendation: " . $fruits->fruitColor . " " . $fruits->fruitName . "</h3>";
  36.                 echo "<img src='".$fruits->fruitImage."'/>";
  37.             }
  38.         ?>
  39.         <p>Please add your favorite fruit to our database using the app below!</p>
  40.         <form action="fruits.php" method="POST">
  41.             <label for="fName">Fruit Name:</label><input type="text" name="fName" id="fName" value="" placeholder="Fruit Name" required/>
  42.             <label for="fColor">Fruit Color:</label><input type="text" name="fColor" id="fColor"  value="" placeholder="Fruit Color"  required/>
  43.             <label for="fImage">Fruit Image:</label><input type="text" name="fImage" id="fImage"  value="" placeholder="Fruit Image url" required/>
  44.             <input type="submit" name="submit" value="Submit"/>
  45.         </form>
  46.             <table>
  47.                 <tr>
  48.                     <th>Fruit Id</th>
  49.                     <th>Fruit Name</th>
  50.                     <th>Fruit Color</th>
  51.                     <th>Fruit Image</th>
  52.                     <th>Action</th>
  53.  
  54.                     <?php
  55.                         $stmt = $dbh->prepare('SELECT * FROM fruits ORDER BY fruitId ASC;'); #prepares an sql statement that selects the information to be return to user
  56.                         $stmt->execute(); #executes the sql statement
  57.                         $result = $stmt->fetchAll(PDO::FETCH_ASSOC); #stores the results of the sql stament in an array called result
  58.  
  59.                         foreach ($result as $row) { #foreach loop to loop through the results from the database and echos them to the user
  60.                             echo '<tr><td>' . $row['fruitId'] . '</td><td>' . $row['fruitName'] . '</td><td>' . $row['fruitColor'] . '</td><td>' . $row['fruitImage'] . '</td><td><a href="deletefruit.php?id=' . $row['fruitId'] . '">Delete</a></td><td><a href="updatefruit.php?id=' . $row['fruitId'] . '">Update</a></td>';
  61.                         }
  62.                     ?>
  63.                 </tr>
  64.             </table>
  65.     </section>
  66.     </body>
  67. </hmtl>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement