請有人可以幫助我使用我的代碼。我一整天都在這裏,幾乎放棄了。功能無法正常工作
形式顯示: checkfields不是空 這是預期... 它繼續通過函數checkUser()如下:
global $mysqli_db;
global $db_table;
//Check the field userName is the same as the Posted Username
$Field = "userName"; //The Field to check
$query = "SELECT $Field WHERE $Field=$userNameCheck FROM $db_table LIMIT 1";
$result = mysqli_query($mysqli_db, $query) or die(mysql_error());
echo "The result of the checkUser is:<br>";
echo $result;
echo "<br>";
if (!$row = mysqli_fetch_array($result) or die(mysql_error()))
{
echo "username was not found in the field in the table<BR>";
return false; //username was not found in the field in the table
}
領域是我的表$ db_table內的數據庫字段。我只是把它作爲一個簡單的變量。我希望下一個回聲說checkuser的結果是: 但它沒有得到那麼遠,所以我想我的$ field和$ result之間有一個錯誤? 希望這個更清楚,道歉很漫長的一天。
<?php
//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 webmailSignUp()
{
global $mysqli_db;
global $db_table;
$webmailFullName = $_POST['webmailFullName'];
$webmailUserName = $_POST['webmailUserName'];
$webmailExEmail = $_POST['webmailExEmail'];
$webmailPhone = $_POST['webmailPhone'];
$webmailDOB = $_POST['webmailDOB'];
//Check that the fields are not empty
echo "check";
if ((empty($webmailFullName)) or (empty($webmailUserName)) or (empty($webmailExEmail)) or (empty($webmailPhone)) or (empty($webmailDOB)))
{
echo "One of your fields are blank! Please try again<BR>";
}
else
{
echo "fields are not empty<BR>";
//Check that there is no existing name in the table
if (checkUser($webmailUserName) == false)
{
echo "Result is <BR>";
echo checkUser($webmailUserName);
//Adding the person to the Database Query
//$query = "INSERT INTO $db_table(userFullName,userName,userExEmail,userPhone,userDOB) VALUES(?,?,?,?,?)";
//Binding to Prevent SQL injection
//$requery = $mysqli_db->prepare($query);
//$requiry->bind_param($webmailFullName,$webmailUserName,$webmailExEmail,$webmailPhone,$webmailDOB);
//if ($requery->execute())
//{
// echo "Person has been added";
//}
//else
//{
// echo "bind failed";
//}
}
else
{
echo "There is already a user registered with this username. Please try a different one.<BR>";
}
}
}
function checkUser($userNameCheck)
{
global $mysqli_db;
global $db_table;
//Check the field userName is the same as the Posted Username
$Field = "userName"; //The Field to check
$query = "SELECT $Field WHERE $Field=$userNameCheck FROM $db_table LIMIT 1";
$result = mysqli_query($mysqli_db, $query) or die(mysql_error());
echo "The result of the checkUser is:<br>";
echo $result;
echo "<br>";
if (!$row = mysqli_fetch_array($result) or die(mysql_error()))
{
echo "username was not found in the field in the table<BR>";
return false; //username was not found in the field in the table
}
else
{
echo "username was found in the field in the table<BR>";
return true; //username was found in the field in the table
}
}
function db_close()
{
global $mysqli_db;
$mysqli_db->close();
}
if(isset($_POST['webmailRegisterSubmit']))
{
webmailSignUp();
db_close();
echo "End of Registration";
}
if(isset($_POST['webamilForgottenPWSubmit']))
{
webmailForgottenPassword();
db_close();
echo "End of Password Reset Request";
}
?>
爲什麼你有'$ Field =「userName」;'?爲什麼不直接將實際的列名稱放入查詢中,而不是像這樣做複雜? –
這是我的數據庫中的用戶名字段。我有一個名爲emailUser的表,其中一個字段名稱是用戶名。這個想法是從用戶名字段中的帖子中查找用戶名,以檢查是否已經存在 – Smokey
對不起,我無法在代碼中發現字符串「checkfields不爲空」... – arkascha