1

I'm trying to use PDO on my register and log in page , and started working on my register page and have an syntax error not sure why.

The error is below:

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 'INSERT INTO users (id, name, username, password, lastname ,email) VALUES (?, ?,' at line 1

Code:

 if ($_SESSION["username"]){

 header("Location: home.php");
 }
else{
if(isset($_POST['firstname']) && isset($_POST['lastname']) &&    isset($_POST['username']) && isset($_POST['password']) && isset($_POST['cpassword'])  && isset($_POST['email'])){
     require('connect.php');

     $dusername = mysql_real_escape_string($_POST["username"]);
    $dfirstname = mysql_real_escape_string($_POST["firstname"]);
     $dpassword = md5(mysql_real_escape_string($_POST["password"]));
    $dlastname =mysql_real_escape_string($_POST["lastname"]);
    $demail =mysql_real_escape_string($_POST["email"]);




$dbhost = "xxxxxxx"; 
$dbname= "xxxxxx";
$dbuser = "xxxx";
$mysql_password = "xxxxx";



 try{


 $db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname.';charset=utf8',   

 ''.$dbuser.'',    

 ''.$mysql_password.'');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}

catch(PDOException $e){
echo $e->getMessage();
die();


}

 mysql_error();
  //According to user's input
 $register =mysql_query( "(INSERT INTO users (id, name, username, password, lastname,email)  VALUES (?, ?, ?, ?, ?)")or die(mysql_error());
 $register =$db->prepare($register);
 $register->execute(array($dfirstname,$dusername,$dpassword,$dlastname,$demail));

// Check username and password match
if ($register) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
// Jump to secured page
header('Location: home.php');
exit;
}
else{

  echo "<div class=\"error\">Error Signing Up... Make Sure all the require 

  fields         are correctly formatted. </div>";
    }
    }
 else{

 }
 }

Updated it

  $register ="INSERT INTO users (id, name, username, password, lastname ,email) VALUES('',?, ?, ?, ?, ?)"or die(mysql_error());
  $register =$db->prepare($register);
  $register->execute(array('',$dfirstname,$dusername,$dpassword,$dlastname,$demail));

Still have the error

6
  • the INSERT INTO,line is where it is saying the error is Commented Dec 18, 2014 at 6:57
  • you cannot mix mysql_* functions and PDO methods, stick with PDO Commented Dec 18, 2014 at 6:58
  • Please add the error message. Commented Dec 18, 2014 at 6:59
  • so mysql_query i don't need it? @Ghost Commented Dec 18, 2014 at 7:00
  • @FollowerofChrist you wouldn't need one bit of it, all of this can be done with just PDO Commented Dec 18, 2014 at 7:01

2 Answers 2

2
$register->execute(array($dfirstname,$dusername,$dpassword,$dlastname,$demail));

should be

$register->execute(array('',$dfirstname,$dusername,$dpassword,$dlastname,$demail));
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the edit but the error still exist @virendra
also change this one $register =mysql_query( "(INSERT INTO users (id, name, username, password, lastname,email) VALUES (?,?, ?, ?, ?, ?)")or die(mysql_error());
1

You have 6 columns in your insert statement but only 5 values in the values clasue. That should also be 6.

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.