2013-04-11 128 views
0

我想知道如果我能讓人看看我的陳述,看看我可能會搞砸了。我已經通過回顯測試了哪些信息,並且一切看起來都正確,但我無法通過物理方式創建適當的記錄。我也沒有得到任何錯誤,並返回到表單發佈後的標題位置。表格信息不張貼

//First we make sure there are enough licenses left to add the user 
$limit = "select * from organization_seats WHERE orgid=$orgid"; 
$orglimit = mysql_query($limit); 
$licenses = $orglimit['limit']; 

$count = "select count(*) from organization_users WHERE organizationid=$orgid"; 

if ((!$licenses < $count)) { 

    echo 'You have reached the number of maximum licenses for your Organization.'; 

} else { 

//If we have licenses left, proceed to add new user 
//Populate the user table 
$sql = "insert into user (firstname, lastname, title, address1, address2, country, city, state, zip, phone, mobile, birthday, username, email, password) values ('$fname','$lname','$title','$address1','$address2','$country', '$city', '$state', '$zip', '$phone', '$mobile', '$bday', '$username', '$email', '$password')"; 

$exec = mysql_query($sql); 

//Add the user to the organization 
$userid = mysql_insert_id(); //call the last ID entered into the user table first 

$sql2 = "insert into organization_users(organizationid, userid, active) values ('$orgid', '$userid', $)"; 
$exec = mysql_query($sql2); 

//recall the userid 
$sql3 = "select * from user where username = $username"; 
$exec = mysql_query($sql3); 
$newuserid = $newuserselect['id']; 

//Add the user to the department 
$sql4 = "insert into organization_dep_users(orgid, depid, userid) values ('$orgid', '$department', '$newuserid')"; 
$exec = mysql_query($sql4); 

if ($exec === TRUE) { 

    header('Location: index.php') ; 

} else { 
    echo mysql_error(); 
} 
} 

順便說一句,我有mysql_real_escape_string附加到我所有的變量。

+0

在新代碼[請不要使用'mysql_ *'功能(http://stackoverflow.com/q/12859942/1190388)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到紅色框?改爲了解準備好的語句,然後使用[tag:PDO]或[tag:MySQLi]。 – hjpotter92 2013-04-11 03:59:13

+1

哪個部分搞亂了? – 2013-04-11 04:00:42

+0

你應該在每次調用後檢查'mysql_error()'。如果以前的通話失敗,您將聽不到它。 '$ sql3'的結果也不會被使用。 – Jim 2013-04-11 04:04:42

回答

0

1)$sql2有錯誤 - 您傳遞的是$而不是實際變量。

2)在$sql3之後,您將從不存在的資源中分配$newuserid。我假設你在它之前缺少$newuserselect = mysql_fetch_assoc($exec);

3)您確實需要在查詢中添加錯誤檢查。如果第一個查詢失敗,第二個查詢將以錯誤的$userid或者FALSE運行,如果以前的查詢沒有創建id。其他問題可能會稍後在代碼中出現,而無需進行錯誤檢查。

4)如上所示,建議過渡到pdo或mysqli。

5)只注意到 - 你的第一選擇查詢還試圖濫用資源 - 你應該做

$orglimit = mysql_query($limit); 
$orgrow = mysql_fetch_assoc($orglimit); 
$licenses = $orgrow['limit']; 

6)又....你$count將無法​​正常工作,您指定的查詢字符串到$count,但從未實際執行查詢來獲取該號碼。所以當你做if ((!$licenses < $count))時,你實際上是將一個數字與一個字符串進行比較,而不是將一個數字與一個數字進行比較。

+0

非常感謝您爲第二組眼睛。我已根據您的建議調整了所有內容,但是當總用戶數爲48且許可證數量限制爲50時,它給我提供了沒有足夠許可證的錯誤。 – Ansipants 2013-04-11 04:30:15

+0

嘗試'if($ licenses> $ count)',如果不工作嘗試'如果($ licenses>(int)$ count)' – 2013-04-11 04:36:22

+0

我能清除最後的錯誤。再次感謝你們! – Ansipants 2013-04-11 04:57:16

0

不知道什麼問題是你到底面臨着..但如果你已經正確地複製代碼,然後我發現了一個錯誤的說法

insert into organization_users(organizationid, userid, active) values ('$orgid', '$userid', $) 

是什麼$ ...?

第二..

$limit = "select * from organization_seats WHERE orgid=$orgid"; 
$orglimit = mysql_query($limit); 
$licenses = $orglimit['limit']; 

應該

$limit = "select * from organization_seats WHERE orgid=$orgid"; 
$resource = mysql_query($limit); 
$orglimit = mysql_fetch_assoc($resource); 
$licenses = $orglimit['limit']; 

的mysql_query總是返回resorce不是陣列..

同樣以$ SQL3

試着改變這些和你應該沒事

建議:請開始使用mysqli_ *或PDO

+0

我已更正所有錯誤,在查詢上更改爲mysqli,添加錯誤處理。所以我現在唯一的問題就是說驗證許可證。我已經嘗試了兩個冬季血推薦,但仍然沒有去。 – Ansipants 2013-04-11 04:46:56