admin管理员组

文章数量:1302407

I am trying to insert data into a table, getting errors, I have tried messaging the data, but cannot seem to get it to work, can you please help me figure out what I did wrong, please...

Here is my code I execute after validation is done:

                   $sql = 'INSERT INTO `tablename` (`file`,`afile`,`type`,`keyword`,`location`,`numbl`,`userul`,`uldate`) values (%s,%s,%s,%s,%s,%s,%s,%d) "'. $_REQUEST['filename'] . '", "' . $url . '", "' . $_REQUEST['filetype'] . '", "' . $_REQUEST['filekeyword'] . '", "' . $_REQUEST['filelocation'] . '", "' . $_REQUEST['filenumbl'] . '", "' . $user_login . '", "' . time() . '"';
                    $wpdb->query($sql);
                    $_tbInsertId = $wpdb->insert_id;

It is not working. I added the show errors code and it says this:

  WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s,%s,%s,%s,%s,%s,%s,%d) "LK-Contractor-NOGglRev-16Ld-082214.csv"' at line 1]

I even tried putting a comma AFTER the closing ) in values... but same error. I added "quote" marks around all of the fields, that did not fix it either.

Do you see what is causing it? Thanks, Richard

I am trying to insert data into a table, getting errors, I have tried messaging the data, but cannot seem to get it to work, can you please help me figure out what I did wrong, please...

Here is my code I execute after validation is done:

                   $sql = 'INSERT INTO `tablename` (`file`,`afile`,`type`,`keyword`,`location`,`numbl`,`userul`,`uldate`) values (%s,%s,%s,%s,%s,%s,%s,%d) "'. $_REQUEST['filename'] . '", "' . $url . '", "' . $_REQUEST['filetype'] . '", "' . $_REQUEST['filekeyword'] . '", "' . $_REQUEST['filelocation'] . '", "' . $_REQUEST['filenumbl'] . '", "' . $user_login . '", "' . time() . '"';
                    $wpdb->query($sql);
                    $_tbInsertId = $wpdb->insert_id;

It is not working. I added the show errors code and it says this:

  WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s,%s,%s,%s,%s,%s,%s,%d) "LK-Contractor-NOGglRev-16Ld-082214.csv"' at line 1]

I even tried putting a comma AFTER the closing ) in values... but same error. I added "quote" marks around all of the fields, that did not fix it either.

Do you see what is causing it? Thanks, Richard

Share Improve this question asked Sep 13, 2014 at 17:44 Richard JonesRichard Jones 112 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 1

I found the answer, in the codex. I just used it's code and adapted it, this code:

  $wpdb->query( $wpdb->prepare( 
"
    INSERT INTO $wpdb->postmeta
    ( post_id, meta_key, meta_value )
    VALUES ( %d, %s, %s )
", 
    10, 
$metakey, 
$metavalue 
) );

So it works now.

If you are still facing the issue try printing the query

echo $sql = 'INSERT INTO `tablename` (`file`,`afile`,`type`,`keyword`,`location`,`numbl`,`userul`,`uldate`) values ("'. $_REQUEST['filename'] . '", "' . $url . '", "' . $_REQUEST['filetype'] . '", "' . $_REQUEST['filekeyword'] . '", "' . $_REQUEST['filelocation'] . '", "' . $_REQUEST['filenumbl'] . '", "' . $user_login . '", "' . time() . '"';


// $wpdb->query($sql);
// $_tbInsertId = $wpdb->insert_id;

Now run the printed query direct in phpmyadmin to check the issue if any of value is missing

Hope it will help

本文标签: termsINSERTING Data into table with placeholders