Advertisement
WaterProgger

form example - insert in db

Sep 25th, 2022 (edited)
628
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 1 0
  1. <html>
  2.     <head>
  3.         <title></title>
  4.     </head>
  5.     <body style="background-color:#999">
  6.         <h1></h1>
  7.  
  8.         <form action="index.php" method="POST">
  9.             <label for="games">Chose a game</label>
  10.  
  11.             <select name="games" id="games">
  12.                 <option value="1">EA</option>
  13.                 <option value="2">Frontier</option>
  14.                 <option value="3">Ubisoft</option>
  15.                 <option value="4">Microsoft</option>
  16.             </select>
  17.             <input type="submit" value="Search">
  18.         </form>
  19.  
  20.         <?php
  21.             // 1.) Gewähltes Game abfragen $_POST / $_GET
  22.             // check, if entry in post exists - with 'isset' or !empty
  23.             if (isset($_POST['games'])) {
  24.                 $games = $_POST['games']; // $_POST weil method=POST, 'game' weil name="game"
  25.                 echo "Game ($games)<br>";
  26.             }
  27.  
  28.             $dbserver = "localhost";
  29.             $dbuser = "root";
  30.             $dbpass = "";
  31.             $dbname = "productdb";
  32.  
  33.             if (!empty($games)) {
  34.  
  35.             // Create connection
  36.             $conn = new mysqli($dbserver, $dbuser, $dbpass, $dbname);
  37.  
  38.             // Check connection
  39.             if ($conn->connect_error) {
  40.             die("Connection failed: " . $conn->connect_error);
  41.             }
  42.             echo "Connected successfully";
  43.  
  44.             // 2.) Game aus der Datenbank holen
  45.             $sql = "SELECT idProduct, productBez, productPrice FROM product WHERE hersteller_idhersteller = $games;"; //
  46.             $result = $conn->query($sql);
  47.  
  48.             // if results are found
  49.             if ($result->num_rows > 0) {
  50.                 // if data available, then output
  51.                 echo '<table border="1" cellspacing="0" cellpadding="0">';
  52.                 echo '<tr><td>Game</td><td>Price</td><td>Properties</td></tr>';
  53.                 // 3.) game als Tabelle usgeben
  54.                 // output data of each row
  55.                 while($row = $result->fetch_assoc()) {
  56.                     #echo "ProductBez: " . $row["productBez"]. " - ProductPrice: " . $row["productPrice"]."<br>";
  57.                    // current product
  58.                     $product = $row['idProduct']; //
  59.                     // SubQuery for properties
  60.                     $sql = "SELECT propertiesBezeichnung, propertiesEinheit FROM properties
  61.                            INNER JOIN product_properties ON properties.idEigenschaften = product_properties.properties_idEigenschaften
  62.                            WHERE product_idProduct = $product;";
  63.                     // get properties
  64.                     $properties = "";
  65.                     $result2 = $conn->query($sql); // load
  66.                     if ($result2->num_rows > 0) { // if exist
  67.                         while($row2 = $result2->fetch_assoc()) {
  68.                             $properties .= $row2['propertiesBezeichnung'].":".$row2['propertiesEinheit']."<br>"; //
  69.                         } // END while result 2
  70.                     } // END if result2
  71.  
  72.                     echo "<tr><td>" . $row["productBez"]. "</td></td>" .$row["productPrice"]."</td><td>".$properties."</td></tr>";
  73.                 }
  74.                 echo "</table>";
  75.               } else { // if no results are found
  76.                 echo "No results found";
  77.               }
  78.  
  79.  
  80.             // 4.) clean up
  81.             $conn->close();
  82.         } // close if
  83.         ?>
  84.  
  85.  
  86.     </body>
  87. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement