简体   繁体   English

mysql从$ _POST数组更新多行

[英]mysql update multiple rows from $_POST array

Essentially Im trying to update multiple MySQL rows using an array I got from the $_POST variable called "units". 本质上,我试图使用从$_POST变量“ units”获得的数组更新多个MySQL行。 Been stuck trying to figure out this. 被困试图找出这一点。 This is what I have so far: 这是我到目前为止的内容:

Current Code that doesnt add to the 2nd person: 当前未添加到第二个人的代码:

if(isset($_POST['unit'])){ $units .= implode(', ', $_POST['unit']); } else { redirect('addcall.php?err=7'); } 

$my_s = $my->prepare('INSERT INTO `calls`(`call_taker`, `call_time`, `call_type`, `call_priority`, `call_location`, `call_city`, `reporter_name`, `reporter_num`, `call_info`, `units`) VALUES (?,?,?,?,?,?,?,?,?,?)') or die($my->error);
$my_s->bind_param('sssisssiss', $name, $date, $c_type, $c_prio, $c_loc, $c_city, $r_name, $r_num, $c_info, $units) or die($my->error);
$my_s->execute() or die($my->error);
$my_s->close();
$my->close();
$my = new mysqli(HOST, USER, PASS, DB);
$my_s2 = $my->prepare('UPDATE `users` SET `busy` = 1 WHERE `badge` = ? AND `enabled` = 1 AND `confirmed` = 1 AND `duty` = 1') or die($my->error);
$my_s2->bind_param('s', $units) or die($my-error);
$my_s2->execute() or die($my-error);
$my_s2->close();
$my->close();
redirect('dashboard.php');

        <table> // table for checkboxes thats in main HTML area
        <tr> <td>Units Available:&nbsp&nbsp&nbsp</td>
        <?php
        $my = new mysqli(HOST, USER, PASS, DB);
        $my_s = $my->prepare('SELECT `name`, `badge` FROM `users` WHERE `duty` = 1 AND `busy` = 0');
        $my_s->execute();
        $my_s->store_result();
        if($my_s->num_rows == 0){
            echo "<td>None</td>";
        }
        $my_s->bind_result($u_name, $u_badge);
        while($row = $my_s->fetch()){
            echo '<td><input type="checkbox" name="unit[]" value="'.$u_badge.'">'.$u_name.' , #'.$u_badge.'&nbsp</td>'; 
        }
        $my_s->close(); $my->close();?>
        </tr>
        </table>

I think your html code is missing a "form" tag. 我认为您的html代码缺少“表单”标签。 Every type of "input" in html needs to be inside a "form" and "/form" tags that define the method which the form is sent (POST or GET) and what is the destination (what php file), defined by "action" parameter. html中的每种“输入”类型都必须位于“表单”和“ / form”标记内,这些标记定义了发送表单的方法(POST或GET)以及目的地(什么是php文件),由“动作”参数。

You could use a foreach loop to update each badge number in $_POST['unit'] . 您可以使用foreach循环更新$_POST['unit']每个徽章编号。 Or you can use it to construct a list of values to use in an IN test. 或者,您可以使用它来构造要在IN测试中使用的值的列表。 Unfortunately, you can't bind a parameter to this, so it's necessary to escape them explicitly. 不幸的是,您不能将参数绑定到此,因此有必要显式转义它们。

if (isset($_POST['unit'])) {
    $units = implode(', ', $_POST['unit']);
    $my_s = $my->prepare('INSERT INTO `calls`(`call_taker`, `call_time`, `call_type`, `call_priority`, `call_location`, `call_city`, `reporter_name`, `reporter_num`, `call_info`, `units`) VALUES (?,?,?,?,?,?,?,?,?,?)') or die($my->error);
    $my_s->bind_param('sssisssiss', $name, $date, $c_type, $c_prio, $c_loc, $c_city, $r_name, $r_num, $c_info, $units) or die($my->error);
    $my_s->execute() or die($my->error);

    $in_units = implode(', ', array_map(function($u) use ($my) { return "'" . $my->real_escape_string($u) . "'"; }, $_POST['unit']));
    $my_s2 = $my->prepare("UPDATE `users` SET `busy` = 1 WHERE `badge` IN ($in_units) AND `enabled` = 1 AND `confirmed` = 1 AND `duty` = 1") or die($my->error);
    $my_s2->execute() or die($my-error);
}

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

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