将SQL结果从循环添加到列表

时间:2018-08-13 12:02:07

标签: php mysql sql list

我正在尝试建立库存系统,我需要将所选结果添加到带有用户输入数量的列表中。我使用下面的代码来获取和显示结果,但是我对如何使用数量和“添加到列表”按钮将项目添加到列表上感到困惑。谁能给我一些指导?原谅我,我是PHP和SQL的新手。我要查找的最终结果只是用户输入的所选项目的列表,因此我可以将此列表粘贴到电子邮件正文中。谢谢!

编辑:我不是在问为什么我不应该在PHP中使用mysql_ *函数。很高兴知道,我将对其进行更改,但这与我最初的问题无关。

<?php
@mysql_connect("localhost", "root", "") or 
die("Error connecting to database: 
".mysql_error());

mysql_select_db("inv") or 
die(mysql_error());

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1- 
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Inventory Search Results</title>
<meta http-equiv="Content-Type" 
content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" 
href="style.css"/>
</head>
<body>
<img src="images.jpg" alt="logo" height="98" 
width="300"> <br><br>
<h3> Inventory Results - Warehouse</h3><hr>
<?php
$query = $_GET['query']; 
// gets value sent over search form

$min_length = 3;
// set minimum length of the query

if(strlen($query) >= $min_length){ // if 
query length is more or equal minimum length 
then

    $query = htmlspecialchars($query); 
    // changes characters used in html to 
 their equivalents

    $query = 
 mysql_real_escape_string($query);
    // makes sure nobody uses SQL injection

    $raw_results = mysql_query("SELECT * 
 FROM products
        WHERE (`PID` LIKE '%".$query."%') OR 
 (`Description` LIKE '%".$query."%') ORDER 
 BY PID") or die(mysql_error());
    /*$assoc = 
 mysql_fetch_assoc($raw_results);


    var_dump($assoc);*/

    if(mysql_num_rows($raw_results) > 0){ // 
if one or more rows are returned do following


        $i = "1";
        while($results = 
mysql_fetch_array($raw_results)){

          echo "<h3><div id='$i'>Product ID: ".$results['PID']."</div></h3>";
          echo "<p>Description: ".$results['Description']."</p>";
          echo "<p>Quantity: ".$results['Quantity']."</p>";
          echo "<p>Location: ".$results['Location']."</p>";
         // echo "<p>Link: ".$results['DataSheetLink']."</p>";
          if(!empty($results['DataSheetLink']))
          echo '<p>Data Sheet Link: <a href="'.$results['DataSheetLink'].'">Click Here</a></p>';
          echo "<a href='mailto:marc?subject=Part Request: ".$results['PID']."&body=I would like to request (enter quantity of this item) of PID ".$results['PID']." - ".$results['Description']." '>Request Item</a><br><br>";

          echo "<form>";
          echo "<input type='text' id='qty$i' value='' placeholder='Quantity' width='50px' />";
          echo '<button name="add" value="'.$results['PID'].'" type="submit">Add to List</button><br>'; 
          echo "</form>";
          echo "<hr>"; 


          $i++;



            // posts results gotten from database
        }


    }
    else{ // if there is no matching rows do following
        echo "Sorry, No results were found";
    }

}
else{ // if query length is less than minimum
    echo "Minimum length is ".$min_length;
}
?>

</body>
</html>

0 个答案:

没有答案