0

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

0

2 Answers 2

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.