开发者

Check if value exists before inserting into MySQL DB in a PHP script

开发者 https://www.devze.com 2023-01-30 00:19 出处:网络
I\'ve looked for similar questions with no success. I have this piece of code: form1.php $query= \"INSERT INTO table1 \";

I've looked for similar questions with no success.

I have this piece of code:

form1.php

$query  = "INSERT INTO table1 ";
$query .= "(fname, lname, mail)";
$query .= " VALUES ";
$query .= "('".$_POST[f开发者_Python百科name]."', '".$_POST[lname]."', '".$_POST[mail]."')"; 

$result = mysql_query($query) or die ("Query Failed: " . mysql_error());

And I want that the script will check if the value inserted exists in the corresponding column, and throw an error if it does. any ideas?


Create a UNIQUE key on the fields you care about, and detect the integrity error after the fact.

0

精彩评论

暂无评论...
验证码 换一张
取 消