当信息提交时,我有一个问题,它根本没有运行,我相信我的语法是错误的。
if ($_POST['note'] != $player->note_text) {
$message = 'Admin '.$user.' has added the note ('.$_POST['note'].') to '.$player->name.'('.$pid.')';
logIt($user, $message, $dbcon);
$note = $_POST['note'];
$note = '"'.$note.'"';
$UpdateN = "INSERT INTO notes (uid, staff_name, name, alias, note_text, warning) VALUES ('$_POST[hidden]', '$user', '$player->name', '$player->aliases', '$note','$_POST[warn]')";
$UpdateN2 = "INSERT INTO players WHERE `playerid` = $_POST[hidden] (warning) VALUES ('$_POST[warn]')";
mysqli_query($dbcon, $UpdateN, $UpdateN2);
我添加的新行似乎已经打破了它,它是'$UpdateN2‘。我不确定新的一行是否破坏了声明,因为我是PHP和mysqli的新手,我希望能以更详细的方式学习和理解论坛或网站的任何指针。
编辑:我已经从使用mysqli转移到PDO了,我强烈建议任何愿意在PHP中使用MYSQL命令的人来看看这个:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
我开始阅读这篇文章并对其进行测试,我发现这要容易得多,但在几天后重新阅读时,它看起来也清晰得多,也可以理解。
发布于 2017-02-21 07:40:19
为了与原始版本中的mysqli过程风格保持一致,使用带有绑定占位符的预准备语句,如下所示:
$UpdateN = 'INSERT INTO notes (uid, staff_name, name, alias, note_text, warning)'
. ' VALUES ( ?, ? , ? , ? , ? , ? )';
$UpdateN2 = 'UPDATE players SET warning = ? WHERE playerid = ? ';
if( $sth = mysqli_prepare($dbcon,$UpdateN) ) {
mysqli_stmt_bind_param($sth,'ssssss'
,$_POST['hidden']
,$user
,$player->name
,$player->aliases
,$_POST['note']
,$_POST['warn']
);
if( mysqli_stmt_execute($sth) ) {
// statement execution successful
} else {
printf("Error: %s\n",mysqli_stmt_error($sth));
}
} else {
printf("Error: %s\n",mysqli_error($dbcon));
}
if( $sth2 = mysqli_prepare($dbcon,$UpdateN2) ) {
mysqli_stmt_bind_param($sth2,'ss'
,$_POST['warn']
,$_POST['hidden']
);
if( mysqli_stmt_execute($sth2) ) {
// statement execution successful
} else {
printf("Error: %s\n",mysqli_stmt_error($sth2));
}
} else {
printf("Error: %s\n",mysqli_error($dbcon));
}
发布于 2017-02-21 07:21:59
如果是我,我只会向mysqli_query发出两个单独的调用,每个单独的查询一个。由于可能存在sql注入漏洞,PHP历来非常谨慎,不允许在单个函数调用中进行多个查询。
需要注意的是,你需要清理你的输入。用户永远不应该直接出现在mysql查询字符串中,因为您的post数据可能类似于‘)’;-actually TABLE $_POST;‘。
最后,在insert上使用WHERE子句。这可能会破坏您的查询。您应该花几天时间研究一下这是如何和为什么会破坏您的查询,以及如何和为什么要编写正确的sql查询。这样做将为您的公司增加价值,因为您的公司不太可能遭受灾难性的数据泄露,它将为您增加价值,因为您将成为一名更好的SQL程序员。
编辑:在我写这篇文章的时间里,有三个不同的人提出了每个观点。LOL。
https://stackoverflow.com/questions/42355871
复制相似问题