简体   繁体   English

为什么我在这里得到“查询为空”?

[英]Why am I getting “Query was empty” here?

Relevant code: 相关代码:

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;
        }

For some reason, $wpdb->last_error is turning out to be Query was empty , and I can't figure out why. 由于某些原因, $wpdb->last_error变成了Query was empty is Query was empty ,我不知道为什么。 I've looked at other SO posts on this topic and they say that an undefined object is being used as the query, but here I'm using $addQuery as the query and I don't see any reason why it is not defined. 我看过有关此主题的其他SO帖子,他们说一个未定义的对象被用作查询,但是在这里,我使用$addQuery作为查询,但没有看到未定义该对象的任何原因。

Any ideas? 有任何想法吗?

Try using $wpdb->insert instead of $wpdb->query to do an insert into the database. 尝试使用$ wpdb-> insert而不是$ wpdb-> query插入数据库。

More information regarding the use of $wpdb can be found here: 有关使用$ wpdb的更多信息,可以在这里找到:

https://codex.wordpress.org/Class_Reference/wpdb https://codex.wordpress.org/Class_Reference/wpdb

The call to ->prepare is failing because you have an error on your query. 调用->prepare失败,因为查询出错。 You have got 8 columns and 8 placeholders plus $caseid that is manually added, and the array you pass to the function contains 7 elements. 您有8列和8个占位符,以及手动添加的$caseid ,传递给该函数的数组包含7个元素。

You probably have one exceeding %s . 您可能有一个超过%s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM