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

Php 如果mySQL中已经存在记录,如何阻止表单添加记录?,php,mysql,forms,duplicates,Php,Mysql,Forms,Duplicates,我已经创建了一个脚本,可以将一名球员添加到团队数据库中,还可以发送电子邮件给其他人进行检查。但是,我想添加一个例外,如果记录已经存在,屏幕上会出现一条错误消息 我尝试使用notexists语句以及if语句。但是,除了添加一个空行之外,数据库中什么也没有发生,只添加了一部分数据。我正在使用php来实现这一点 <?php include("file:///C|/Users/Duncan-PC2/Documents/tugay/connect.php"); $name = trim(mysql

我已经创建了一个脚本,可以将一名球员添加到团队数据库中,还可以发送电子邮件给其他人进行检查。但是,我想添加一个例外,如果记录已经存在,屏幕上会出现一条错误消息

我尝试使用notexists语句以及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>");

}  

您可以通过先执行
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."'");

在列上添加唯一的约束。为什么要使用过时的代码?欢迎来到21世纪。“如何阻止我的表单添加记录”-什么表单?另外,不知道您是如何访问该文件的。这是什么类型的时髦语法<代码>file:///C|/
您似乎并不急于回答这个问题,而是找到了问题或是留下了问题。你已经得到了评论和回答。就我个人而言,我帮不了你。我不会一连几个小时/几天都在问问题。祝你好运。我注意到你的编辑:
“$var”
“$var.”
都产生相同的结果。所以,不是这样。这个问题缺少太多的细节。要给出进一步的答案,我需要知道是否抛出了sql错误。