Php 功能无法正常工作

Php 功能无法正常工作,php,function,mysqli,return,Php,Function,Mysqli,Return,请有人帮我写代码。我已经为此奋斗了一整天,几乎要放弃了 表格显示: 检查字段不是空的 这是预期的。。。 它通过下面的函数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

请有人帮我写代码。我已经为此奋斗了一整天,几乎要放弃了

表格显示: 检查字段不是空的 这是预期的。。。 它通过下面的函数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";
}
?>
$userNameCheck未定义,将创建一个断开的查询

$userNameCheck未正确地包含在查询中的引号中


您之所以看不到这一点,是因为尽管使用mysqli进行数据库查询,但仍然使用了mysql\u错误。改用mysqli_error.

为什么使用$Field=userName;?为什么不直接在查询中输入实际的列名,而不是像这样进行复杂运算呢?这是我数据库中的用户名字段。我有一个名为emailUser的表,其中一个字段名是username。其思想是在用户名字段中查找帖子中的用户名,以检查是否已经存在。对不起,我无法发现代码中的字符串检查字段不是空的……这一行不正确。在WHERE SELECT$Field之后有FROM,其中$Field=$userNameCheck FROM$db_table LIMIT 1应该先出现,然后检查WHERE Try SELECT$Field FROM$db_table,其中$Field='$userNameCheck'LIMIT 1@SmokeySo很多问题,所以时间很短。我在30分钟内完成了工作,但我不确定这是否有足够的时间来修复这个混乱:我请求你只使用“谢谢你”将MySQL\u错误修改为MySQL\u错误。请您解释一下引号的作用-它们是否只在MySQLquery字符串中需要?@Smokey这些引号是为了让MySQL知道$userNameCheck是一个字符串,而不是对字段的引用。再考虑一下,如果$USENAMECHECK中有空格,查询会是什么样子。
$query = "SELECT $Field FROM $db_table WHERE $Field=$userNameCheck LIMIT 1"; 
$result = mysqli_query($mysqli_db, $query) or die(mysql_error());