INSERT INTO syntax Error MYSQL with PHP

February 19, 2015 2.2k views

I attempt to insert data with this code
$username = mysqlirealescapestring($mysqli, $POST["username"]);
$user
pass = mysqlirealescapestring($mysqli, $POST["userpass"]);
$useremail = mysqlirealescapestring($mysqli, $POST["useremail"]);
$sql = "INSERT INTO
users(id, user
name, userpass, useremail ,userlevel)
VALUES(NULL,
$user
name,
$userpass,
$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.

1 comment
  • 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 PasteBin.com 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).

    http://pastebin.com/svQxwNE6

    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 );
    
    $stmt->execute();
    
2 Answers

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 PasteBin.com 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).

http://pastebin.com/svQxwNE6

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

$stmt->execute();

View the original comment

@VPFedoraguy

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

to:

$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.

Have another answer? Share your knowledge.