繁体   English   中英

使用PHP从SQL数据库检索特定数据

[英]Retrieve Specific Data from SQL database using PHP

因此对于一个项目,我必须将数据添加到SQL数据库中,然后根据用户的选择将显示不同的数据。

这也正是我的教授的指示 点击这里

我已经将数据添加到我的SQL数据库中,但是我目前正在努力检索它们。

这就是我设置网站的方式 点击这里

用户将在此处添加他们的输入

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <?php
      include_once 'includes/dbc.php';
     ?>
     <br>
    <meta charset="utf-8">
    <title>Final - Shoe Store</title>
    <style media="screen">
      footer{
        position: fixed;
        left: 0;
        bottom: 0;
        width: 100%;
        background-color: #4d4d4d;
        color: white;
        text-align: center;
        font-size: 50%;
      }
      .header{
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        background-color: #4d4d4d;
        color: white;
        text-align: center;
      }
      body {
        background-color: lightgrey;
        margin: 15%;
        text-align: center;
        zoom: 150%;
      }
    </style>
  </head>
  <div class="header">
    <h1> Final - Shoe Store </h1>
  </div>
  <body>
<p>Please complete this form to see the prices.</p>
<p> <span style="color:#FF0000;" >* required field</span> </p>

<form action="handler.php" method="post">
  <p>
    <label> What brand of shoes would you like?
      <select name="brand" required>
        <option value="NULL" selected disabled>---------</option>
        <option value="adidas">Adidas</option>
        <option value="converse">Converse</option>
        <option value="nike">Nike</option>
        <option value="reebok">Reebok</option>
      </select>
    </label>
      <span style="color:#FF0000">*</span>
  </p>
  <p>
    <label>What type of shoes do you want?
      <select name="type" required>
        <option value="NULL" selected disabled>-----------------</option>
        <option value="basketball">Basketball Shoes</option>
        <option value="fitness">Fitness Shoes</option>
        <option value="running">Running Shoes</option>
        <option value="walking">Walking Shoes</option>
      </select>
    </label>
      <span style="color:#FF0000">*</span>
  </p>
  <p>
    <label>How many shoes would you like?
      <input type="text" name="quantity" required>
    </label>
      <span style="color:#FF0000">*</span>
  </p>
  <p>
    <label>Choose `Adult` or `Children` to get the price for the shoes.</label>
  </p>
    <input type="submit" name="delete" value="Delete">
    <input type="submit" name="adult" value="Adult">
    <input type="submit" name="children" value="Children">
</form>
  </body>
  <footer>© 2018 Guillermo Molina Matus. All Rights Reserved.</footer>
</html>

这是handler.php,它是处理输入的文件

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>

     <br>
    <meta charset="utf-8">
    <title> Fianl - Lumber</title>
    <style media="screen">
      footer{
        position: fixed;
        left: 0;
        bottom: 0;
        width: 100%;
        background-color: #4d4d4d;
        color: white;
        text-align: center;
        font-size: 50%;
      }
      .header{
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        background-color: #4d4d4d;
        color: white;
        text-align: center;
      }
      body {
        background-color: lightgrey;
        margin: 15%;
        text-align: center;
        zoom: 150%;
      }
    </style>
  </head>
  <div class="header">
    <h1> Final - Shoe Store </h1>
  </div>
  <body style="background-color: lightgrey">
    <?php
    //Renaming the values
    $adult = isset($_POST['adult']);
    $children = isset($_POST['children']);
    $type = isset($_POST['type']);
    $quantity = isset($_POST['quantity']);
    $brand = isset($_POST['brand']);
    $delete = isset($_POST['delete']);


      //If they click on the inches button
      if ($adult)
      {
        include_once 'includes/dbc.php';
        if (($brand == "adidas")){
        echo "Adult<br>";} else {
          echo "no";
        }

    //display your result
  echo "The total price would be $";
      }
      elseif ($children) {
        include_once 'includes/dbc.php';
        echo "Children<br>";

    //display your result
  echo "The total price would be $";
      }
      elseif ($delete) {

        include_once 'includes/dbc_delete.php';
      }


     ?>
     <br>
     <br>
     <form action="input.php" method="post">
       <input type="submit" name="home" value="Go Home">
     </form>
  </body>
  <footer>
    © 2018 Guillermo Molina Matus. All Rights Reserved.
  </footer>
</html>



我遇到的主要问题是根据客户的选择向客户显示适当的价格。 我只想知道如何根据他们的选择检索正确的价格。

这是我的数据库设置方式

select sum('price') from products GROUP BY brands,kindOfShoes.

只给您洞察力。 你可以做这样的事情。 以上将为您提供各个品牌和种类的鞋的总价。 试试看,如果不加评论,这会有所帮助。

我的第一个建议是规范化数据库。

在当前方案中,您可以通过查询中的多个AND条件轻松获取数据。

例如,在原始PHP中,您可以使用$ type,$ brand等中的数据动态填充以下查询中的值。

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM YOURDBNAME where Brand='Nike' AND KindOfShoes='Running' AND Age='Adult'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Brand: " . $row["Brand"]. " - KindOfShoes: " . $row["KindOfShoes"]. " " . $row["Age"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM