dq_1984 2013-12-17 22:31
浏览 27
已采纳

MySQLi更新查询不起作用 - 没有错误?

I'm trying to figure out why on earth the query below isn't working. Basically it checks if a checkbox is checked, if it is the checkboxes relating variable and therefore mysql record is set to 1. If the checkbox isn't checked its set to 0. I've added multiple different troubleshooting and error reporting snippets - no errors. Yet no success? This is driving me mad. Any advice is warmly welcomed! Thank you.

if(isset($_POST['updateUser'])){

$mysqli = mysqli_connect($config['host'], 
                         $config['user'], 
                         $config['pass'], 
                         $config['db']);

foreach($_POST as $k => $v) {
    $check[$k] = isset($_POST[$k]) ? 1 : 0;
} 

$query = "UPDATE users_permissions SET editComments = ?, 
                                       editTopics = ?, 
                                       editArticles = ?, 
                                       autoApprove = ?, 
                                       allowArticleMod = ?, 
                                       courseAuthor = ?, 
                                       seeFinance = ?, 
                                       editUsers = ?, 
                                       editUsersPermissions = ?, 
                                       editSuperUsers = ?,
                                       editWL = ?
                                   WHERE 
                                       userid = ?";

$stmt = $mysqli->prepare($query);
$stmt->bind_param('iiiiiiiiiiii',                                                                                         

                                  $check['editComments'],
                                  $check['editTopics'],
                                  $check['editArticles'],
                                  $check['autoApprove'],
                                  $check['allowArticleMod'],
                                  $check['courseAuthor'],
                                  $check['seeFinance'],
                                  $check['editUsers'],
                                  $check['editUsersPermissions'],
                                  $check['editSuperUsers'],
                                  $check['editWL'],
                                  $profileData['userid']);
                                  $profileData['userid']);

$stmt->execute();

}

UPDATED: Changes made to code to reflect suggestion made by @Floris

  • 写回答

1条回答 默认 最新

  • dongpiao8821 2013-12-17 22:42
    关注

    I'm sorry but those if-blocks hurt my eyes. I know it's not really "the" answer but you should do this instead:

    $checks = [];
    foreach($_POST as $k => $v) {
        $checks[$k] = isset($_POST[$k]) ? 1 : 0;
    }
    

    Now you have an array called $checks which has all the $_POST keys as you had before with a 0/1 value depending on whether it was set, and you skip a whole bunch of ugly code! Yay! Now, just replace the bind_param vars with $check vars.

    EDIT - More code, with comments:

    //Go through each variable in the $_POST array, using "$k" to refer to the key and "$v" to the value,
    //So, $_POST["editComments"] = "foo" would have $k = "editComments" and $v = "foo"
    foreach($_POST as $k => $v) {
        //This is a shorthand if-statement which basically means: if isset($_POST[$k]) == true, $checks[$k] = 1. Else, $checks[$k] = 0.
        $checks[$k] = isset($_POST[$k]) ? 1 : 0;
    }
    
    //Nothing new here
    $query = "UPDATE users_permissions SET editComments = ?, 
                                       editTopics = ?, 
                                       editArticles = ?, 
                                       autoApprove = ?, 
                                       allowArticleMod = ?, 
                                       courseAuthor = ?, 
                                       seeFinance = ?, 
                                       editUsers = ?, 
                                       editUsersPermissions = ?, 
                                       editSuperUsers = ?,
                                       editWL = ?
                                   WHERE 
                                       userid = ?";
    
    $stmt = $mysqli->prepare($query);
    
    //So here's how to bind the $checks variables:
    $stmt->bind_param('iiiiiiiiiiii', $checks["editComments"],
                                      $checks["editTopics"], //and so on
                                      $editArticles,
                                      $autoApprove,
                                      $allowArticleMod,
                                      $courseAuthor,
                                      $seeFinance,
                                      $editUsers,
                                      $editUsersPermissions,
                                      $editSuperUsers,
                                      $editWL,
                                      $profileData['userid']);
    
    $stmt->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题