让我开始说,我只是在过去几个月一直在编码。我知道我可能在任何地方都有很多错误或不好的做法。我喜欢建设性的批评,所以请让我知道我能做的更好,以及如何解决我目前的问题。
此代码的目的是根据先前输入的信息创建一个零件号及其相关位置列数据(存储类型、机架号、货架号)表。我把报名表准备得很好。我输入了一些我想搜索的部分,它会把这个数字贴回自己。然后向我展示了要放入零件号的文本输入字段的数量。
//this form is to submit the number of parts you're looking for,
//and posts back to itself
<form action=View2.php method="post">
<input type="text" name="num">
<button type="submit">Number of Parts</button>
</form>
//This form takes the posted number, and creates that many text fields,
//populated with the number part you are entering.
<form action="List.php" method="post">
<?php while ($i<=$num){
echo "<input type='text' name='part$i' value='part$i'><br><br>";$i++;}?><input type="hidden" name="num" value="<?php $num?>">
<button type="submit">Submit</button>
</form>
我的问题是运行mysqli_查询来填充表。我不知道从这里去哪里。我知道我需要获取发布的每个零件号,并将其用作“选择位置”搜索中的条件,因此我制作了以下While循环:
<?php
echo "<table border='1'>
<tr>
<th>Part Number</th>
<th>Location</th>
<th>Rack</th>
<th>Shelf</th>
</tr>";
while($i<=$num){
$x=$_POST["part$i"];
$result = ($con,"SELECT * FROM parts WHERE pn ='$x'");
$row = ($result);
echo "<tr>";
echo "<td>" . $x . "</td>";
echo "<td>" . $row['rcb'] . "</td>";
echo "<td>" . $row['ra'] . "</td>";
echo "<td>" . $row['sh'] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";?>
此时页面崩溃,但是如果我注释掉$result行,我将得到一个表,其中的part number字段填充了上一页的值。
有谁知道我做错了什么,或者我怎样才能做得更好?
最佳答案
这条线没什么用:
$result = ($con,"SELECT * FROM parts WHERE pn ='$x'");
您需要实际查询数据库。
$mysqli = new mysqli("localhost", "my_user", "my_password", "...");
...
$result = $mysqli->query("SELECT * FROM parts WHERE pn ='$x'");
您应该使用准备好的语句,这样您的代码就不会对sql注入打开。。。