Dmitrij Holkin
Dmitrij Holkin

Reputation: 2055

Mysql insert data if not exist else show warning

I need to check if value already exist in table and insert it if not exist, else i need to show alert about exception

Now i insert only data without checking

if(isset($_POST['new_ok'])){$result = mysql_query("INSERT INTO db1 (name) VALUES('".$name."')",$db);}

I trying do like that

if(isset($_POST['new_ok'])){if(mysql_num_rows(mysql_query("SELECT name FROM db1 WHERE name= '$name'");) != 0){$result = mysql_query("INSERT INTO db1 (name) VALUES('".$name."')",$db);}else{echo "wrong";}}

But mysql_num_rows always return 1

Upvotes: 2

Views: 324

Answers (2)

student
student

Reputation: 64

try this example

$sq="SELECT * FROM db1 WHERE username='$name'";
 $rsult=mysql_query($sq);      $count=mysql_num_rows ($rsult); 
if($count==1){echo "username already in use"; }

else 
 {//update statement here}

Upvotes: 1

Juno Im
Juno Im

Reputation: 77

ERE name= '$name'");) != 0){$read

I think you should delete semicolon here.

Upvotes: 0

Related Questions