1

我刚开始做网站。在注册页面的顶部,有一个搜索栏,您可以在其中找到现有用户。找到并选择用户后,它将自动填写注册表中的值。从那里,您可以更新用户信息。或者创建一个新用户。出于某种原因,无论我输入什么值,它都只调用更新函数而不创建新用户。任何帮助将不胜感激。谢谢!

//Checks database to see if user exist by first name and last name
$check_exist ="SELECT * FROM staff WHERE firstname= '$firstname' AND     lastname='$lastname'";
$exist_result = mysql_query($check_exist);

echo "TEST exist_result:" .$exist_result ."<br>";

//Updates user if user already exist
if($exist_result)
{
    echo "Update function called... <br>";
    for($i = 0; $exist_row = mysql_fetch_array($exist_result); $i++)
    {
        //get the id number
        $ID = $exist_row['ID'];
    }

    //Updates staff table
    mysql_query("UPDATE staff SET position = '$position', firstname = '$firstname', lastname = '$lastname', 
    address = '$address', city = '$city', state = '$state', zipcode = '$zipcode', phone = '$phone', ss = '$ss'
    WHERE ID = '$ID'");

    //Updates login table
    mysql_query("UPDATE login SET ID = '$ID', position = '$position', username =     '$username', password = '$password', email = '$email' WHERE ID = '$ID'");

}

//Registers user if user doesn't exist in database
 else 
{
    echo "Insert function called...<br>";
    //Insert Staff information
    $insert_staff = "INSERT INTO staff (position, firstname, lastname, 
    address, city, state, zipcode, phone, ss) 
    VALUES ('$position','$firstname','$lastname','$address','$city',
    '$state','$zipcode','$phone','$ss')";
    mysql_query($insert_staff);

    //This gets the newly created unqiue ID number from staff and insert it into login table
    $get_id = mysql_query("SELECT * FROM staff
    WHERE firstname='$firstname' AND lastname ='$lastname'");
    $login_row = mysql_fetch_array($get_id);
    $eID = $login_row['ID'];

    //Insert Login information
    $query2 = "INSERT INTO login (ID, position, username, password, email)
    VALUES ('$eID', '$position','$username','$password','$email')";
    mysql_query($query2);
}

这是我得到的错误:

Notice: Use of undefined constant myusername - assumed 'myusername' in         /Applications/MAMP/htdocs/cms/manager/insertReg.php on line 9

Deprecated: Function session_is_registered() is deprecated in /Applications/MAMP/htdocs/cms/manager/insertReg.php on line 9

Notice: A session had already been started - ignoring session_start() in /Applications/MAMP/htdocs/CMS/connectdb.php on line 8
TEST first name:ksdfjlsdkjf3242
TEST last name:lkjdslfkjdslkj
TEST exist_result:Resource id #5
Update function called... 

Notice: Undefined variable: ID in /Applications/MAMP/htdocs/cms/manager/insertReg.php on line 55

Notice: Undefined variable: ID in /Applications/MAMP/htdocs/cms/manager/insertReg.php on line 58

Notice: Undefined variable: ID in /Applications/MAMP/htdocs/cms/manager/insertReg.php on line 59
4

2 回答 2

1

问题是您正在测试并且只要查询有效$exist_result,它将始终返回(它返回一个资源并且评估为真)。true

您需要计算行数$exist_result并检查0行。

我建议切换到 PDO /prepared statements,但在您的情况下,您可以使用:

mysql_num_rows($exist_result);
于 2012-05-19T21:31:36.087 回答
1

作为对@jeroen 的回复,这段代码应该对@ylhtravis 有所帮助:

if (mysql_num_rows($exist_result)) {
    //error code here
} else {
    //continue
}
于 2012-05-19T21:34:06.600 回答