相关代码:
add_action( 'wp_ajax_proj_update', 'proj_update' );
function proj_update ( )
{
$id = $_POST['id'];
$compname = $_POST['compname'];
$projname = $_POST['projname'];
$imageurl = $_POST['imageurl'];
$sumsmall = $_POST['sumsmall'];
$sumfull = $_POST['sumfull'];
$results = $_POST['results'];
$caseid = (!isset($_POST['caseid']) || strcmp($_POST['caseid'],'none')) ? $_POST['caseid'] : "NULL"; // weirdness required to get the value of our <select name="caseid"> element converted to something we can insert to database
$hide = array_key_exists('hide',$_POST) !== false ? 1 : 0; // weirdness required to get the value of <input type="checkbox" name="hide"> converted to something we can insert to database
$thisAction = $_POST['thisAction'];
global $wpdb;
$message = "";
switch ($thisAction)
{
case 'add':
{
/* Note: Have to break up prepare statement because https://core.trac.wordpress.org/ticket/12819 */
$addQuery = $wpdb->prepare("INSERT INTO projs (compname,projname,imageurl,sumsmall,sumfull,results,caseid,hide) VALUES (%s,%s,%s,%s,%s,%s,%d," . $caseid . ",%d)",
array($compname, $projname,$imageurl,$sumsmall,$sumfull,$results,$hide));
$message .= $wpdb->query($addQuery)
? 'Successfully added project to the database.'
: 'Error occurred when trying to add project to database: ' . $wpdb->last_error;
break;
}
出于某种原因,$wpdb->last_error
结果为Query was empty
,我无法弄清楚原因。我看过其他的S.O.关于这个主题的帖子,他们说未使用的对象被用作查询,但我在这里使用$addQuery
作为查询,我没有看到任何没有定义的原因。
有什么想法吗?
答案 0 :(得分:0)
尝试使用$ wpdb-&gt; insert而不是$ wpdb-&gt;查询来插入数据库。
有关使用$ wpdb的更多信息,请访问:
答案 1 :(得分:0)
对->prepare
的调用失败,因为您的查询出错。你有8列和8个占位符加上$caseid
手动添加,你传递给函数的数组包含7个元素。
你可能有一个超过%s
。