MySQL 更新到 SQLi 后注册不起作用?
MySQL Registration not working when updated to SQLi?
所以我确实对MySQL和MySQLi之间的区别了解不多,我会在工作的时候慢慢地把它捡起来……我正在努力在这个新网站上使用我的旧 SQL PHP 注册表格但没有任何运气,我要么得到 "mysqli_query() expects at least 2 parameters, 1 given"
或空白页面,要么带回网站错误,例如 "Desired Username is a required field. Please enter it below."
该字段何时填写?如果有人能看一看,我将不胜感激!谢谢。
Register.php -
<?php
if (isset($_GET['signuperror'])) {
$signuperror = $_GET['signuperror'];
} else {//if there is no choice, display series 1
$signuperror = 1;
}
switch($signuperror) {
case 1:
echo '';
break;
case 2:
echo "<div class='alert alert-danger'><strong>First Name is a required field. Please enter it below.</strong></span></div> ";
break;
case 3:
echo "<div class='alert alert-danger'><strong>Last Name is a required field. Please enter it below.</strong></span></div> ";
break;
case 4:
echo "<div class='alert alert-danger'><strong>Email Address is a required field. Please enter it below.</strong></span></div> ";
break;
case 5:
echo "<div class='alert alert-danger'><strong>Desired Username is a required field. Please enter it below.</strong></span></div> ";
break;
case 6:
echo "<div class='alert alert-danger'><strong>Your passwords do NOT match, please check your passwords.</strong></span></div> ";
break;
case 7:
echo "<div class='alert alert-danger'><strong>Your email address has already been used by another user. Please use a different Email address.</strong></span></div> ";
break;
case 8:
echo "<div class='alert alert-danger'><strong>The username you have selected has already been used by another user. Please choose a different Username.</strong></span></div> ";
break;
case 9:
echo "<div class='alert alert-danger'><strong>There has been an error creating your account. Please contact the admin.</strong></span></div> ";
break;
default:
break;
}
?>
<form name="register" id="register" action="registercode.php" method="POST">
<div class="register-top-grid">
<h3>PERSONAL INFORMATION</h3>
<div class="wow fadeInLeft" data-wow-delay="0.4s">
<span><b>First Name</b><label>*</label></span>
<input type="text" value="" data-msg-required="Please enter your first name." maxlength="100" name="first_name" id="first_name" required>
</div>
<div class="wow fadeInRight" data-wow-delay="0.4s">
<span><b>Last Name</b><label>*</label></span>
<input type="text" value="" data-msg-required="Please enter your last name." maxlength="100" name="last_name" id="last_name" required>
</div>
<div class="wow fadeInRight" data-wow-delay="0.4s">
<span><b>Email Address</b><label>*</label></span>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." size="68" name="email_address" id="email" required>
</div>
<div class="clearfix"> </div>
<a class="news-letter" href="#">
<label class="checkbox"><input type="checkbox" name="checkbox" checked=""><i> </i>Sign Up for Newsletter</label>
</a>
</div>
<div class="register-bottom-grid">
<h3>LOGIN INFORMATION</h3>
<div class="wow fadeInLeft" data-wow-delay="0.4s">
<span><b>Username</b><label>*</label></span>
<input type="text" value="" data-msg-required="Please enter a valid username." maxlength="100" name="username" id="username" required>
</div><div class="clearfix"> </div><div class="clearfix"> </div><div class="clearfix"> </div><div class="clearfix"> </div>
<div class="wow fadeInLeft" data-wow-delay="0.4s">
<span><b>Password</b><label>*</label></span>
<input type="password" value="" name="password" id="password" required>
</div>
<div class="wow fadeInRight" data-wow-delay="0.4s">
<span><b>Confirm Password</b><label>*</label></span>
<input type="password" value="" name="passwordcheck" id="passwordcheck" required>
</div>
</div>
<div class="clearfix"> </div>
<div class="register-but">
<input type="submit" value="REGISTER" class="btn btn-info"> | Already a member? <a href="signin.php"><b>Login</b></a>
<div class="clearfix"> </div>
</form>
</div>
</div>
</div>
</div>
<!-- registration -->
registercode.php -
<?
include 'core/init.php';
// Define post fields into simple variables
$first_name = mysqli_real_escape_string($_POST['first_name']);
$last_name = mysqli_real_escape_string($_POST['last_name']);
$email_address = mysqli_real_escape_string($_POST['email_address']);
$username = mysqli_real_escape_string($_POST['username']);
$password = $_POST['password'];
$passwordcheck = $_POST['passwordcheck'];
/* Let's strip some slashes in case the user entered
any escaped characters. */
$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
/* Do some error checking on the form posted fields */
if((!$first_name) || (!$last_name) || (!$email_address) || (!$username) || (!$password) || (!$passwordcheck)){
if(!$first_name){
header('Location: register.php?signuperror=2');
}
if(!$last_name){
header('Location: register.php?signuperror=3');
}
if(!$email_address){
header('Location: register.php?signuperror=4');
}
if(!$username){
header('Location: register.php?signuperror=5');
}
if ($password !== $passwordcheck){
header('Location: register.php?signuperror=6');
}
include "register.php"; // Show the form again!
/* End the error checking and if everything is ok, we'll move on to
creating the user account */
exit(); // if the error checking has failed, we'll exit the script!
}
/* Let's do some checking and ensure that the user's email address or username
does not exist in the database */
$sql_email_check = mysqli_query($conn, "SELECT email_address FROM users WHERE email_address='$email_address'");
$sql_username_check = mysqli_query($conn, "SELECT username FROM users WHERE username='$username'");
$email_check = mysqli_num_rows($sql_email_check);
$username_check = mysqli_num_rows($sql_username_check);
if(($email_check > 0) || ($username_check > 0)){
if($email_check > 0){
header('Location: register.php?signuperror=7');
unset($email_address);
}
if($username_check > 0){
header('Location: register.php?signuperror=8');
unset($username);
}
include 'register.php'; // Show the form again!
exit(); // exit the script so that we do not create this account!
}
/* Everything has passed both error checks that we have done.
It's time to create the account! */
$db_password = md5($password);
// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysqli_query($conn, "INSERT INTO users (email_address, username, first_name, last_name, password, reg_date, last_login, activated, admin)
VALUES('$email_address', '$username', '$first_name', '$last_name', '$db_password', now(), now(), '0', '0')") or die (mysqli_error());
if(!$sql){
header('Location: register.php?signuperror=9');
} else {
$userid = mysqli_insert_id();
// Let's mail the user!
$subject = "Fundfeeder Accounts";
$message = "Dear $first_name $last_name,
Thank you for registering at Fundfeeder, http://www.fundfeeder.co.uk/
You are one step away from logging in and accessing your account.
To activate your membership, please click here: http://www.fundfeeder.co.uk/activate.php?id=$userid&code=$db_password
Once you activate your memebership, you will be able to login with the information you provided.
Thanks!
FundFeeder Admin Team.
This is an automated response, please do not reply!";
mail($email_address, $subject, $message, "From: FundFeeder Accounts<admin@fundfeeder.co.uk>\nX-Mailer: PHP/" . phpversion());
////// MAIL ADMIN
$subject2 = "FundFeeder New User!";
$message2 = "Dear Admin,
This is a message to alert you that a new user has signed up to FundFeeder.
You can view all details of the new member and all other members direct from the admin control panel at http://fundfeeder.co.uk/admin.php
Here are the details of the new registered user:
Username: $username
Email Address: $email_address
Thanks!
FundFeeder Accounts.
This is an automated response, please do not reply!";
mail('aidan6141@hotmail.co.uk', $subject2, $message2, "From: FundFeeder Accounts<admin@fundfeeder.co.uk>\nX-Mailer: PHP/" . phpversion());
header('Location: signin.php?loginerror=6');
include 'signin.php';
}
?>
core/init.php(数据库连接)-
<?php
$conn = mysqli_connect("mysql.hostinger.co.uk","u479096627_admin","password", "u479096627_ctrl");
//Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
?>
您需要查看 core/init.php 以找到建立 mysqli 连接的位置。你应该看到这样的东西:
$link = mysqli_connect("myhost","myuser","mypassword","mydb");
如果该文件尚未更新为使用 mysqli,它可能如下所示:
$link = mysql_connect('myhost', 'myuser', 'mypassword', 'mydb');
您需要做的就是将 mysql_connect
更改为 mysqli_connect
以使其使用 mysqli。
然后,您将 运行 查询如下:
$sql_email_check = mysqli_query($link, "SELECT email_address FROM users WHERE email_address='$email_address'");
有关详细信息,请参考 mysqli_connect 的 PHP 手册和
和 mysqli_query
您需要将数据库连接定义为所有 msqli_connect 语句的参数。
样本在这里:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$db ="database";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
现在将查询传递给 msqli_query 时:
$query = "your-SQL-query";
$run = msqli_query($conn,$query);
if(!$run){
//handle the error here if query fails
}
要检查任何已存在的行,您需要传递以下内容
在这个例子中它将是:
$row_check = mysqli_num_rows($run);
希望您现在了解如何使用 msqli* 函数。
所以我确实对MySQL和MySQLi之间的区别了解不多,我会在工作的时候慢慢地把它捡起来……我正在努力在这个新网站上使用我的旧 SQL PHP 注册表格但没有任何运气,我要么得到 "mysqli_query() expects at least 2 parameters, 1 given"
或空白页面,要么带回网站错误,例如 "Desired Username is a required field. Please enter it below."
该字段何时填写?如果有人能看一看,我将不胜感激!谢谢。
Register.php -
<?php
if (isset($_GET['signuperror'])) {
$signuperror = $_GET['signuperror'];
} else {//if there is no choice, display series 1
$signuperror = 1;
}
switch($signuperror) {
case 1:
echo '';
break;
case 2:
echo "<div class='alert alert-danger'><strong>First Name is a required field. Please enter it below.</strong></span></div> ";
break;
case 3:
echo "<div class='alert alert-danger'><strong>Last Name is a required field. Please enter it below.</strong></span></div> ";
break;
case 4:
echo "<div class='alert alert-danger'><strong>Email Address is a required field. Please enter it below.</strong></span></div> ";
break;
case 5:
echo "<div class='alert alert-danger'><strong>Desired Username is a required field. Please enter it below.</strong></span></div> ";
break;
case 6:
echo "<div class='alert alert-danger'><strong>Your passwords do NOT match, please check your passwords.</strong></span></div> ";
break;
case 7:
echo "<div class='alert alert-danger'><strong>Your email address has already been used by another user. Please use a different Email address.</strong></span></div> ";
break;
case 8:
echo "<div class='alert alert-danger'><strong>The username you have selected has already been used by another user. Please choose a different Username.</strong></span></div> ";
break;
case 9:
echo "<div class='alert alert-danger'><strong>There has been an error creating your account. Please contact the admin.</strong></span></div> ";
break;
default:
break;
}
?>
<form name="register" id="register" action="registercode.php" method="POST">
<div class="register-top-grid">
<h3>PERSONAL INFORMATION</h3>
<div class="wow fadeInLeft" data-wow-delay="0.4s">
<span><b>First Name</b><label>*</label></span>
<input type="text" value="" data-msg-required="Please enter your first name." maxlength="100" name="first_name" id="first_name" required>
</div>
<div class="wow fadeInRight" data-wow-delay="0.4s">
<span><b>Last Name</b><label>*</label></span>
<input type="text" value="" data-msg-required="Please enter your last name." maxlength="100" name="last_name" id="last_name" required>
</div>
<div class="wow fadeInRight" data-wow-delay="0.4s">
<span><b>Email Address</b><label>*</label></span>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." size="68" name="email_address" id="email" required>
</div>
<div class="clearfix"> </div>
<a class="news-letter" href="#">
<label class="checkbox"><input type="checkbox" name="checkbox" checked=""><i> </i>Sign Up for Newsletter</label>
</a>
</div>
<div class="register-bottom-grid">
<h3>LOGIN INFORMATION</h3>
<div class="wow fadeInLeft" data-wow-delay="0.4s">
<span><b>Username</b><label>*</label></span>
<input type="text" value="" data-msg-required="Please enter a valid username." maxlength="100" name="username" id="username" required>
</div><div class="clearfix"> </div><div class="clearfix"> </div><div class="clearfix"> </div><div class="clearfix"> </div>
<div class="wow fadeInLeft" data-wow-delay="0.4s">
<span><b>Password</b><label>*</label></span>
<input type="password" value="" name="password" id="password" required>
</div>
<div class="wow fadeInRight" data-wow-delay="0.4s">
<span><b>Confirm Password</b><label>*</label></span>
<input type="password" value="" name="passwordcheck" id="passwordcheck" required>
</div>
</div>
<div class="clearfix"> </div>
<div class="register-but">
<input type="submit" value="REGISTER" class="btn btn-info"> | Already a member? <a href="signin.php"><b>Login</b></a>
<div class="clearfix"> </div>
</form>
</div>
</div>
</div>
</div>
<!-- registration -->
registercode.php -
<?
include 'core/init.php';
// Define post fields into simple variables
$first_name = mysqli_real_escape_string($_POST['first_name']);
$last_name = mysqli_real_escape_string($_POST['last_name']);
$email_address = mysqli_real_escape_string($_POST['email_address']);
$username = mysqli_real_escape_string($_POST['username']);
$password = $_POST['password'];
$passwordcheck = $_POST['passwordcheck'];
/* Let's strip some slashes in case the user entered
any escaped characters. */
$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
/* Do some error checking on the form posted fields */
if((!$first_name) || (!$last_name) || (!$email_address) || (!$username) || (!$password) || (!$passwordcheck)){
if(!$first_name){
header('Location: register.php?signuperror=2');
}
if(!$last_name){
header('Location: register.php?signuperror=3');
}
if(!$email_address){
header('Location: register.php?signuperror=4');
}
if(!$username){
header('Location: register.php?signuperror=5');
}
if ($password !== $passwordcheck){
header('Location: register.php?signuperror=6');
}
include "register.php"; // Show the form again!
/* End the error checking and if everything is ok, we'll move on to
creating the user account */
exit(); // if the error checking has failed, we'll exit the script!
}
/* Let's do some checking and ensure that the user's email address or username
does not exist in the database */
$sql_email_check = mysqli_query($conn, "SELECT email_address FROM users WHERE email_address='$email_address'");
$sql_username_check = mysqli_query($conn, "SELECT username FROM users WHERE username='$username'");
$email_check = mysqli_num_rows($sql_email_check);
$username_check = mysqli_num_rows($sql_username_check);
if(($email_check > 0) || ($username_check > 0)){
if($email_check > 0){
header('Location: register.php?signuperror=7');
unset($email_address);
}
if($username_check > 0){
header('Location: register.php?signuperror=8');
unset($username);
}
include 'register.php'; // Show the form again!
exit(); // exit the script so that we do not create this account!
}
/* Everything has passed both error checks that we have done.
It's time to create the account! */
$db_password = md5($password);
// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysqli_query($conn, "INSERT INTO users (email_address, username, first_name, last_name, password, reg_date, last_login, activated, admin)
VALUES('$email_address', '$username', '$first_name', '$last_name', '$db_password', now(), now(), '0', '0')") or die (mysqli_error());
if(!$sql){
header('Location: register.php?signuperror=9');
} else {
$userid = mysqli_insert_id();
// Let's mail the user!
$subject = "Fundfeeder Accounts";
$message = "Dear $first_name $last_name,
Thank you for registering at Fundfeeder, http://www.fundfeeder.co.uk/
You are one step away from logging in and accessing your account.
To activate your membership, please click here: http://www.fundfeeder.co.uk/activate.php?id=$userid&code=$db_password
Once you activate your memebership, you will be able to login with the information you provided.
Thanks!
FundFeeder Admin Team.
This is an automated response, please do not reply!";
mail($email_address, $subject, $message, "From: FundFeeder Accounts<admin@fundfeeder.co.uk>\nX-Mailer: PHP/" . phpversion());
////// MAIL ADMIN
$subject2 = "FundFeeder New User!";
$message2 = "Dear Admin,
This is a message to alert you that a new user has signed up to FundFeeder.
You can view all details of the new member and all other members direct from the admin control panel at http://fundfeeder.co.uk/admin.php
Here are the details of the new registered user:
Username: $username
Email Address: $email_address
Thanks!
FundFeeder Accounts.
This is an automated response, please do not reply!";
mail('aidan6141@hotmail.co.uk', $subject2, $message2, "From: FundFeeder Accounts<admin@fundfeeder.co.uk>\nX-Mailer: PHP/" . phpversion());
header('Location: signin.php?loginerror=6');
include 'signin.php';
}
?>
core/init.php(数据库连接)-
<?php
$conn = mysqli_connect("mysql.hostinger.co.uk","u479096627_admin","password", "u479096627_ctrl");
//Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
?>
您需要查看 core/init.php 以找到建立 mysqli 连接的位置。你应该看到这样的东西:
$link = mysqli_connect("myhost","myuser","mypassword","mydb");
如果该文件尚未更新为使用 mysqli,它可能如下所示:
$link = mysql_connect('myhost', 'myuser', 'mypassword', 'mydb');
您需要做的就是将 mysql_connect
更改为 mysqli_connect
以使其使用 mysqli。
然后,您将 运行 查询如下:
$sql_email_check = mysqli_query($link, "SELECT email_address FROM users WHERE email_address='$email_address'");
有关详细信息,请参考 mysqli_connect 的 PHP 手册和 和 mysqli_query
您需要将数据库连接定义为所有 msqli_connect 语句的参数。
样本在这里:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$db ="database";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
现在将查询传递给 msqli_query 时:
$query = "your-SQL-query";
$run = msqli_query($conn,$query);
if(!$run){
//handle the error here if query fails
}
要检查任何已存在的行,您需要传递以下内容
在这个例子中它将是:
$row_check = mysqli_num_rows($run);
希望您现在了解如何使用 msqli* 函数。