gpt4 book ai didi

php - Wpdb prepare 调用不正确

转载 作者:行者123 更新时间:2023-12-05 06:44:09 27 4
gpt4 key购买 nike

我想准备我的数据以避免 SQL 注入(inject)。所以我当前的工作代码显示表列中的数据列表:

global $wpdb;
$sliders = $wpdb->get_results('SELECT alias, title FROM wp_revslider_sliders', ARRAY_A);

echo '<select name="revslider">';
if ($sliders) {
foreach($sliders as $slide){
echo '<option value="'.$slide['alias'].'" '.($select_revslider_shortcode == $slide['alias'] ? 'selected=""' : '').'>'.$slide['title'].'</option>';
}
}
echo '</select>';

我需要使用 $wpdb->prepare 来确保我的数据正确地从数据库中获取。我目前的进度:

$sliders = $wpdb->query($wpdb->prepare("SELECT id, alias, title FROM wp_revslider_sliders", ARRAY_A));

这是行不通的。我收到通知:

Notice: wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder.

谁能告诉我我的代码哪里有问题?

问候

最佳答案

prepare 方法用于防止SQL 注入(inject)。当您想要将变量插入到查询中时,您会使用它。它接受占位符,例如 %s 代表字符串,%d 代表整数,%f 代表 float 。

您的查询没有任何变量,因此您不需要 prepare 方法。您看到错误是因为您没有使用任何占位符。

https://developer.wordpress.org/reference/classes/wpdb/#protect-queries-against-sql-injection-attacks

关于php - Wpdb prepare 调用不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30510225/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com