功能无法正常工作

时间:2014-02-25 16:23:29

标签: php function mysqli return

请有人帮我处理我的代码。我整天都在这里,并且非常接近放弃。

表单显示: 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中的数据库字段。为简单起见,我只是将其作为变量。我希望下一个echo表示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";
}
?>

1 个答案:

答案 0 :(得分: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