2012-05-19 79 views
1

我剛剛開始製作網站。在註冊頁面的頂部,有一個搜索欄,您可以在其中找到現有用戶。找到並選擇用戶後,它將自動填寫註冊表單中的值。從那裏,你可以更新用戶信息。或者創建一個新用戶。出於某種原因,無論我輸入什麼值,它只會調用更新功能,而不會創建新用戶。任何幫助將不勝感激。謝謝!無法使用PHP更新MySQL中的表格

//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 
+0

記住(特別是在執行更新和刪除時)過濾和轉義用戶提供的任何數據,包括post,get,server,session和cookie數據以從SQL注入中進行自我投影。 –

回答

1

在回答@jeroen,此代碼應幫助@ylhtravis:

if (mysql_num_rows($exist_result)) { 
    //error code here 
} else { 
    //continue 
} 
1

的問題是,你正在測試$exist_result,並且總是返回true只要查詢有效(它返回一個資源並且評估爲真)。

您需要對$exist_result中的行進行計數並檢查0行。

我建議切換到PDO /準備語句,但在你的情況下,你可以使用:

mysql_num_rows($exist_result); 
+0

謝謝!我不知道它會一直迴歸真實。我剛剛開始從資源網站學習PHP。這非常有趣!而你們是非常有幫助和令人敬畏的!再次感謝你! – ylhtravis

+0

@ylhtravis不客氣。請注意,嚴格來說,它不會返回「true」,它會返回資源。 – jeroen

+0

哦,如果你不介意,我有一個問題。它總是返回一個'資源#'。這個數字到底是什麼意思? – ylhtravis