我正在尝试将数据插入数据库,但它给了我这个错误:
Sorry, your registration failed. Please go back and try again.
请帮忙,我的数据库连接成功,可以看到,这是我创建它的查询..
create database if not exists raj;
use raj;
CREATE TABLE IF NOT EXISTS `raj`.`users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'auto incrementing user_id of each user, unique index',
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s name, unique',
`user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s password in salted and hashed format',
`user_email` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s email, unique',
`user_college` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s college, unique',
`user_branch` varchar(50),
`user_year` varchar(50),
`user_firstname` varchar(50),
`user_lastname` varchar(50),
`user_gender` varchar(50),
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';
这是我创建的注册表:
<?php
// show potential errors / feedback (from registration object)
if (isset($registration)) {
if ($registration->errors) {
foreach ($registration->errors as $error) {
echo $error;
}
}
if ($registration->messages) {
foreach ($registration->messages as $message) {
echo $message;
}
}
}
?>
<!-- register form -->
<form method="post" action="register.php" name="registerform">
<!-- the user name input field uses a HTML5 pattern check -->
<label for="login_input_username">Username (only letters and numbers, 2 to 64 characters)</label>
<input id="login_input_username" class="login_input" type="text" pattern="[a-zA-Z0-9]{2,64}" name="user_name" required />
<!-- the email input field uses a HTML5 email type check -->
<label for="login_input_email">User's email</label>
<input id="login_input_email" class="login_input" type="email" name="user_email" required />
<label for="login_input_password_new">Password (min. 6 characters)</label>
<input id="login_input_password_new" class="login_input" type="password" name="user_password_new" pattern=".{6,}" required autocomplete="off" />
<label for="login_input_password_repeat">Repeat password</label>
<input id="login_input_password_repeat" class="login_input" type="password" name="user_password_repeat" pattern=".{6,}" required autocomplete="off" />
<div id="select_option_colg_pos">
<select id="selectbasic" name="userCollege" class="input-large">
<option>Select your college</option>
<option>MAIIT kota-rajasthan</option>
</select>
<select id="selectbasic" name="userBranch" class="input-large">
<option>Select your Branch</option>
<option>Computer science</option>
<option>Civil</option>
<option>Mechanical</option>
<option>Electrical</option>
</select>
<select id="selectbasic" name="userYear" class="input-medium">
<option>Year</option>
<option>1st year</option>
<option>2nd year</option>
<option>3rd year</option>
<option>4th year</option>
<option>Year completed</option>
</select>
</div><br>
<input id="user_name" type="text" name="userFirstName" placeholder="First name" required="" />
<input id="user_name2" type="text" name="userLastName" placeholder="Last name" required="" /><br><br>
<select id="selectbasic" style="margin-left: 5%;" name="userGender" class="input-medium">
<option>Select gender</option>
<option>Female</option>
<option>Male</option>
</select>
<input type="submit" name="register" value="Register" />
</form>
<!-- backlink -->
<a href="index.php">Back to Login Page</a>
然后你去注册过程(我的意思是将行插入数据库):
<?php
/**
* Class registration
* handles the user registration
*/
class Registration
{
/**
* @var object $db_connection The database connection
*/
private $db_connection = null;
/**
* @var array $errors Collection of error messages
*/
public $errors = array();
/**
* @var array $messages Collection of success / neutral messages
*/
public $messages = array();
/**
* the function "__construct()" automatically starts whenever an object of this class is created,
* you know, when you do "$registration = new Registration();"
*/
public function __construct()
{
if (isset($_POST["register"])) {
$this->registerNewUser();
}
}
/**
* handles the entire registration process. checks all error possibilities
* and creates a new user in the database if everything is fine
*/
private function registerNewUser()
{
if (empty($_POST['user_name'])) {
$this->errors[] = "Empty Username";
} elseif (empty($_POST['user_password_new']) || empty($_POST['user_password_repeat'])) {
$this->errors[] = "Empty Password";
} elseif ($_POST['user_password_new'] !== $_POST['user_password_repeat']) {
$this->errors[] = "Password and password repeat are not the same";
} elseif (strlen($_POST['user_password_new']) < 6) {
$this->errors[] = "Password has a minimum length of 6 characters";
} elseif (strlen($_POST['user_name']) > 64 || strlen($_POST['user_name']) < 2) {
$this->errors[] = "Username cannot be shorter than 2 or longer than 64 characters";
} elseif (!preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name'])) {
$this->errors[] = "Username does not fit the name scheme: only a-Z and numbers are allowed, 2 to 64 characters";
} elseif (empty($_POST['user_email'])) {
$this->errors[] = "Email cannot be empty";
} elseif (strlen($_POST['user_email']) > 64) {
$this->errors[] = "Email cannot be longer than 64 characters";
} elseif (!filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) {
$this->errors[] = "Your email address is not in a valid email format";
} elseif (!empty($_POST['user_name'])
&& strlen($_POST['user_name']) <= 64
&& strlen($_POST['user_name']) >= 2
&& preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name'])
&& !empty($_POST['user_email'])
&& strlen($_POST['user_email']) <= 64
&& filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)
&& !empty($_POST['user_password_new'])
&& !empty($_POST['user_password_repeat'])
&& ($_POST['user_password_new'] === $_POST['user_password_repeat'])
) {
// create a database connection
$this->db_connection = new mysqli("localhost", "root", "1234", "raj", 3306);
// change character set to utf8 and check it
if (!$this->db_connection->set_charset("utf8")) {
$this->errors[] = $this->db_connection->error;
}
// if no connection errors (= working database connection)
if (!$this->db_connection->connect_errno) {
// escaping, additionally removing everything that could be (html/javascript-) code
$user_name = $this->db_connection->real_escape_string(strip_tags($_POST['user_name'], ENT_QUOTES));
$user_email = $this->db_connection->real_escape_string(strip_tags($_POST['user_email'], ENT_QUOTES));
$user_college= $this->db_connection->real_escape_string(strip_tags($_POST['userCollege']));
$user_branch= $this->db_connection->real_escape_string(strip_tags($_POST['userBranch']));
$user_year= $this->db_connection->real_escape_string(strip_tags($_POST['userYear']));
$user_firstname= $this->db_connection->real_escape_string(strip_tags($_POST['userFirstName']));
$user_lastname= $this->db_connection->real_escape_string(strip_tags($_POST['userLastName']));
$user_gender= $this->db_connection->real_escape_string(strip_tags($_POST['userGender']));
$user_password = $_POST['user_password_new'];
// crypt the user's password with PHP 5.5's password_hash() function, results in a 60 character
// hash string. the PASSWORD_DEFAULT constant is defined by the PHP 5.5, or if you are using
// PHP 5.3/5.4, by the password hashing compatibility library
$user_password_hash = password_hash($user_password, PASSWORD_DEFAULT);
// check if user or email address already exists
$sql = "SELECT * FROM users WHERE user_name = '" . $user_name . "' OR user_email = '" . $user_email . "';";
$query_check_user_name = $this->db_connection->query($sql);
if ($query_check_user_name->num_rows == 1) {
$this->errors[] = "Sorry, that username / email address is already taken.";
} else {
// write new user's data into database
$sql = "INSERT INTO users (user_name, user_password_hash, user_email, user_college, user_branch, user_year, user_firstname, user_lastname, user_gender)
VALUES('" . $user_name . "', '" . $user_password_hash . "', '" . $user_email . "', '". $user_college ."', '". $user_branch ."', '". $user_year ."', '". $user_firstname ."', '". $user_lastname ."', '". $user_gender ."');";
$query_new_user_insert = $this->db_connection->query($sql);
// if user has been added successfully
if ($query_new_user_insert) {
$this->messages[] = "Your account has been created successfully. You can now log in.";
} else {
$this->errors[] = "Sorry, your registration failed. Please go back and try again.";
}
}
} else {
$this->errors[] = "Sorry, no database connection.";
}
} else {
$this->errors[] = "An unknown error occurred.";
}
}
}