By VPFedoraguy
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.
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!
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
bindParamin 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 );
$stmt->execute();
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.