gpt4 book ai didi

php - 检查重复的数据库条目

转载 作者:行者123 更新时间:2023-11-29 05:35:41 24 4
gpt4 key购买 nike

我最近一直在做一个表格。它工作正常。我只想对其添加一个调整,如果注册一个已经在数据库中注册的电子邮件 ID,它会显示错误。这是我的整个 PHP 代码::

<?php
//include the connection file

require_once("validation.php");

if( isset($_POST['send']) && (!validateName($_POST['name']) || !validateEmail($_POST['email']) || !validatePasswords($_POST['pass1'], $_POST['pass2']) || !validateContact($_POST['contact']) || !validateAge($_POST['age'])) ):?>
<div id="error">
<ul>
<? if(!validateName($_POST['name'])):?>
<li><strong>Invalid Name:</strong> We want names with more than 3 letters.</li>
<? endif?>
<? if(!validateEmail($_POST['email'])):?>
<li><strong>Invalid E-mail:</strong> Type a valid e-mail please.</li>
<? endif?>
<? if(!validatePasswords($_POST['pass1'], $_POST['pass2'])):?>
<li><strong>Passwords are invalid:</strong> Passwords doesnt match or are invalid!</li>
<? endif?>
<? if(!validateContact($_POST['contact'])):?>
<li><strong>Please enter a valid number.</strong></li>
<? endif?>
<? if(!validateAge($_POST['age'])):?>
<li><strong>Please enter a valid age</strong></li>
<? endif?>
<? if(!validateWork($_POST['work'])):?>
<li><strong>Please enter work</strong></li>
<? endif?>
</ul>
</div>
<?php elseif(isset($_POST['send'])):?>
<div id="error" class="valid">
<ul>
<?php
require_once('connection.php');
$query = "INSERT INTO employee (name, password, email, contact, age, gender, location, skill, work) VALUES ";
$query .= "('".$_POST['name']."', '".$_POST['pass1']."', '".$_POST['email']."','".$_POST['contact']."','".$_POST['age']."','".$_POST['gender']."','".$_POST['location']."','".$_POST['skill']."','".$_POST['work']."')";
// run the query
mysql_query($query);?>
<li><strong>Congratulations!</strong> You have been successfully registered!</li>
</ul>
</div>
<?php endif?>

最佳答案

您可以给电子邮件字段一个 UNIQUE 约束,这样就不会出现重复。在这种情况下,INSERT 查询将失败,您的查询将返回 false

if (!mysql_query($query)) {
// Error, email already exists, no data was inserted
} else {
// Successfully inserted row, print success message.
}

INSERT 查询实际上会失败,而不仅仅是返回 0 行已修改。

关于php - 检查重复的数据库条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10864768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com