Hi, I'm quite new to PHP and MYSQL, but I have chosen to do an A-level project in it. This is just a basic registering page, which I've copied and modified from one out of a book. That one works, but this doesn't, saying that "mysql_numrows(): supplied argument is not a valid MySQL result resource". I hope that makes sense to someone.
<?php #Script 8.7 - register.php
//SEND NOTHING TO BROWSER BEFORE HEADER()
//Check if form is sumbitted
if (isset($_POST['submitted'])) {
require_once('../mysql_pp_connect.php');
$errors = array();
//Validate crap
if (empty($_POST['title'])) {
$errors[] = 'You forgot to enter your first name.';
} else {
$t = escape_data($_POST['title']);
}
if (empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name.';
} else {
$fn = escape_data($_POST['first_name']);
}
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$ln = escape_data($_POST['last_name']);
}
if (empty($_POST['addressline1'])) {
$errors[] = 'You forgot to enter your address.';
} else {
$al1 = escape_data($_POST['addressline1']);
}
$al2 = escape_data($_POST['addressline2']);
$al3 = escape_data($_POST['addressline3']);
if (empty($_POST['postcode'])) {
$errors[] = 'You forgot to enter your postcode.';
} else {
$pc = escape_data($_POST['postcode']);
}
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = escape_data($_POST['email']);
}
if (empty($_POST['phone_number'])) {
$errors[] = 'You forgot to enter your phone number.';
} else {
$pn = escape_data($_POST['phone_number']);
}
if(!empty($_POST['password1'])) {
if($_POST['password1'] != $_POST['password2']) {
$errors[] = "Your passwords didn't match";
} else {
$p = escape_data($_POST['password1']);
}
} else {
$errors[] = "You didn't enter a password!";
}
if (empty($errors)) {
$query = "SELECT user_id FROM users WHERE email='$e'";
$result = mysql_query ($query);
if (mysql_num_rows($result) == 0) {
//Make query
$query = "INSERT INTO customers (Title, FirstName, LastName, AddressLine1, AddressLine2, Addressline3, Postcode, EmailAddress, PhoneNumber, Password) VALUES ('$t', '$fn', '$ln', '$al1', '$al2', '$al3', '$pc', '$e', '$pn', SHA('$p'))";
$result = @mysql_query ($query); //Run query
if ($result) {
//send email blah blah
//redirect to thanks.php page
//start defining url
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')) {
$url = substr($url, 0, -1); //chop off slashah.
}
$url .= '/thanks.php';
header("Location: $url");
exit();
} else {
$errors[] = 'You could not be registered due to a system error. oops. sorry!';
$errors[] = mysql_error() . '<p>Query ' . $query;
}
} else {
$errors = 'The email address has already been registered!';
}
}
mysql_close();
}else{
$errors = NULL;
} //end of submit conditional
$page_title = 'Register';
include ('./header.html');
if(!empty($errors)){
echo '<h1 id="mainhead">Error!</h1><p class="error">The following errors occurred:<br><ul>';
foreach ($errors as $msg) {
echo "<li>$msg</li><br>";
}
echo '</ul><p>Please try again';
} //end of if empty errrors if
?>
Cheers