0

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?

0

1 Answer 1

5

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

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

1 Comment

As an addendum: to fix the issue, use $sql = sprintf(...

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.