$fname=$_POST['fname'];
$lname = $_POST['lname'];
$Phone = $_POST['Phone'];
$date = date("Y/m/d");
mysql_query ( "INSERT INTO example (fname,lname,Phone,date) VALUES ('$fname' ,'$lname', '$Phone', '$date')")
or die(mysql_error());
当我在我的代码中写入重复条目时,这些错误将显示: 我的PRIMARY键是Phone。
Duplicate entry '' for key 'PRIMARY'
但我希望在添加数据之前通过JavaScript检查并显示以防止此错误。 这怎么可能?请完全帮助和线源。感谢
答案 0 :(得分:0)
$fname=$_POST['fname'];
$lname = $_POST['lname'];
$Phone = $_POST['Phone'];
$date = date("Y/m/d");
$query = "SELECT * FROM example WHERE Phone = '".mysql_real_escape_string($_POST['Phone'])."'";
if (mysql_num_rows(mysql_query($query)) > 0)
{
print "inuse";
}
else
{
mysql_query ( "INSERT INTO example (fname,lname,Phone,date) VALUES ('$fname' ,'$lname', '$Phone', '$date')")
or die(mysql_error());
}