0

With this query I get an error "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..."

$sql = 'INSERT INTO articles (  long_text )
        VALUES ("' . mysqli_real_escape_string($conn, $long) . '")';

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

Does anyone know what is causing the problem?

3
  • 2
    Add the complete error message. learn about prepared statements Commented Mar 9, 2016 at 10:25
  • add after $sql variable echo $sql; and show us result Commented Mar 9, 2016 at 10:26
  • 1
    You are inserting the text inside the query sourrounded by double quote ("), while in SQL text should be sourrounded by single quote ('). Commented Mar 9, 2016 at 10:31

1 Answer 1

1

Quotes problem in your query. You are tring to write double quotes inside single quotes.

You need to change your query as prepare statement as

$stmt = $conn->prepare("INSERT INTO articles (`long_text`) VALUES (?)");
$stmt->bind_param('s', $long);
/* execute prepared statement */
$stmt->execute();

printf("%d Row inserted.\n", $stmt->affected_rows);

/* close statement and connection */
$stmt->close();

Read http://php.net/manual/en/mysqli-stmt.bind-param.php

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

Comments

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.