我现在尝试使我的代码更具用户功能,并从函数中删除我的全局变量,也使用mysqli而不是mysql。当用户已经在数据库中时,我无法纠正错误。
当我将用户添加到数据库(谁不在我的数据库中)时。我的数据库已更新,我的回声输出符合预期:
There are no empty Form Input Fields
christest@allcoles.com WAS NOT found in the table: emailUser field: username
DOB is: 2009-02-12
christest@allcoles.com has been added to the Webmail Database
Database is Closed.
End of Registration.
当我开始一个新会话并尝试添加相同的用户名时,我得到:
There are no empty Form Input Fields
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /hermes/bosoraweb033/b1714/ipg.allcolescom/_webmail/mailDB.php on line 75
我期望得到的是:
There are no empty Form Input Fields
There is already a user registered with this username. Please try a different one.
Database is Closed.
End of Registration.
第75行(这是我的checkInField函数):
if (!$row = mysqli_fetch_array($result) or die(mysqli_error()))
完整的代码在这里:
//Main Code Sequence
error_reporting(-1);
ini_set('display_errors',1);
//Database Setup
$db_host = "***";
$db_name = "***";
$db_table = "emailUser";
$db_username = "***";
$db_password = "***";
$mysqli_db = new mysqli($db_host,$db_username,$db_password,$db_name);
function webmailForgottenPassword() //The function for the website FORGOTTEN PASSWORD FORM
{
// Code to be added
}
function webmailSignUp($db_connection,$db_con_table) //The function for the website REGISTER FORM
{
$webmailFullName = $_POST['webmailFullName'];
$webmailUserName = $_POST['webmailUserName'];
$webmailExEmail = $_POST['webmailExEmail'];
$webmailPhone = $_POST['webmailPhone'];
$webmailDOB = $_POST['webmailDOB'];
//Check that the fields are not empty
if (checkBlankFieldsError($webmailFullName,$webmailUserName,$webmailExEmail,$webmailPhone,$webmailDOB) == false)
{
echo "There are no empty Form Input Fields<br>";
//Check that there is no existing name in the table
if (checkInField($webmailUserName,$db_connection,$db_con_table,"userName") == false)
{
//Check DOB Field
$dob = convertDate($webmailDOB);
echo "DOB is: $dob<br>";
//Binding and Query to prevent SQL injection
$query = "INSERT INTO $db_con_table(userFullName,userName,userExEmail,userPhone,userDOB) VALUES(?,?,?,?,?)";
$requery = $db_connection->prepare($query);
$requery->bind_param("sssss",$webmailFullName,$webmailUserName,$webmailExEmail,$webmailPhone,$dob);
if ($requery->execute())
{
echo "$webmailUserName has been added to the Webmail Database<br>";
}
else
{
echo "bind failed on $webmailUserName <br>";
}
}
else
{
echo "There is already a user registered with this username. Please try a different one.<br>";
}
}
else
{
echo "There is 1 or more empty input fields. Please try again.<br>";
}
$db_connection->close();
echo "Database is Closed.<br>";
}
function checkInField($value,$db_connection,$db_con_table, $db_field) // Checks a value is not within a database field
{
$query = "SELECT $db_field FROM $db_con_table WHERE $db_field='$value' LIMIT 1";
$result = mysqli_query($db_connection, $query) or die(mysqli_error());
if (!$row = mysqli_fetch_array($result) or die(mysqli_error()))
{
echo "$value WAS NOT found in the table: $db_con_table field: $db_field<br>";
return false;
}
else
{
echo "$value WAS found in the table: $db_con_table field: $db_field<br>";
return true;
}
}
function checkBlankFieldsError($field1,$field2,$field3,$field4,$field5) //Checks if form fields are blank
{
$fields = array($field1,$field2,$field3,$field4,$field5);
$error = false;
foreach($fields AS $fieldname) //Loop trough each fieldname in the fields array
{
if(empty($fieldname))
{
$error = true;
}
else
{
}
}
return $error;
}
function convertDate($aString) //Converts a String to a Date in Y-M-D format
{
$date2 = DateTime::createFromFormat('m/d/Y', $aString);
return $date2->format('Y-m-d');
}
//Main Code Sequence on form buttons
if(isset($_POST['webmailRegisterSubmit']))
{
webmailSignUp($mysqli_db,$db_table);
echo "End of Registration.<br>";
}
if(isset($_POST['webamilForgottenPWSubmit']))
{
webmailForgottenPassword();
echo "End of Password Reset Request.<br>";
}
?>
对于答案,请注意我处于初级阶段,因此很可能需要帮助才能理解原因。感谢您的时间。克里斯
答案 0 :(得分:0)
使用 connection.php
<?php
$host = "localhost";
$username = "";
$password = "";
$database = "";
$conn = mysql_connect($host, $username, $password) or die ("Could not connect");
$db = mysql_select_db($database, $conn) or die ("Could not select DB");
?>
和强>
if (!$row = mysqli_fetch_assoc($result) or die(mysqli_error()))
or
if (!$row = mysql_fetch_assoc (or) array($result) or die(mysqli_error()))
答案 1 :(得分:0)
您好我找到了一种替代方法,可以使用while循环来保持mySQLi。
干杯克里斯
$mysqli_db = new mysqli($db_host,$db_username,$db_password,$db_name);
//The Call
if (checkInField($webmailUserName,$db_connection,$db_con_table,"userName") == false)
{
echo "next part";
}
//The Query to find a username from a column within a table within a database
//using mySQLi
function checkInField($value,$db_connection,$db_con_table, $db_field) // Checks a value is not within a database field
{
$query = "SELECT $db_field FROM $db_con_table WHERE $db_field='$value'";
$result = $db_connection->query($query) or die($mysqli->error());
// GOING THROUGH THE DATA
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "User $value found: '$row[$db_field]' in the table $db_con_table column $db_field<br>";
mysqli_close($db_connection);
return true;
}
}
else
{
echo "User $value has not been found in the table $db_con_table column $db_field<br>";
mysqli_close($db_connection);
return false;
}
}