2014-02-25 141 views
-1

請有人可以幫助我使用我的代碼。我一整天都在這裏,幾乎放棄了。功能無法正常工作

形式顯示: 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"; 
} 
?> 
+0

爲什麼你有'$ Field =「userName」;'?爲什麼不直接將實際的列名稱放入查詢中,而不是像這樣做複雜? –

+0

這是我的數據庫中的用戶名字段。我有一個名爲emailUser的表,其中一個字段名稱是用戶名。這個想法是從用戶名字段中的帖子中查找用戶名,以檢查是否已經存在 – Smokey

+0

對不起,我無法在代碼中發現字符串「checkfields不爲空」... – arkascha

回答

1
$query = "SELECT $Field FROM $db_table WHERE $Field=$userNameCheck LIMIT 1"; 
$result = mysqli_query($mysqli_db, $query) or die(mysql_error()); 

$userNameCheck沒有定義,並且將創建一個破碎的查詢。

$userNameCheck在您的查詢中未正確引號引號。

你沒有看到這個的原因是因爲你使用mysql_error,儘管使用mysqli作爲數據庫查詢。改爲使用mysqli_error

+0

謝謝修改MySQL_error爲mysqli_errors。請你能解釋一下這些引用是做什麼的 - 他們只需要一個MySQLquery字符串? – Smokey

+1

@Smokey引號是爲了讓MySQL知道'$ userNameCheck'是一個字符串,而不是對字段的引用。另外考慮一下,如果'$ userNameCheck'中有空格,查詢會是什麼樣子。 – Jim