简体   繁体   English

MYSQL-仅插入不存在的记录吗?

[英]MYSQL - Only insert record if it does not already exist?

I'm a noobie at mysql... but I'm trying to get this to check for a duplicate record based on the serialkey AND program. 我是mysql的noobie ...但是我正试图让它检查基于serialkey和程序的重复记录。 The record can only be submitted if the serialkey has not been submitted for that specific program already. 仅当尚未为该特定程序提交序列号时,才可以提交记录。 1 unique key is allowed PER program. PER程序允许使用1个唯一密钥。

num is the unique number for each record num是每个记录的唯一编号

I had everything working properly with the record being submitted, BESIDES this duplicate check. 除此重复检查外,我已使所有工作与提交的记录一起正常工作。 Remember, I want the key to be allowed to be submitted multiple times, but ONLY once PER program. 记住,我希望密钥可以多次提交,但每个程序只能提交一次。

 <?php $title = "Product Key Submission"; $con=mysqli_connect("URL HERE","USER HIDDEN","PASS HIDDEN","DB"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // escape variables for security $firstname = mysqli_real_escape_string($con, $_POST['firstname']); $lastname = mysqli_real_escape_string($con, $_POST['lastname']); $email = mysqli_real_escape_string($con, $_POST['email']); $program = mysqli_real_escape_string($con, $_POST['program']); $key = mysqli_real_escape_string($con, $_POST['serialkey']); $time = time(); $registered = date('Ymd g:i:sa',$time); if ($firstname == ""){ echo "<h1>BLANK FIELD FOR NAME</h1>"; mysqli_close($con); } else { //###################### CHECK IF RECORD EXISTS ###################### $query = mysqli_query("SELECT count(*) AS 'num' FROM `product_keys` WHERE `program` = '$program' AND `serial_key` = '$key'"); $num = mysqli_fetch_assoc($query); if ($num['num'] >= 1) { echo "You have reached your limit of key submissions."; } //###################### CHECK IF RECORD EXISTS ###################### else { echo "THE KEY WILL BE SUBMITTED..."; 

You can leave your code the way it is, with the exception of: 您可以按原样保留代码,但以下情况除外:

Do: 做:

$query = mysqli_query($con,"SELECT...

The problem is that you are not connecting with DB. 问题是您没有与数据库连接。


You may also have error reporting's default set to off. 您也可能将错误报告的默认设置设为关闭。

It should have thrown the following warnings: 它应该引发以下警告:

Warning: mysqli_query() expects at least 2 parameters, 1 given in... 警告:mysqli_query()至少需要2个参数,其中1个参数在...

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in... 警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在参数null中给出...

Meaning that mysqli_query() expects at least 2 parameters is looking for a DB connection and the second warning message is because it enchains with the first warning and continues until it finds some form of success to stop the process. 这意味着mysqli_query() expects at least 2 parameters正在寻找数据库连接,而第二条警告消息是因为它与第一个警告结合在一起,并一直持续到找到某种形式的成功来停止该过程为止。

which is why it was going to your 这就是为什么要去你的

else {
        echo "THE KEY WILL BE SUBMITTED...";

condition. 条件。

Add error reporting to the top of your file(s) which will help during production testing. 错误报告添加到文件顶部,这将在生产测试期间提供帮助。

error_reporting(E_ALL);
ini_set('display_errors', 1);

as well as or die(mysqli_error($con)) to mysqli_query() . 以及or die(mysqli_error($con)) mysqli_query()

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

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