INSERT INTO syntax Error MYSQL with PHP

I attempt to insert data with this code $user_name = mysqli_real_escape_string($mysqli, $POST[“user_name”]); $user_pass = mysqli_real_escape_string($mysqli, $POST[“user_pass”]); $user_email = mysqli_real_escape_string($mysqli, $POST[“user_email”]); $sql = “INSERT INTO users(id, user_name, user_pass, user_email ,user_level) VALUES(NULL, $user_name, $user_pass, $user_email, 0)”; $result = mysqli_query($mysqli, $sql); However when I actually launch the query, I get an error that says this. 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 ’ , , 0)’ at line 4 I also know this isn’t a very secure at all, but it’s for school and not for actual use.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.


As a follow up, given the fact that passing options to PDO directly may cause issues (such as some of the options not properly setting), you may want to use foreach to iterate over the options and pass them in using setAttribute, like so:

$options    =   [
                    PDO::ATTR_EMULATE_PREPARES  =>  false,
                    PDO::ATTR_ERRMODE           =>  PDO::ERRMODE_EXCEPTION

$db         =   new PDO( 'mysql:host=HOSTNAME;dbname=DBNAME;charset=utf8', 'DBUSERNAME', 'DBPASSWORD' );

if ( ! empty( $options ) )
    foreach ( $options as $key => $value )
        $db->setAttribute( $key, $value );

Note, we’ve changed:

$db         =   new PDO( 'mysql:host=HOSTNAME;dbname=DBNAME;charset=utf8', 'DBUSERNAME', 'DBPASSWORD', $options );


$db         =   new PDO( 'mysql:host=HOSTNAME;dbname=DBNAME;charset=utf8', 'DBUSERNAME', 'DBPASSWORD' );

… so we’re no longer passing in $options when constructing our PDO object.

This question was answered by @jtittle:

The first thing I’d take a look at is your connection details; errors can begin & end there. Without your actual username & password in the details, please post a follow up with the full connection details, or, if it’s a rather large class (as an example), use and post a link. This will keep the post a little cleaner and PB offers syntax spacing/highlighting :).

That being said, I would honestly take a look at PDO if you’re running PHP 5.3.3+. It’s better to design a script or class upfront with security in mind rather than add it as a “feature” later on (i.e. when it may be too late).

Just a basic example of how to connect to PDO and also how to insert data. This specific example is using bindParam in an array (due to the number of fields). It can also be written out for each variable without the array (see below the link).

This line (from the PastBin above)…

$stmt->execute( array( ‘:field1’ => $field1, ‘:field2’ => $field2, ‘:field3’ => $field3, ‘:field4’ => $field4, ‘:field5’ => $field5 ) );

is the same as:

$stmt->bindParam( ‘:field1’, $field1 ); $stmt->bindParam( ‘:field2’, $field2 ); $stmt->bindParam( ‘:field3’, $field3); $stmt->bindParam( ‘:field4’, $field4 ); $stmt->bindParam( ‘:field5’, $field5 );


View the original comment