我正在开发一个带验证的注册表单,我有一个使用mysql的数据库。我想将用户输入的数据与数据库中的数据进行比较,特别是电子邮件,以检查它是否已在数据库中列出/使用。如果用户输入的密码少于6个字符,我已经设置了警报代码,如果电子邮件地址已存在于我的数据库中,我想对其进行相同操作。这是我的代码:
表格代码:
<?php
//Start session
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8">
<title> Athan Motorcycle Spare Parts and Accesories</title>
<link rel="stylesheet" href="css/bootstrap.css" type="text/css"/>
<script type="text/javascript">
function validateForm()
{
var a=document.forms["abc"]["firstname"].value;
var b=document.forms["abc"]["lastname"].value;
var d=document.forms["abc"]["email"].value;
var e=document.forms["abc"]["pword"].value;
var f=document.forms["abc"]["ambot"].value;
var g=document.forms["abc"]["number"].value;
var h=document.forms["abc"]["house"].value;
var i=document.forms["abc"]["street"].value;
var j=document.forms["abc"]["city"].value;
if ((a==null || a==""))
{
alert("you must enter your username");
return false;
}
if ((b==null || b==""))
{
alert("you must enter your password");
return false;
}
if ((d==null || d==""))
{
alert("you must enter your email address");
return false;
}
if ((e==null || e==""))
{
alert("you must enter your password");
return false;
}
if ((f==null || f==""))
{
alert("Retype password");
return false;
}
if ((g==null || g==""))
{
alert("you must enter your contact number");
return false;
}
if ((h==null || h==""))
{
alert("you must enter your house location");
return false;
}
if ((i==null || i==""))
{
alert("you must enter your Street");
return false;
}
if ((j==0 || j==""))
{
alert("Please select your City");
return false;
}
if( e != f ) {
alert("Password does not match");
return false;
}
var atpos=d.indexOf("@");
var dotpos=d.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=d.length)
{
alert("Not a valid e-mail address");
return false;
}
}
</script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//called when key is pressed in textbox
$("#contact").keypress(function (e)
{
//if the letter is not digit then display error and don't type anything
if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
{
//display error message
$("#errmsg").html("Number Only").show().fadeOut("slow");
return false;
}
});
});
</script>
</script>
</head>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<body>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src=”js/bootstrap.js”></script>
<div class="container well">
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<ul class="nav">
<li><img src="img/12.jpg"></a></li>
<li class="active"><a href="index.html">Home</a></li>
<li><a href="customization.html">Customization</a></li>
<li><a href="error.php">Order Parts</a></li>
<li><a href="inspiration.html">Inspiration Gallery</a></li>
<li><a href="about.html">About us</a></li>
<li><a href="loginuser.php">Login</a></li>
</ul>
</div>
</div>
</div>
<div class="hero-unit">
<!-- Skitter Styles -->
<link href="css/skitter.styles.css" type="text/css" media="all" rel="stylesheet" />
<!-- Skitter JS -->
<script type="text/javascript" language="javascript" src="js/jquery-1.6.3.min.js"> </script>
<script type="text/javascript" language="javascript" src="js/jquery.easing.1.3.js"> </script>
<script type="text/javascript" language="javascript" src="js/jquery.animate-colors- min.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.skitter.min.js"></script>
<!-- Init Skitter -->
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('.box_skitter_large').skitter({
theme: 'clean',
numbers_align: 'center',
progressbar: true,
dots: true,
preview: true
});
});
</script>
<style type="text/css">
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
</style>
<br>
<div style="width:400px; margin:0 auto; position:relative; border:3px solid rgba(0,0,0,0); -webkit-border-radius:5px; -moz-border-radius:5px; border-radius:5px; -webkit-box-shadow:0 0 18px rgba(0,0,0,0.4); -moz-box-shadow:0 0 18px rgba(0,0,0,0.4); box-shadow:0 0 18px rgba(0,0,0,0.4); margin-top:20px; color:#FF0A0A;">
<form id="form1" name="abc" method="post" action="addmem.php" onsubmit="return validateForm()">
<div style="background-color:#cccccc; font-family:Arial, Helvetica, sans-serif; color:#000000; padding:5px; height:22px; width:390px;">
<div style="float:left;"><strong>Members Registration</strong></div>
<div style="float:right; margin-right:3px; background-color:#cccccc; width:25px; text-align:center; height:22px;"><a href="loginuser.php">X</a></div>
</div>
<table width="368" align="center">
<tr>
<td colspan="2"><div style="font-family:Arial, Helvetica, sans-serif; font-size:14px;"><font color="white">All Field Mark with asterisk (<span class="style1"><b>*</b></span>) must be filled up</div></td>
</tr>
<tr>
<td colspan="2"><div style="font-family:Arial, Helvetica, sans-serif; color:#FF0000; font-size:12px;"><?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?></div></td>
</tr>
<tr>
<td width="120" valign="top"><div align="right"> <font color="#00EBFF">Firstname: </div></td>
<td width="236"><input type="text" name="firstname">
<span class="style2">*</span></td>
</tr>
<tr>
<td valign="top"><div align="right"><font color="#00EBFF">Lastname:</div> </td>
<td><input type="text" name="lastname">
<span class="style2">*</span></td>
</tr>
<tr>
<td valign="top"><div align="right"><font color="#00EBFF">Email:</div></td>
<td><input type="text" name="email">
<span class="style2">*</span></td>
</tr>
<tr>
<td valign="top"><div align="right"><font color="#00EBFF">Password:</div> </td>
<td><input type="password" name="pword">
<span class="style2">*</span></td>
</tr>
<tr>
<td valign="top"><div align="right"><font color="#00EBFF">Retype Password:</div></td>
<td><input type="password" name="ambot">
<span class="style2">*</span></td>
</tr>
<tr>
<td valign="top"><div align="right"><font color="#00EBFF">Contact Number: </div></td>
<td><input name="number" type="number" min="0" id="contact" size="11">
<span style="font-family:Arial, Helvetica, sans-serif; font- size:12px; color:#FF0000; font-weight:bold;" id="errmsg"></span><span class="style2">* </span></td>
</tr>
<tr>
<td valign="top"><div align="right"><font color="#00EBFF">Brgy. and House: </div></td>
<td><input type="text" name="house">
<span class="style2">*</span></td>
</tr>
<tr>
<td valign="top"><div align="right"><font color="#00EBFF">Street No:</div> </td>
<td><input type="text" name="street">
<span class="style2">*</span></td>
</tr>
<tr>
<td valign="top"><div align="right"><font color="#00EBFF">City:</div></td>
<td><select name="city"><option value="0">--Select One--</option>
<option value="Caloocan City">Caloocan City</option>
<option value="Las Pinas City">Las Pinas City</option>
<option value="Makati City">Makati City</option>
<option value="Malabon City">Malabon City</option>
<option value="Mandaluyong City">Mandaluyong City</option>
<option value="Manila">Manila</option>
<option value="Marikina City">Marikina City</option>
<option value="Muntinlupa City">Muntinlupa City</option>
<option value="Navotas">Navotas</option>
<option value="Paranaque City">Paranaque City</option>
<option value="Pasay City">Pasay City</option>
<option value="Pasig City">Pasig City</option>
<option value="Pateros">Pateros</option>
<option value="Quezon City">Quezon City</option>
<option value="San Juan">San Juan</option>
<option value="Taguig">Taguig</option>
<option value="Valenzuela City">Valenzuela City</option></select>
<span class="style2">*</span></td>
</tr>
<tr>
<td valign="top"> </td>
<td><input type="submit" value="Save"></td>
</tr>
</table>
</form>
</div>
<hr>
<div class="footer">
<p>Copyright © Athan Motorcycle</p>
</div>
</body>
</html>
这是我的PHP代码,我把错误放在:
<?php
session_start();
$errmsg_arr = array();
$errflag = false;
include('config.php');
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$pword=$_POST['pword'];
$number=$_POST['number'];
$house=$_POST['house'];
$street=$_POST['street'];
$city=$_POST['city'];
$min_length = 6;
// you can set minimum length of the query if you want
if(strlen($pword) >= $min_length){
//this one will not feed in the database if there's a duplicate but still a problem ohmaygawd:3
//mysql_query("INSERT INTO athan_members (firstname, lastname, email, number, house1, street1, city, password) VALUES ('$firstname', '$lastname', '$email', '$number', '$house', '$street', '$city', '$pword') ON DUPLICATE KEY UPDATE")
mysql_query("INSERT INTO athan_members (firstname, lastname, email, number, house1, street1, city, password) VALUES ('$firstname', '$lastname', '$email', '$number', '$house', '$street', '$city', '$pword')");
header("location: loginuser.php");
}
else
{
$errmsg_arr[] = 'password must contain not less than 6 characters';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: new.php");
exit();
}
mysql_close($con);
?>
答案 0 :(得分:1)
您可以添加以下代码,以检查具有给定电子邮件的用户是否已存在:
$result = mysql_query("select 1 from athan_members where email='"
. mysql_escape_string($email) . "'");
$userExists = (mysql_fetch_array($result) !== FALSE);
mysql_free_result($result);
之后,您可以使用$ userExists中存储的值来执行错误检查逻辑。
但是,正如其他人所说,你不应该使用mysql_函数来处理数据库。
答案 1 :(得分:0)
在数据库中查询输入的电子邮件。如果你有0行,那么它就不存在了。