0

I have got this function:

   public static function insert_user($user)
{
    $con = mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("speakom",$con) or die(mysql_error());
    mysql_query("INSERT INTO user (user_ip,user_name,full_name,email_address,password,gender,birthday,banned,role,country) 
        VALUES('".$user->ip."','".$user->name."','".$user->full_name."','".$user->email."','".$user->password."',".$user->gender.",'".$user->birthday."',".$user->banned.",".$user->role.",'".$user->country."'") or die(mysql_error());
    mysql_close($con);
}

And I get this 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 '' at line 2

Where does the error point to ? how do I know where the error is?

0

5 Answers 5

3

You're missing the closing ) from the VALUES ( clause. In general, it's easier to assign your SQL to a variable (which you can output for debugging purposes like this) prior to passing it to mysql_query.

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

Comments

2

Instead of yelling you should use PDO and prepared statements, here's the answer in PDO style:

$con = new PDO('mysql:host=localhost;dbname=speakom', 'root', ''); // optionally add encoding options
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // enable exception throwing
$stmt = $db->prepare('INSERT INTO user (user_ip, user_name, full_name, email_address, password, gender, birthday, banned, role, country) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$stmt->execute(array(
    $user->ip, $user->name, $user->full_name, $user->email, $user->password, 
    $user->gender, $user->birthday, $user->banned, $user->role, $user->country,
));

Disclaimer didn't test this, but it should give you a good idea :)

Comments

1

would you run

echo "INSERT INTO user (user_ip,user_name,full_name,email_address,password,gender,birthday,banned,role,country)      VALUES('".$user->ip."','".$user->name."','".$user->full_name."','".$user->email."','".$user->password."',".$user->gender.",'".$user->birthday."',".$user->banned.",".$user->role.",'".$user->country."'";

and i advise you to use `user` instead of user

Comments

1
VALUES('".$user->ip."','".$user->name."','".$user->full_name."','".$user->email."','".$user->password."',".$user->gender.",'".$user->birthday."',".$user->banned.",".$user->role.",'".$user->country."'"

You are missing ) at the end. By the way, use PDO or mysqli.

2 Comments

what does it give me mysqli? are those prepared statements?
1

Some of the values you want to insert are not in quote, and you missed the closing ) for VALUES. Try this

mysql_query("INSERT INTO user (user_ip,user_name,full_name,email_address,password,gender,birthday,banned,role,country) 
    VALUES('$user->ip', '$user->name','$user->full_name', '$user->email', '$user->password', '$user->gender', '$user->birthday', '$user->banned', '$user->role', '$user->country')") or die(mysql_error());

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.