1

I have this code:

add_action( 'delete_post', 'my_delete_function' );
 function my_delete_function($post_id) { 
    global $wpdb;
    $achievement = get_the_category($post_id); 
    $h = $achievement[0]->cat_ID; 
    $s = ''.str_replace('"', '', $h);
    $p = var_dump(htmlentities($s));
   $wpdb->query("INSERT INTO ".$wpdb->prefix."votes (post, votes, guests, usersinks, guestsinks) VALUES('', ".$p.", '', '', '') ") or die(mysql_error());
}

mySQL keeps throwing this 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 ' '', '', '')' at line 1

Ive ran the same query in phpMyAdmin, replacing the php vars with values and it works fine

Additionally, ive made sure the the value of $s is just a number by using the echo function on a blank page.

Any help is appreciated

5
  • Add a var_dump($s); right before the last line to check what you're outputting. If it contains HTML, use var_dump(htmlentities($s)); Commented Jan 8, 2012 at 11:39
  • Can you show the full final query? Commented Jan 8, 2012 at 11:41
  • Check that your single quotes are actually single quotes and not backquotes. Also replace .$s. with .(int)$s. just to make sure. Commented Jan 8, 2012 at 11:42
  • @AramKocharyan - I tried var dump but i got this error: string(0) "" 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 ' '', '', '')' at line 1 Commented Jan 8, 2012 at 11:43
  • @DickLaurent- this is the full query. The code i have given is wrapped around a wordpress action. Ill update the above code to matxh my entire function. Commented Jan 8, 2012 at 11:45

4 Answers 4

5

If you want to get the last error and last query you can use this properties of $wpdb object:

$wpdb->last_error 

will show you the last error, if you got one.

$wpdb->last_query 

will assist you with showing the last query (where the error occurred)

I hope this will help you out.

Sign up to request clarification or add additional context in comments.

Comments

4

maybe $wpdb->query("INSERT INTO ".$wpdb->prefix."votes (post, votes, guests, usersinks, guestsinks) VALUES('', '".$s."', '', '', '') ") or die(mysql_error());

Comments

1

My guess is that the $s is empty and the SQL query ends up looking like this:

VALUES('', , '', '', '')  

Why are you doing

$s = str_replace('"', '', $h);

Is it actually possible that the int contains an "? This is not nice. But I'd eventually do this

$s = (int)str_replace('"', '', $h); 

to ensure that the variable is an int - no matter what.

Comments

0

Try this:

global $wpdb;
$achievement = get_the_category($post_id); 
$h = $achievement[0]->cat_ID; 
$s = str_replace('"', '', $h);
$wpdb->query("
    INSERT INTO ".$wpdb->prefix."votes (votes) 
    VALUES ('".$s."');") or die(mysql_error());

If that doesn't work then you are probably leaving blank fields that require a value.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.