Nurdism
Nurdism

Reputation: 608

PHP MYSQL Error, Invalid query (but its not invalid)

I'm running into a bit of a problem and I can't figure out how to fix it

PHP Code using

$date = mysql_real_escape_string(date("Y/m/d"));
$title = mysql_real_escape_string($_POST["title"]);
$post = mysql_real_escape_string($_POST["post"]);
$sql =  printf("INSERT INTO `modcraft_mods`.`news` (`date`, `title`, `poster`, `post`) VALUES ('%s', '%s', '%s', '%s');" , $date, $title, mysql_real_escape_string($user->data['username']), $post);
$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

Output:

INSERT INTO modcraft_mods.news (date, title, poster, post) VALUES ('2011/09/19', 'Test Title', 'PixelMaster', 'Test Post');

Invalid query: 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 '151' at line 1

The weird this is that I ran the query in PhPMyAdmin and it worked fine... Does anyone know what could cause this?

Upvotes: 0

Views: 1714

Answers (1)

uadnal
uadnal

Reputation: 11425

printf returns an integer (length of the string), not a string. Use sprintf instead.

http://php.net/manual/en/function.printf.php

https://www.php.net/manual/en/function.sprintf.php

Upvotes: 5

Related Questions