1

So what I am trying to do is a very basic and straight way of inserting a record into mysql db.

It is just something I have done few times before, however for some reason it is not working with me this time.

So in the following couple of lines of code I will show my code, which basically do the following

1- Check if the user exists in the DB (An existing user is a user with the same email)

2- If the user exists in the DB then it sends an http response with a status code of 409 which means duplication.

(Anyway note that this works perfectly, which implies the connection was made successfully to the DB, and it was able to retrieve any exact user, if any)

3- If the user does not exist it should be inserted in the DB (Here is the problem)

My Code

 //Checking if the user exist
 $result = mysql_query("SELECT * FROM $table_name WHERE email='".$post_email."'",$con) or die ('Error: '.mysql_error ());
 $num_rows = mysql_num_rows($result);

 if($num_rows > 0){
  // Close Connection
   mysql_close($con);

  echo "409";

 }
 else{
     mysql_query("INSERT INTO samam_users (username,password,email) VALUES ('ALI','AHMED','[email protected]')",$con);
    // Select the record
    $user_id = mysql_insert_id();
    $result = mysql_query("SELECT * FROM $table_name WHERE email='".$post_email."'",$con) or die ('Error: '.mysql_error ());
     // Close Connection
     mysql_close($con);

     echo "200 " . $result['username'];
  }

I googled the possible solutions for this issue, however all similar issues I went through were because of syntax errors.

Any suggestions? Thanks in advance :)

3

3 Answers 3

2

What is the exact error message you are getting? Copy/paste that here, please.

Also, the only odd thing I see is that you are doing the SELECT commands with a variable $table_name, and in the INSERT command you are hard-coding a table name..? Maybe that's it?

INSERT INTO samam_users ...

just put the same table name variable there? INSERT INTO $table_name ...

Let me know if this helps. :)

2
  • Your question reminded me that I didn't use die(mysql_error());, used it and yes it helped. Thanks, @dootzky
    – A.Shoman
    Apr 26, 2014 at 9:29
  • you are most welcome :) have a great weekend @abdel-rahman-shoman
    – d-wade
    Apr 26, 2014 at 14:48
2
$sql  = "INSERT INTO samam_users (username,password,email) VALUES ('ALI','AHMED','[email protected]')";


if(!mysql_query($sql,$con)) {
   die(mysql_error());
}else {
   echo 'inserted succesfully'; 
}

mysql_error() will give you information about why your query isn't working - allowing you to debug it.

Also don't use mysql_*, it's going to be deprecated and there are much better more secure options like MySQLi or preferably PDO

1
  • I just forgot to print the error message, so yes I did and figured out the issue Thnaks, @JohnnyFaldo
    – A.Shoman
    Apr 26, 2014 at 9:31
0

I think you have to put all the values in INSERT command in double quotes instead of single quote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.