只需尝试以下方法。,
HTML部分:
<form action="" method="post" name="register">
<table>
<tr><td>First Name</td><td><input type="text" name="first_name" /></td></tr>
<tr><td>Last Name</td><td><input type="text" name="last_name" /></td></tr>
<tr><td>Address</td><td><input type="text" name="address" /></td></tr>
<tr><td>Postal Code</td><td><input type="text" name="postcode" /></td></tr>
<tr><td>Email</td><td><input type="text" name="emailaddress" /></td></tr>
<tr><td>Password</td><td><input type="password" name="password" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="register" value="Register" /></td></tr>
</table>
</form>
PHP部分:
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
$select_db = mysql_select_db("mayan",$con);
if(isset($_POST['register']))
{
$firstname = mysql_real_escape_string($_POST['first_name']);
$lastname = mysql_real_escape_string($_POST['last_name']);
$address = mysql_real_escape_string($_POST['address']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$emailaddress = mysql_real_escape_string($_POST['emailaddress']);
$password = mysql_real_escape_string($_POST['password']);
$query = "select emailaddress FROM mayan_users where emailaddress='$emailaddress'";
$link = mysql_query($query)or die(mysql_error());
$num = mysql_num_rows($link);
if ($num>0){
echo 'Email already exists'; //email already taken
}
else {
$insert_query = "insert into `mayan_users`(`firstname`,`lastname`,`address`,`postcode`,`emailaddress`,`password`) values('$firstname','$lastname','$address','$postcode','$emailaddress','$password')";
$result = mysql_query($insert_query)or die(mysql_error());
echo "Registered Successfully!";
}
}
?>
MySQL表结构:
CREATE TABLE IF NOT EXISTS `mayan_users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`postcode` varchar(255) NOT NULL,
`emailaddress` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
我认为这可以帮助您解决问题。