简体   繁体   English

在 PHP 脚本中插入 MySQL 数据库之前检查值是否存在

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

I've looked for similar questions with no success.我一直在寻找类似的问题,但没有成功。

I have this piece of code:我有这段代码:

form1.php表单1.php

$query  = "INSERT INTO table1 ";
$query .= "(fname, lname, mail)";
$query .= " VALUES ";
$query .= "('".$_POST[fname]."', '".$_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?有任何想法吗?

在您关心的字段上创建一个 UNIQUE 键,并在事后检测完整性错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM