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

时间:2013-12-17 22:31:05

标签: php

我试图弄清楚为什么地球上的查询不起作用。基本上它检查是否选中了复选框,如果它是复选框相关变量,因此mysql记录设置为1.如果未选中该复选框,则将其设置为0.我添加了多个不同的故障排除和错误报告片段 - 否错误。但没有成功?这让我很生气。热烈欢迎任何建议!谢谢。

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();

}

更新:对代码进行的更改以反映@Floris提出的建议

1 个答案:

答案 0 :(得分:1)

我很抱歉,但那些if-blocks伤害了我的眼睛。我知道这不是真正的“答案”,但你应该这样做:

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

现在你有一个名为$checks的数组,其中包含所有$_POST键,具有0/1值,具体取决于它是否已设置,并且您跳过一大堆丑陋的代码!好极了!现在,只需用$ check vars替换bind_param变量。

编辑 - 更多代码,注释:

//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();