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
  • What error do you get? What have you done to debug this? I see no error checking or handling in your code. You should be using mysql_error() to see what error MySQL reports. Commented Apr 25, 2014 at 19:58
  • 4
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. You are also wide open to SQL injections Commented Apr 25, 2014 at 19:58
  • What is a obvious difference between the insert and select - statements is the tablename and the explicit expression of fields. Perhaps something is wrong there? you should post the table schema. Typos are quiet common. Commented Apr 25, 2014 at 20:03

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. :)

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

2 Comments

Your question reminded me that I didn't use die(mysql_error());, used it and yes it helped. Thanks, @dootzky
you are most welcome :) have a great weekend @abdel-rahman-shoman
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 Comment

I just forgot to print the error message, so yes I did and figured out the issue Thnaks, @JohnnyFaldo
0

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

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.