从$ _POST数组MySQL的更新多行 [英] mysql update multiple rows from $_POST array

查看:158
本文介绍了从$ _POST数组MySQL的更新多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我试着用数组,我从被称为单位的 $ _ POST 变量得到更新多个MySQL行。一直停留试图找出这一点。这是我到目前为止有:

当前code,这并不添加到第二人:

 如果(使用isset($ _ POST ['单元'])){$ =单位破灭('',$ _ POST ['单元']); }其他{重定向('addcall.php ERR = 7?'); }$ my_s = $我的 - > prepare(INSERT INTO`calls`(`call_taker`,`call_time`,`call_type`,`call_priority`,`call_location`,`call_city`,`reporter_name`,`reporter_num ??????????`,`call_info`,`units`)VALUES(,,,,,,,,,)')或死亡($我的 - >错误);
$ my_s-> bind_param('sssisssiss',$名称,$日期,$ c_type,$ c_prio,$ C_LOC,$ c_city,$ r_name,$ r_num,$ c_info,$单位)或死亡($我的 - >错误);
$ my_s->的execute()或死亡($我的 - >错误);
$ my_s->关闭();
$我的 - >关闭();
$ =我新的mysqli(HOST,USER,PASS,DB);
$ my_s2 = $我的 - > prepare('?`UPDATE SET users``busy` = 1 WHERE`badge` =和'enabled` = 1和'confirmed` = 1和'duty` = 1')或死亡($我的 - >错误);
$ my_s2-> bind_param('S',$单位)或死亡($我的错误);
$ my_s2->的execute()或死亡($我的错误);
$ my_s2->关闭();
$我的 - >关闭();
重定向('dashboard.php');        <表> //表中主HTML区域复选框,多数民众赞成
        &所述; TR> < TD>单位可用:放大器;&NBSP放大器;&NBSP放大器;&NBSP LT; / TD>
        < PHP
        $ =我新的mysqli(HOST,USER,PASS,DB);
        $ my_s = $我的 - > prepare(SELECT`name`,`badge` FROM`users`其中`duty` = 1和'busy` = 0');
        $ my_s->执行();
        $ my_s-> store_result();
        如果($ my_s-> NUM_ROWS == 0){
            回声< TD>无< / TD>中;
        }
        $ my_s-> bind_result($ u_name,$ u_badge);
        而($行= $ my_s->取()){
            呼应'< TD><输入类型=复选框NAME =单位[]值='$ u_badge。'>'。$ u_name。 ,#'。$ u_badge。'和;&NBSP LT; / TD>';
        }
        $ my_s->关闭(); $我的 - >关闭();?>
        < / TR>
        < /表>


解决方案

您可以使用的foreach 循环更新 $每个证件号码_ POST ['单元'] 。或者你可以用它来构建值列表在测试使用。不幸的是,你不能绑定的参数,这一点,所以它明确地逃离他们是必要的。

 如果(使用isset($ _ POST ['单元'])){
    $单位=破灭('',$ _ POST ['单元']);
    $ my_s = $我的 - > prepare(INSERT INTO`calls`(`call_taker`,`call_time`,`call_type`,`call_priority`,`call_location`,`call_city`,`reporter_name`,`reporter_num ??????????`,`call_info`,`units`)VALUES(,,,,,,,,,)')或死亡($我的 - >错误);
    $ my_s-> bind_param('sssisssiss',$名称,$日期,$ c_type,$ c_prio,$ C_LOC,$ c_city,$ r_name,$ r_num,$ c_info,$单位)或死亡($我的 - >错误);
    $ my_s->的execute()或死亡($我的 - >错误);    $ in_units =破灭('',array_map(函数($ U)使用($我){返回'$我的 - 方式> real_escape_string($ U)。';},$ _ POST ['单元' ]));
    $ my_s2 = $我的 - > prepare(更新`users` SET`busy` = 1 WHERE`badge` IN($ in_units)和`enabled` = 1和'confirmed` = 1和'duty` = 1)或死亡($我的 - >错误);
    $ my_s2->的execute()或死亡($我的错误);
}

Essentially Im trying to update multiple MySQL rows using an array I got from the $_POST variable called "units". 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>

解决方案

You could use a foreach loop to update each badge number in $_POST['unit']. Or you can use it to construct a list of values to use in an IN test. 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);
}

这篇关于从$ _POST数组MySQL的更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆