This question already has an answer here:
This issue has baffled me. I have a simple mysqli query, which successfully inserts the data in db but also throws an error in response.
I know I have to parametrize the query etc. This is just a test run.
$query = "INSERT INTO user(user_id,subdomain) VALUES ('$user_id','$subdomain')";
$sql = mysqli_query($conn, $query);
On execution, I get the following (formatted for legibility) error:
Error: 1 INSERT INTO user(user_id,subdomain) VALUES ('abcdefgh|123456','abcd') 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 '1' at line 1
But when I check the db, the data is inserted properly.
Table schema is given below:
user
Column Type Null Default
id (Primary) int(50) No
user_id varchar(50) No
subdomain varchar(30) No
modified timestamp No CURRENT_TIMESTAMP
Any ideas on what is happening here?
EDIT: adding more code
$host = 'localhost';
$user = 'root';
$pass = 'mysql';
$db = 'localauth0';
$conn = mysqli_connect($host, $user, $pass, $db);
mysqli_set_charset($conn, "utf8");
$user_id = mysqli_real_escape_string($conn, $_POST['user_id']);
$subdomain = mysqli_real_escape_string($conn, $_POST['storeSubdomain']);
// $query = "INSERT INTO user (user_id,subdomain) VALUES ('$user_id','$subdomain')";
$query = "INSERT INTO user(user_id,subdomain) VALUES ('".$user_id."','".$subdomain."')";
$sql = mysqli_query($conn, $query);
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $query . "<br>" . $conn->error;
}
$conn->close();
exit();
Here's your problem:
You call mysqli_query twice, the first one inserts the record:
The second time, it just runs a query on the string "1" but then your error report concatenates the wrong stuff to report the problem.
How about
I think I got it. You check for
$conn->query($sql) === TRUE
but it isn't: http://php.net/manual/de/mysqli.query.phpIt's mixed. And according to the comments, it can also contain a mysqli_result.
Your query didn't fail. Just your if condition does :)
Update: I'm wrong. I really should read the links before I suggest them:
You have an INSERT query, so most likley my guess was wrong.