简体   繁体   中英

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. 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.

Any ideas?

Try using $wpdb->insert instead of $wpdb->query to do an insert into the database.

More information regarding the use of $wpdb can be found here:

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

The call to ->prepare is failing because you have an error on your query. 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.

You probably have one exceeding %s .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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