如何编写更新SQL语句来更新多个记录

时间:2014-04-08 22:15:58

标签: php html mysql forms

到目前为止,我有这个代码,它读取一个包含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"];

?>

2 个答案:

答案 0 :(得分:1)

第一个问题:使用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
    }
}

答案 1 :(得分:0)

待办事项

print_r($row)

确切了解行阵列的构造方式并从那里开始工作。

对于比较运算符,请使用

$row[3] === 0

而不是

$row[3] == 0

如果值和数据类型匹配而不仅仅是值,则返回true。

0可以表示布尔值false以及数值0