Colin Colin - 6 months ago 31
HTML Question

PHP insert into mysql table with no errors doesn't work

I have troubles with my PHP script. I want to insert something in a SQL table, but it doesn't work. There's no error or thing like that. It just doesn't insert the values.

Here's the code:

require_once("php_con.php");

$query = sprintf(
"INSERT INTO carts (id, ordernumber, quantity) VALUES (%s, %s, %s)",
$mysqli->real_escape_string($_SESSION['user']),
$mysqli->real_escape_string($_POST['ordernumber']),
$mysqli->real_escape_string($_POST['quantity'])
);
$mysqli->query($query);


and here is my php_con.php which is in the same folder:

$mysqli = @new mysqli('localhost', 'hah', 'password', 'hah');


My selects do work, but my inserts don't. I know the difference between single- and double quotes, it seems that I forgot something.

Answer

You can try this:

require_once("php_con.php");

$query = sprintf(
    "INSERT INTO carts (`id`, `ordernumber`, `quantity`) VALUES ('%s', '%s', '%s')",
    $mysqli->real_escape_string($_SESSION['user']),
    $mysqli->real_escape_string($_POST['ordernumber']),
    $mysqli->real_escape_string($_POST['quantity'])
);
$mysqli->query($query);

Check error logs to see if there is an error.