gpt4 book ai didi

php - 如何编写更新SQL语句来更新多条记录

转载 作者:行者123 更新时间:2023-11-29 13:04:16 25 4
gpt4 key购买 nike

到目前为止,我有这段代码,它读取一个包含 3 个 varchar 字段的简单表:

<?php
//db connection code...

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM Sheet1";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body><table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);
?>

数据库有4个字段,3个varchar和1个int,当前值为0。我检查了页面源代码并确认每个复选框名称都是stickerID。现在我将把它发布到我必须创建的 editform.php 中。我想知道的是我应该如何编写更新 sql,以便它考虑用户在表单中选择的每个新值?

这是我的想法,但是如何为每个复选框做到这一点?

editform.php

<?php

//update multiple records

//UPDATE user_items SET stickerStatus = $_POST["stickerStatus"] WHERE stickerID = $_POST["stickerID"];

?>

最佳答案

第一个问题:使用mysql_fetch_assoc()而不是mysql_fetch_row()。这将返回一个关联数组而不是枚举数组。

第二个问题:阅读HTML formsform handling .

评论中问题的答案:

// The <form> tag should only be echoed once.
echo '<form name="some form" action="editform.php" method="post">';
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<input type="hidden" name="status_<?php echo $row['stickerID"; ?>" value="0">
<input type="checkbox" name="status_<?php echo $row['stickerID'] ?>" value="<?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
// You need a submit button to send the form
echo '<input type="submit">';
// Close the <form> tag
echo '</form>';

使用与复选框同名的隐藏输入可确保将给定输入名称的值发送到服务器。未选中的复选框的值将不会被发送。在这种情况下,将使用隐藏的输入。

您可以在editform.php中获取提交的值,如下所示:

<?php
foreach ($_POST as $field => $value) {
if (strpos($field, 'status_')) {
// Using (int) makes sure it's cast to an integer, preventing SQL injections
$stickerID = (int) str_replace('status_', '', $field);
// Again, preventing SQL injections. If the status could be a string, then use mysql_real_escape_string()
$stickerStatus = (int) $value;

// Do something with the results
}
}

关于php - 如何编写更新SQL语句来更新多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22949257/

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