Connecting PHP to MySQL Problems

August 12, 2011 at 22:31:19
Specs: Windows 7
So I'm a beginner with PHP and MySQL and am learning the two programs. In my textbook I am at the point at connecting PHP and MySQL and I am having trouble. What I'm doing is creating a registration form, that saves the information to my database. Here's the code:
<?php # Script 8.3 - register.php

$page_title = 'Register';
include ('includes/header.html');

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

	$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 ('includes/mysqli_connect.php'); // Connect to the db.
		// Make the query:
		$q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";		
		$r = @mysqli_query ($dbc, $q); // Run the query.
		if ($r) { // If it ran OK.
			// Print a message:
			echo '<h1>Thank you!</h1>

You are now registered. In Chapter 11 you will actually be able to log in!</p>

<br /></p>';	
		} else { // If it did not run OK.
			// Public message:
			echo '<h1>System Error</h1>
			<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; 
			// Debugging message:
			echo '

' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
		} // End of if ($r) IF.
		mysqli_close($dbc); // Close the database connection.
		// Include the footer and quit the script:
		include ('includes/footer.html'); 
	} else { // Report the errors.
		echo '<h1>Error!</h1>
		<p class="error">The following error(s) occurred:<br />';
		foreach ($errors as $msg) { // Print each error.
			echo " - $msg<br />\n";
		echo '</p>

Please try again.</p>

<br /></p>';
	} // End of if (empty($errors)) IF.

} // End of the main Submit conditional.
<form action="register.php" method="post">

First Name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></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>

Email Address: <input type="text" name="email" size="20" maxlength="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>"  /> </p>

Password: <input type="password" name="pass1" size="10" maxlength="20" /></p>

Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /></p>

<input type="submit" name="submit" value="Register" /></p>
	<input type="hidden" name="submitted" value="TRUE" />
include ('includes/footer.html');

the code for the mysqli_connect.php file is:
<?php # Script 8.2 - 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', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'sitename');

// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );


What happens after I click submit on the page is a blank screen comes up, but the header. What's supposed to happen is there should be a thank you page, but nothing comes up except for the website's header. I also looked at the database's user table and there are no new inserted information. Any help?
You can view the page here:

See More: Connecting PHP to MySQL Problems

Report •

August 14, 2011 at 14:05:32
If you're having issues, It may be better not to supress the messages of the sql functions and see what is happening - maybe you're getting a syntax error. Another thing may be to use __LINE__ in conjunction with your die message.

You might also try turning on all errors.


Report •
Related Solutions

Ask Question