简体   繁体   English

使用php和复选框更新多行mysql

[英]update multiple rows mysql using php and checkboxes

I am creating a system in which a user will be able to change the "status" of an item in a MySQL table. 我正在创建一个系统,用户可以在其中更改MySQL表中项目的“状态”。

Basically my table has a unique (auto incrementing) ID field, and 18 other fields. 基本上,我的表有一个唯一的(自动递增)ID字段,以及其他18个字段。 One of those fields is a status field. 这些字段之一是状态字段。 I need to figure out a way to change the status from "active" to "hold" when a user checks off a checkbox. 我需要找出一种方法,当用户选中一个复选框时,将状态从“活动”更改为“保留”。

I am assuming that if I assign a value to the checkbox and feed it into an auto incrementing array, that it should pull through when it's checked. 我假设如果我为复选框分配一个值并将其输入到一个自动递增数组中,则它应在选中时通过。 I don't know if this is correct though. 我不知道这是否正确。

Every time I try it, the only status that gets changed is the 1st one and that's it. 每次尝试时,唯一更改的状态就是第一状态。 Here's my code that pulls out the entry's: 这是我提取该条目的代码:

while($row = mysqli_fetch_array($result))
        {
            echo "<table border='2' style='width: 100%; margin: auto; border-width: 1px'><tr><th>Date Requested</th><th>Time Called</th><th>Tenant Name</th><th>Apartment</th><th>Work Requested</th><th>Tenant need to be present?</th><th>Good Date</th><th>Time to complete</th><th>Resident Phone #</th></tr>";
            echo "<input type='hidden' name='id[$i]' value=" . $row['id'] . ">";
            echo "<tr>";
            echo "<td align='center'>" . date("m-d-Y", strtotime($row['r_date'])) . "</td>";
            echo "<td align='center'>" . date("g:i A", strtotime($row['t_call'])) . "</td>";
            echo "<td align='center'>" . $row['t_name'] . "</td>";
            echo "<td align='center'>" . $row['loc'] . "</td>";
            echo "<td align='center'>" . $row['w_req'] . "</td>";
            echo "<td align='center'>" . $row['apt_status'] . "</td>";
            if ($row['to_be'] == "1969-12-31") {
                echo "<td align='center'> </td>";
                }
            else {
                echo "<td align='center'>" . date("m-d-Y", strtotime($row['to_be'])) . "</td>";
                };
            echo "<td align='center'>" . $row['g_time'] . "</td>";
            echo "<td align='center'>" . $row['tel'] . "</td></tr>";
            echo "<tr><td align='center' colspan = '3' style='border-width:0px; background-color: #E6E6E6'><strong>Work performed/notes:</strong><br><textarea name='work[$i]'style='width:250px; height:60px'>" . htmlspecialchars($row['w_performed'], ENT_QUOTES) . "</textarea></td>";
            echo "<td colspan='2' align='center' style='border-width:0px; background-color: #E6E6E6'><strong>Check to put Work Order on hold.</strong><br><input name='holding' type='checkbox' value='" . $row['id'] . "' ></td>";
            echo "<td colspan='4' style='border-width:0px; background-color: #E6E6E6'>&nbsp;</td>";
            echo "<tr><td colspan='9' style='border-width:0px'><hr style='height:3px; color: #333; background-color: #333'></td>";
            ++$i;
        }
echo "</table>";

And the code I've been trying to use to pull through the ID using the checkbox is this: 我一直试图使用复选框来提取ID的代码是这样的:

$i = 0;

        $size = count($_POST['holding']);
        while($i < $size) 
        {

        while(isset($_POST['holding'][$i]))
        {
            $sql = "UPDATE w_o SET w_status = 'hold' WHERE id = " . $_POST['id'][$i];
            mysqli_query($mysqli, $sql) or die('Error: ' . $mysqli_error($mysqli));
            ++$i;
        }
        }

I feel like I'm WAY off and would appreciate any help. 我觉得我要离开了,很感谢您的帮助。 I've googled how to do this a gazillion times and for some reason I can't seem to find a good enough tutorial. 我已经用谷歌搜索了如何做到这一点,而且由于某种原因,我似乎找不到足够好的教程。 Thank you for your help! 谢谢您的帮助!

All of your checkboxes will have the same holding name. 您所有的复选框都将具有相同的holding名称。 Upon submission, PHP will only use ONE of the multiple values submitted. 提交后,PHP将仅使用提交的多个值之一。 If you want to submit multiple different values, you either have to give each field a unique name, or use PHP's "array" hack for the field name: holding[] . 如果要提交多个不同的值,则必须为每个字段指定一个唯一的名称,或者对字段名称使用PHP的“数组”技巧: holding[] Adding the [] tells PHP to expect multiple values, and $_POST['holding'] will be an array of the values, rather than just a single value. 添加[]告诉PHP期望多个值,而$_POST['holding']将是值的数组,而不仅仅是一个值。

I guess I figured it out, thank you for all the help! 我想我已经知道了,谢谢您的所有帮助! This is what I used: 这是我使用的:

$stmt = mysqli_prepare($mysqli, "UPDATE w_o SET w_status = 'hold' WHERE id = ?");

        mysqli_stmt_bind_param($stmt, "i", $id);
        foreach ($_POST['holding'] as $id) 
        {
            mysqli_stmt_execute($stmt);
        }

I think I tried this earlier, but didn't set holding as an array. 我想,我想这较早,但没有设置holding为阵列。 Thank you again for all the help!!! 再次感谢您的所有帮助!!!

$sql = "UPDATE w_o SET w_status = 'hold' WHERE id in (".array_map("intval",implode(',',$_POST['holding'][$i])).")";

因此您可以使用单个sql更新所有这些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM