Jump to content

Inserting username/password


myotch

Recommended Posts

Took Skoglund's Lynda course on creating a CMS for a project I am doing for a customer.

 

I was extremely careful to set up the database, and pretty much copying everything he was doing in the course.

 

So, I try to create a user in the database, and....crickets.  Name and password don't come up.  No error messages from the site.  Just, nothing.  So I'm sure it's pretty simple to fix, otherwise, there would have been chaos, right?

 

For the page: hawcreekrenovations.com/new_user.php

 

We know the page is correctly connecting to the MySQL because the drop down in the form is populated by the user-types in the database.

 

So, I guess, you need the pertinent code:

 

// START FORM PROCESSING
if (isset($_POST['submit'])) { //Form has been submitted
	$errors = array();

	// perform validations on the form data
	$required_fields = array('username', 'password', 'user_type_id' );
	$errors = array_merge($errors, check_required_fields($fields_with_lengths, $_POST));

	$fields_with_lengths = array('username' => 30, 'password'=> 30);
	$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

	$username = trim(mysql_prep($_POST['username']));
	$password = trim(mysql_prep($_POST['password']));
	$hashed_password = sha1($password);
	$user_type_id = trim(mysql_prep($_POST['user_type_id']));

	if ( empty($errors) ) {
		$query = "INSERT INTO users (
				username, hashed_password, user_type_id
			) VALUES (
				'{$username}', '{$hashed_password}', '{$user_type_id}'
			)";
		$result = mysql_query($query, $connection);
		if ($result) {
			$message = "The user was successfully created.";
		} else {
			$message = "The user could not be created.";
			$message .= "<br />" . mysql_error();
		}
	} else {
			if (count($errors) == 1) {
				$message = "There was 1 error in the form.";
			} else {
				$message = "There were " . count($errors) . " erros in the form.";
			}
	}
} else { // Form has not been submitted.

	$username = "";
	$password = "";
}
?>

 

And the form:

 

<form action="new_user.php" method "post">
            <table>
            	<tr>
                	<td>Username:</td>
                    <td><input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /></td>
                </tr>
                <tr>
                	<td>Password:</td>
                    <td><input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" ? /></td>
                </tr>
                <tr>
                	<td>User Type:</td>
                    <td><?php // 3. Perform database query 	
                $result = mysql_query("SELECT * FROM user_type", $connection);
                    if (!$result) { 		
                        die("Database query failed: " . mysql_error());
                    }
                // 4. Use returned data (if any) 	
                echo "<select name='type'>";
                while ($row = mysql_fetch_array($result)) {
                        echo "<option value=\"".$row["user_type_id"]."\">".$row["type"] . "</option>";
                }
                echo "</select>"; ?></td>
                </tr>
                <tr>
                	<td colspan="2"><input type="submit" name="submit" value="Create user" /></td>
                </tr>
            </table>
            </form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.