gpt4 book ai didi

PHP MySQL 单页多个表单和多个提交

转载 作者:行者123 更新时间:2023-11-29 08:57:26 24 4
gpt4 key购买 nike

我正在尝试使用表单来过滤数据库,然后提供一种更新过滤数据的方法。我可能没有以最好的方式做到这一点,但它已经接近工作了。问题似乎是,当我按下“更新”按钮时,一些变量被清除;计数和 ID。

我通过将其设置为 session 变量来固定计数,但没有办法使用 ID 来完成此操作。我不确定为什么计数会被删除,所以如果有人能告诉我,我将不胜感激。

我的代码的更新部分在没有过滤器部分的情况下使用时效果很好,同样,问题是 id 变量为空。我已经测试了所有其他变量,并在更新语句中为 ID 添加了一个常量以进行测试。这是我的代码。

<?php
session_start();
?>

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="inventory"; // Database name
$tbl_name="computers"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$school=$_POST['school'];
$make=$_POST['make'];
$model=$_POST['model'];
$barcode=$_POST['barcode'];
$location=$_POST['location'];
?>

<?php
include 'nav-bar.php';
?>

<h2 align="center">Filter Computers</h2>
<form name="form" method="post" style="margin: 0; text-align: center;">
<p><label>School Name:</label><input type="text" name="school" size="8" id="school" tabindex="1"</p>
<p><label>Make:</label><input type="text" name="make" size="25" id="make" tabindex="1"</p>
<p><label>Model:</label><input type="text" name="model" size="25" id="model" tabindex="1"</p>
<p><label>Barcode:</label><input type="text" name="barcode" size="12" id="barcode" tabindex="1"</p>
<p><label>Location:</label><input type="text" name="location" size="25" id="location" tabindex="1"</p>
<p><label>Serial:</label><input type="text" name="serial" size="25" id="location" tabindex="1"</p>
<p><label>Date Acquired yyyy-dd-mm:</label><input type="text" name="date" size="8" id="location" tabindex="1"</p>
<p><label>Processor:</label><input type="text" name="processor" size="25" id="location" tabindex="1"</p>
<p><label>RAM:</label><input type="text" name="ram" size="25" id="location" tabindex="1"</p>
<p><input align="center" type="submit" name="Filter" value="Filter">
</form>

<?php

if($_POST['Filter']){
$sql = "SELECT * FROM $tbl_name WHERE school like '%$school' AND make like '%$make' AND model like '%$model' AND location like '%$location'";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);
$_SESSION['count']=$count;


?>

<strong>Update Multiple Computers</strong><br>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>School</strong></td>
<td align="center"><strong>Make</strong></td>
<td align="center"><strong>Model</strong></td>
<td align="center"><strong>Barcode</strong></td>
<td align="center"><strong>Location</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input name="school[]" type="text" id="school" value="<?php echo $rows['school']; ?>"></td>
<td align="center"><input name="make[]" type="text" id="make" value="<?php echo $rows['make']; ?>"></td>
<td align="center"><input name="model[]" type="text" id="model" value="<?php echo $rows['model']; ?>"></td>
<td align="center"><input name="barcode[]" type="text" id="barcode" value="<?php echo $rows['barcode']; ?>"></td>
<td align="center"><input name="location[]" type="text" id="location" value="<?php echo $rows['location']; ?>"></td>
</tr>

<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
}
// Check if button name "Update" is active, do this
if(isset($_POST['Update'])){
for($i=0;$i<$_SESSION['count'];$i++){
$sql1="UPDATE $tbl_name SET school='$school[$i]', make='$make[$i]', model='$model[$i]' , barcode='$barcode[$i]' , location='$location[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
session_destroy();
}
if(isset($result1)){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=update_multiple.php\">";
}
?>`

我们非常感谢任何帮助。

最佳答案

这就是隐藏输入的用途。为您的 ID 添加一个:

<input type="hidden" name="id[]" value="<?=$rows['id']?>" />

此外,您不需要保存计数。您可以使用 count($id) 计算数组中的项目数。

最后,清理您的输入。切勿将用户提交的数据直接放入查询中。对整数使用 intval,对字符串使用 mysql_real_escape_string,或使用准备好的语句。

关于PHP MySQL 单页多个表单和多个提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9596379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com