在PHP脚本中将值插入MySQL DB之前检查值是否存在 [英] Check if value exists before inserting into MySQL DB in a PHP script

查看:61
本文介绍了在PHP脚本中将值插入MySQL DB之前检查值是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找类似的问题,但没有成功.

I've looked for similar questions with no success.

我有这段代码:

form1.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密钥,并在事后检测完整性错误.

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

这篇关于在PHP脚本中将值插入MySQL DB之前检查值是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆