我构建了一个非常简单的PHP表单,允许用户使用以下PHP代码发送应用程序:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$host = '###';
$username = '###';
$pass = '###';
mysql_connect($host,$username,$pass);
mysql_select_db("###");
$status = mysql_real_escape_string($_POST['status']);
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$url = mysql_real_escape_string($_POST['url']);
$query = "INSERT INTO creathive_applications
VALUES (NULL,'$status','$firstname','$lastname','$email','$url')";
$result = mysql_query($query) or trigger_error(mysql_error().". Query: ".$query);
}
我想要做的是确保同一个人不应用TWICE,所以如果电子邮件地址已经存在于数据库中,那么它将在表单上显示一条消息“抱歉看起来你已经应用了”
这是HTML表单,我已在字段集中添加了消息,因此需要执行a)如果电子邮件退出或显示成功消息,则显示此消息然后b)将#membership表单添加到URL以使视图跳转到页面上的表单,以便用户将看到消息。对此可以有任何帮助吗?致谢
<form action="" method="post">
<fieldset id="membershipform">
<div id="error"><p>sorry email in use</p></div>
<div id="success"><p>Thanks your application has been sent</p></div>
<ul class="clearfix">
<li id="li-status">
<span>I am a:</span>
<menu>
<li><label for="student"><input type="radio" name="status" id="student" checked="checked" value="Graduate" /> Graduate</label></li>
<li><label for="student2"><input type="radio" name="status" id="student2" value="Undergraduate" /> Undergraduate</label></li>
</menu>
</li>
<li id="li-firstname">
<label for="firstname">First Name</label> <input name="firstname" type="text" placeholder="First Name" id="firstname" title="First Name" />
</li>
<li id="li-lastname">
<label for="lastname">Last Name</label> <input name="lastname" type="text" placeholder="Last Name" id="lastname" title="Last Name" />
</li>
<li id="li-email">
<label for="email">Email address</label> <input name="email" type="text" placeholder="Email address" id="email" title="Email address" />
</li>
<li id="li-url">
<label for="url">URL</label> <input name="url" type="text" placeholder="URL of something you've made" id="url" title="URL of something you've made" />
</li>
<li id="li-buttons">
<input name="submit" type="submit" value="Send Application ►" title="Send Application" />
</li>
</ui>
</fieldset>
</form>
答案 0 :(得分:0)
在插入之前执行选择查询以验证电子邮件没有任何条目:
select from creathive_applications where email = $email
如果有任何结果返回,则显示您的消息而不是插入记录。如果电子邮件已经存在,您可以添加javascript onload代码以将表单移动到#membershipform。
答案 1 :(得分:0)
您可以更改表格,以便电子邮件ID是主要的,因此它将是唯一的。
您可以查看其他查询
从creathive_applications中选择emai ='$ email'
答案 2 :(得分:0)
很简单,您必须根据数据库进行检查。
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$host = '###';
$username = '###';
$pass = '###';
mysql_connect($host,$username,$pass);
mysql_select_db("###");
$status = mysql_real_escape_string($_POST['status']);
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$url = mysql_real_escape_string($_POST['url']);
$query_check = "SELECT COUNT(*) as existents FROM creathive_applications .... emailfiel = 'email_entered'";
switch($query_check['existents']){
case 0:
//do the insert operation
$query = "INSERT INTO creathive_applications VALUES (NULL,'$status','$firstname','$lastname','$email','$url')";
$result = mysql_query($query) or trigger_error(mysql_error().". Query: ".$query);
break;
default:
echo "no no";
}
}
答案 3 :(得分:0)
检查数据库中是否存在$email
,以及是否设置了$error = 1
。然后,如果$error = 1
,则打印消息。
$emailchk = mysql_query("SELECT * FROM creathive_applications WHERE email = '$email'");
if(mysql_num_rows($emailchk) > 0) {
$error = 1;
}
if isset($error) {
echo '<div id="error"><p>sorry email in use</p></div>';
}
答案 4 :(得分:0)
你可以这样做:
PHP代码:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$host = '###';
$username = '###';
$pass = '###';
$emailerror = null;
mysql_connect($host,$username,$pass);
mysql_select_db("###");
$status = mysql_real_escape_string($_POST['status']);
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$url = mysql_real_escape_string($_POST['url']);
$emailchk = mysql_query("SELECT * FROM creathive_applications WHERE email = '$email'");
if(mysql_num_rows($emailchk) == 0) {
$query = "INSERT INTO creathive_applications VALUES (NULL,'$status','$firstname','$lastname','$email','$url')";
$result = mysql_query($query) or trigger_error(mysql_error().". Query: ".$query);
}else{
$emailerror = '<span style="color:rgb(255,0,0)">Email already taken</span>';
}
}
HTML (必须在同一个文件中):
<form action="" method="post">
<fieldset id="membershipform">
<div id="error"><p>sorry email in use</p></div>
<div id="success"><p>Thanks your application has been sent</p></div>
<ul class="clearfix">
<li id="li-status">
<span>I am a:</span>
<menu>
<li><label for="student"><input type="radio" name="status" id="student" checked="checked" value="Graduate" /> Graduate</label></li>
<li><label for="student2"><input type="radio" name="status" id="student2" value="Undergraduate" /> Undergraduate</label></li>
</menu>
</li>
<li id="li-firstname">
<label for="firstname">First Name</label> <input name="firstname" type="text" placeholder="First Name" id="firstname" title="First Name" />
</li>
<li id="li-lastname">
<label for="lastname">Last Name</label> <input name="lastname" type="text" placeholder="Last Name" id="lastname" title="Last Name" />
</li>
<li id="li-email">
<label for="email">Email address</label> <input name="email" type="text" placeholder="Email address" id="email" title="Email address" /> <?php echo $emailerror;?>
</li>
<li id="li-url">
<label for="url">URL</label> <input name="url" type="text" placeholder="URL of something you've made" id="url" title="URL of something you've made" />
</li>
<li id="li-buttons">
<input name="submit" type="submit" value="Send Application ►" title="Send Application" />
</li>
</ui>
</fieldset>
</form>