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','amsh-1992@hotmail.com')",$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 :)

Abdelrahman Shoman
  • 2,882
  • 7
  • 36
  • 61
  • 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. – John Conde Apr 25 '14 at 19:58
  • 4
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Apr 25 '14 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. – Peter Apr 25 '14 at 20:03

3 Answers3

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

d-wade
  • 621
  • 7
  • 9
2
$sql  = "INSERT INTO samam_users (username,password,email) VALUES ('ALI','AHMED','amsh-1992@hotmail.com')";


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

JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
0

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