如果在mySQL中已存在记录,如何阻止我的表单添加记录?

时间:2016-08-24 12:53:52

标签: php mysql forms duplicates

我创建了一个脚本,该脚本已经可以将玩家添加到团队数据库中,以及向某人发送电子邮件进行检查。但是我想添加一个例外,如果记录已经存在,则屏幕上会显示错误消息。

我尝试过使用not exists语句以及if语句的替代方法。但是除了添加空行之外,数据库中没有任何事情发生,不添加数据。我正在使用php来做到这一点。

<?php
include("file:///C|/Users/Duncan-PC2/Documents/tugay/connect.php");

$name = trim(mysql_real_escape_string($_POST["name"]));
$surname = trim(mysql_real_escape_string($_POST["surname"]));   
$dob = trim(mysql_real_escape_string($_POST["dob"]));
$email = trim(mysql_real_escape_string($_POST["email"]));
$phone = trim(mysql_real_escape_string($_POST["contact_no"]));
$nat = trim(mysql_real_escape_string($_POST["nat"]));
$address = trim(mysql_real_escape_string($_POST["address"]));
$transfer = trim(mysql_real_escape_string($_POST["transfer"]));
$team_name = trim(mysql_real_escape_string($_POST["team_name"]));
$licence = trim(mysql_real_escape_string($_POST["licence"]));
$division = trim(mysql_real_escape_string($_POST["division"]));
$img = addslashes(file_get_contents($_FILES['image']['tmp_name']));

$result = mysql_query("SELECT name FROM teams WHERE name = '$name' and    surname = '$surname'");

if(mysql_num_rows($result) == 0) {

    mysql_query("INSERT INTO teams (id, name, surname, dob, email, contact_no, nat, address, transfer, team_name, licence, division, image)
    VALUES ('', '$name', '$surname', '$dob', '$email', '$phone', '$nat', '$address', '$transfer', '$team_name', '$licence', '$division', '$img')");

    $to = 'lisanssekreteri@gmail.com  '; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
    $email_subject = "New Player for $team_name Added";
    $email_body = "$team_name have added a new player! \n\n"."Here are the details:\n\nName: $name $surname\n\n Please go into the system and check this Player. ";
    $headers = "From: TCFF PLAYER REGISTRATION\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
    $headers .= "Reply-To: $email_address"; 
    mail($to,$email_subject,$email_body,$headers);  


    echo ('<link href="css/bootstrap.css" rel="stylesheet" type="text/css" />

    <h1 class="col-lg-10">
    <div class="alert alert-info alert-dismissable">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    <i class="fa fa-info-circle"></i> PLEASE READ: This player has been added, please go to the red tab labelled with non approved players! Please do not attempt to add this player again. THIS PLAYER IS NOW APART OF YOUR TEAM AND WILL BE ACTIVATED SOON please be patient!</h1>
    <h1 class="col-lg-10">Thankyou This Player Has Been Succefully Added To Your Team! 

                    </div></h1>
    <span class="col-lg-10">
    <button class="btn-lg btn btn-danger" onclick="history.go(-2);">  Click Here To Go Back To Admin Panel, Please do not try to re-add this player!</button>
    </span>');

} else {

    echo ("<h1>This Player Already Exists or is apart of another team</h1>");

}  

1 个答案:

答案 0 :(得分:0)

您可以通过首先检查记录是否已存在来执行SELECT查询来防止双重插入。看一下下面的例子:

SELECT id
FROM some_table
WHERE something='bla';

// If the above query does not return any results, you can insert.
INSERT INTO some_table (something) VALUES ('bla');

修改

这可能无法解决问题,但我发现按照以下方式执行查询字符串是很好的做法:

$result = mysql_query("SELECT name FROM teams WHERE name = '".$name."' and    surname = '".$surname."'");
相关问题