这是完整的错误
警告:mysql_num_rows()要求参数1为资源,布局在第35行的C:\ xampp \ Installations \ htdocs \ finished \ register.php中给出 用户名已存在查询为空
这是我的代码:
<?php
//Checks to make sure the submit button is pressed
if (isset($_POST['submit'])){
$errors = array();
$valid = true;
//Checks to make sure the password meets the rules of validation
if(isset($_POST['Password'])) {
if(!preg_match('/^(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/',$_POST['Password'])) {
$errors ['Password'] = "Your password must have the following criteria - contain at least (1) upper case letter - contain at least (1) lower case letter - contain at least (1) number or special character - contain at least (8) characters in length";
echo $errors ['Password'];
$valid = false;
}
}
//Checks to make sure the email meets the rules of validation
if(isset($_POST['Email'])) {
if(!preg_match('/^([a-zA-Z0-9._-])+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$/',$_POST['Email'])) {
$errors['Email'] = "Must be valid email address";
echo $errors['Email'];
$valid = false;
}
}
if(empty($errors)) {
//This creates the connection to the database
$con = mysql_connect("localhost", "Peter", "password");
if(!$con) {
die("Can not connect:" . mysql_error());
}
//Selects the database I wish to add the table into
mysql_select_db("deepseadiving",$con);
//Passes the entered information into the table called user
$mysql=mysql_query("SELECT FROM User (Username) WHERE Username = '$_POST[Username]'");
if(mysql_num_rows($mysql)>=0)
{
echo"Username already exists";
}
else
{
$sql = "INSERT INTO User (Username,Password,FirstName,LastName,Email,Address,ContactNumber) VALUES('$_POST[Username]','$_POST[Password]','$_POST[FirstName]','$_POST[LastName]','$_POST[Email]','$_POST[Address]','$_POST[ContactNumber]')";
header("location:register.php");
}
mysql_query($mysql,$con) or die(mysql_error($con));
mysql_close($con);
}
}
?>
答案 0 :(得分:1)
错误是
你的$ mysql在数据库中放入错误的查询会发生什么事情,所以它返回FALSE 没有错,那么mysql_query会将资源返回到表...
$mysql=mysql_query("SELECT Username FROM user WHERE Username='".$_POST['Username']."');
或者例如
$mysql=mysql_query("SELECT * FROM table_name WHERE Username='".$_POST['Username']."');
您的插入查询也是错误的。
$sql = "INSERT INTO User (Username,Password,FirstName,LastName,Email,Address,ContactNumber) VALUES('".$_POST['Username']."','".$_POST['Password']."','".$_POST['FirstName']."','".$_POST['LastName']."','".$_POST['Email']."','".$_POST['Address']."','".$_POST['ContactNumber']."')";