我试图将代码从 mysqli_* 语句转换为准备好的语句以防止 SQL 注入。以下代码是我正在尝试转换的代码(目前它可以正常工作):
$details = mysqli_query($linkDB,"SELECT * FROM ".PREFIX."Issues WHERE id='".$_POST['article']."' AND disabled='0' LIMIT 1");
$detail = mysqli_fetch_assoc($details);
这是我尝试转换为准备好的语句。任何使这更简洁的方法都将不胜感激(因为我将从 2 行代码变为多行):
$SQL = "SELECT * FROM ".PREFIX."Issues WHERE id='?' AND disabled='0' LIMIT 1";
$PRE = mysqli_stmt_init($linkDB);
//if (! $PRE = mysqli_prepare($linkDB, $SQL)) { (alt attempt)
if (! mysqli_stmt_prepare($PRE, $SQL)) {
echo "<f><msg>ERROR: Could not prepare query: ".$SQL.", ".mysqli_error($linkDB)."</msg></f>";
} else {
mysqli_stmt_bind_param($PRE, "i", $test);
$test = $_POST['article'];
if (! mysqli_stmt_execute($PRE)) {
echo "<f><msg>ERROR: Could not execute query: ".$SQL.", ".mysqli_error($linkDB)."</msg></f>";
} else{
$details = mysqli_stmt_get_result($PRE);
$detail = mysqli_fetch_assoc($details);
mysqli_stmt_close($PRE);
}
}
上面的代码不会在 $detail 变量中返回/存储 db 值,以便稍后在脚本中进行处理。我试过注释掉 mysqli_stmt_close($PRE) 调用,但这没有区别。我感谢您的帮助!
宝慕林4294392
米脂