Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start(); #starts session
- $jsoncontent = file_get_contents("http://localhost:8888/ads.php");
- $jsoncontents = json_decode($jsoncontent);
- $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
- if($_SERVER['REQUEST_METHOD']=='POST') { #conditional to run when user submits form
- $fName = $_POST['fName']; #stores fruit name from form in variable
- $fColor = $_POST['fColor']; #stores fruit color from form in variable
- $fImage = $_POST['fImage']; #stores fruit image from form in variable
- $stmt = $dbh->prepare('INSERT INTO fruits (fruitName, fruitColor, fruitImage) VALUES (:fruitname, :fruitcolor, :fruitimage'); #prepares sql statement that will be executed
- $stmt->bindParam(':fruitname', $fName); # binds the information from the $fName varaible to :fruitname to be added to the database
- $stmt->bindParam(':fruitcolor', $fColor); # binds the information from the $fColor varaible to :fruitcolor to be added to the database
- $stmt->bindParam(':fruitimage', $fImage); # binds the information from the $fImage varaible to :fruitimage to be added to the database
- $stmt->execute(); #executes the SQL statement
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Fruits App</title>
- <link rel="stylesheet" type="text/css" href="css/main.css" />
- </head>
- <body>
- <section>
- <h2>Fruit Database App</h2>
- <?php
- foreach ($jsoncontents->fruits as $fruits) {
- echo "<h3>Our fruit recommendation: " . $fruits->fruitColor . " " . $fruits->fruitName . "</h3>";
- echo "<img src='".$fruits->fruitImage."'/>";
- }
- ?>
- <p>Please add your favorite fruit to our database using the app below!</p>
- <form action="fruits.php" method="POST">
- <label for="fName">Fruit Name:</label><input type="text" name="fName" id="fName" value="" placeholder="Fruit Name" required/>
- <label for="fColor">Fruit Color:</label><input type="text" name="fColor" id="fColor" value="" placeholder="Fruit Color" required/>
- <label for="fImage">Fruit Image:</label><input type="text" name="fImage" id="fImage" value="" placeholder="Fruit Image url" required/>
- <input type="submit" name="submit" value="Submit"/>
- </form>
- <table>
- <tr>
- <th>Fruit Id</th>
- <th>Fruit Name</th>
- <th>Fruit Color</th>
- <th>Fruit Image</th>
- <th>Action</th>
- <?php
- $stmt = $dbh->prepare('SELECT * FROM fruits ORDER BY fruitId ASC;'); #prepares an sql statement that selects the information to be return to user
- $stmt->execute(); #executes the sql statement
- $result = $stmt->fetchAll(PDO::FETCH_ASSOC); #stores the results of the sql stament in an array called result
- foreach ($result as $row) { #foreach loop to loop through the results from the database and echos them to the user
- 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>';
- }
- ?>
- </tr>
- </table>
- </section>
- </body>
- </hmtl>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement