Hello Forum Members:
Could I get some help where the problem might be? I have my script partially working but I can't register the user to the DB. It shows correct connectivity but I can't save the users, all I got is the error message saying duplicate. I tried to compile a picture to show that the script works for a couple of functions but when it comes to save the user to the DB, it gives me an error. Any suggestion accepted.
My code :
<?php # Script 8.3 - register.php #2
$page_title = 'Register';
include ('includes/header.html');
// Check if the form has been submitted:
if (isset($_POST['submitted'])) {
//Open the DB
require_once('../mysqli_connect3.php');
$errors = array();
// Initialize an error array.
// Check for a first name:
if (empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name.';}
else {$fn = trim($_POST['first_name']);
}
// Check for a last name:
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';}
else {$ln = trim($_POST['last_name']);
}
// Check for an email address:
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';}
else {$e = trim($_POST['email']);
}
// Check for a password and match against the confirmed password:
if (!empty($_POST['pass1'])) {
if ($_POST['pass1'] != $_POST['pass2']) {
$errors[] = 'Your password did not match the confirmed password.';}
else {
$p = trim($_POST['pass1']);}
}
else {$errors[] = 'You forgot to enter your password.';}
if (empty($errors)) { // If everything's OK.
// Register the user in the database...
require_once ('../mysqli_connect3.php'); //connect to the db.
// Make the query:
$q = "INSERT INTO users (first_name, last_name, email, pass)
VALUES ('$fn', '$ln', '$e', SHA1('$p') )";
//$r = @mysqli_query ($dbc, $q);
//As per forum suggestion
//this will output any query string error syntax.
$r = @mysqli_query ($dbc, $q) or die("Error: " . mysqli_error($dbc) );
//$r = @mysqli_query ($dbc, $q);
//Check if the query ran. Does $r have a TRUE value?
if ($r)
{ //Print a thankyou message to the user
echo '<h1>Thank you</h1>
<p>You are now registered. In Chapter 11 you will actually be
able to log in!</p><p><br /></p>';
}
else { //If the query didn't run OK.
//A message to the user
echo '<h1>Oops, system error.</h1>
<p class="error">You might not be registered because of a system error. Sorry.</p>';
//A message to the programmer
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
}
//Close the Db connection
mysqli_close($dbc);
//include the footer and exit the script
include ('includes/footer.html');
exit();
}
else{ //Report the input errors to the user
echo '<h1>Errors</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg)
{
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
}
//Close the Db connection
mysqli_close($dbc);
}
?>
<h1>Register</h1>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="20"
value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="40"
value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
<p>Email Address: <input type="text" name="email" size="20" maxlength="80"
value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>
<p>Password: <input type="password" name="pass1" size="10" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" class="button_style"/></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('includes/footer.html');
?>
<?php #Script - mysqli_connect.php
//This file contains the database access information
//This file also establishes a connection to MySQL
//and selects the Database.
//Set the database access information as constants:
DEFINE ('DB_USER','XXXX');
DEFINE ('DB_PASSWORD' , 'XXXX');
DEFINE ('DB_HOST' , 'XXXX.1and1.com');
DEFINE ('DB_NAME', 'XXXX');
//Make the connection:
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysqli_connect_error();
?>