mysqli_query insert success and throws error [dupl

2019-09-24 12:34发布

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();

标签: php mysqli
2条回答
男人必须洒脱
2楼-- · 2019-09-24 13:10

Here's your problem:

$sql = mysqli_query($conn, $query);

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $query . "<br>" .$conn->error;
}

You call mysqli_query twice, the first one inserts the record:

$sql = mysqli_query($conn, $query);

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

$result = mysqli_query($conn, $query);

if ($result) {
    echo "New record created successfully";
} else {
    echo "Error: " .  . "<br>" .  mysqli_error($conn);
}
查看更多
欢心
3楼-- · 2019-09-24 13:31

I think I got it. You check for $conn->query($sql) === TRUE but it isn't: http://php.net/manual/de/mysqli.query.php

It'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:

mysqli:query() Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

You have an INSERT query, so most likley my guess was wrong.

查看更多
登录 后发表回答