我在不同的项目上有类似的代码,但是我想添加一个字段以允许用户注册他们的代码以及姓名和电子邮件地址。但是脚本不断返回:
警告:mysqli_num_rows()要求参数1为mysqli_result,第61行/Applications/XAMPP/xamppfiles/htdocs/finalyear/group/public_html/register.php中给出布尔值
以下是此脚本的代码:
<?php
//TITLE AND INCLUDES
$page_title = 'Register';
include ('includes/header.html');
//CONDITIONAL TEST THAT WILL BE EXECUTED IF FORM SUBMITTED
if($_SERVER['REQUEST_METHOD']=='POST'){
require('../connect.php');
$errors = array();
//STORE ERROR MESSAGE IF b_code FIELD IS EMPTY
if(empty($_POST['b_code'])){
$errors[] = 'Enter your B-Code:';
}
else{
$uc = mysqli_real_escape_string($dbc,trim($_POST['b_code']));
}
//STORE ERROR MESSAGE IF first_name FIELD IS EMPTY
if(empty($_POST['first_name'])){
$errors[] = 'Enter your first name:';
}
else{
$fn = mysqli_real_escape_string($dbc,trim($_POST['first_name']));
}
//STORE ERROR MESSAGE IF last_name FIELD IS EMPTY
if(empty($_POST['last_name'])){
$errors[] = 'Enter your last name:';
}
else{
$ln = mysqli_real_escape_string($dbc,trim($_POST['last_name']));
}
//STORE ERROR MESSAGE IF email FIELD IS EMPTY
if(empty($_POST['email'])){
$errors[] = 'Enter your email address:';
}
else{
$e = mysqli_real_escape_string($dbc,trim($_POST['email']));
}
//STORE PASSWORD VALUE IN VAR OR ERROR MESSAGE IF FIELDS DO NOT MATCH/ARE EMPTY
if(!empty($_POST['pass1'])){
if($_POST['pass1'] != $_POST['pass2']){
$errors[] = 'Passwords do not match:';
}
else{
$p = mysqli_real_escape_string($dbc,trim($_POST['pass1']));
}
}
else{
$errors[] = 'Enter your password:';
}
//STORE ERROR MESSAGE IF EMAIL ADDRESS IS ALREADY REGISTERED
if(empty($errors)){
$q = "SELECT id FROM users WHERE email = '$e'";
$r = mysqli_query($dbc,$q);
//var_dump(mysqli_error($dbc));
if(mysqli_num_rows($r) != 0){
$errors[] = 'Email address already registered:<a href = "login.php">Login</a>';
}
}
//ERROR MESSAGE LOGGING FINISHED
//STATEMENTS TO STORE USER DATA IN DATABASE TABLE, DISP CONF MESSAGE IF SUCCESSFUL && CLOSE CONNECTION/FINISH SCRIPT
if(empty($errors)){
$q = "INSERT INTO users (b_code,first_name,last_name,email,password,reg_date) VALUES ('$uc',$fn','$ln','$e',SHA1('$p'),NOW())";
$r = mysqli_query($dbc,$q);
if($r){
echo '<div id = "content">
<h1>Registered!</h1>
<p>You are now registered.</p>
<p><a href = "login.php">Login</a></p>
</div>';
}
mysqli_close($dbc);
exit();
}
else{
echo '<h1>Error!</h1>
<p id = "err_msg">The following error(s) occurred:<br/>';
foreach($errors as $msg){
echo "-$msg<br/>";
}
echo 'Please try again.</p>';
echo '</div>';
mysqli_close($dbc);
}
}
?>
<div id = "content">
<h1>Register</h1>
<form action = "register.php" method = "POST">
<p>B-Code:<input type = "text" name = "b_code" value = "<?php if (isset($_POST['b_code'])) echo $_POST['b_code'];?>"</p>
<p>First Name:<input type = "text" name = "first_name" value = "<?php if (isset($_POST['first_name'])) echo $_POST['first_name'];?>"</p>
<p>Last Name:<input type = "text" name = "last_name" value = "<?php if (isset($_POST['last_name'])) echo $_POST['last_name'];?>"</p>
<p>Email Address:<input type = "text" name = "email" value = "<?php if (isset($_POST['email'])) echo $_POST['email'];?>"</p>
<p>Password: <input type = "password" name = "pass1" value = "<?php if (isset($_POST['pass1'])) echo $_POST['pass1'];?>"</p>
<p>Confirm Password: <input type = "password" name = "pass2" value = "<?php if (isset($_POST['pass2'])) echo $_POST['pass2'];?>"</p>
<p><input type = "submit" value = "register"></p>
</form>
</div>
<?php
include('includes/footer.html');
?>