2012-04-19 35 views
1

警告:mysql_fetch_assoc():提供的參數不是在*一個有效的MySQL結果資源上線35警告:mysql_fetch_assoc():

if($_SERVER['REQUEST_METHOD'] =='POST'){ 

    $userid = $_SESSION['userid']; 
    $todo = $_POST['todo']; 
    $description = $_POST['description']; 
    $date = $_POST['date']; 
    $priority = $_POST['priority']; 
    $username = $_SESSION['username']; 
} 

$get_product = mysql_query("SELECT * FROM inlog WHERE userid = '".$_SESSION['userid']."' ORDER BY id DESC"); 

while ($row = mysql_fetch_assoc($get_product)) { // Line 35 
     ?> 

我會就像有人能夠解釋我做錯了什麼一樣,搜索了網頁,但無法解決我的問題。這就是爲什麼我在這裏:) /////////問題已解決

下一個問題: 林呼應,(而不是死)我的錯誤的待辦事項等等。但問題是,他仍然增加它在我的數據庫中。任何人都可以解釋怎麼做,我知道如果他沒有死,他仍然會添加它,但只會給出一條消息。

我猜這裏沒有必要把腳本放在這裏。但如果是這樣。我添加它。

+0

可能重複http://stackoverflow.com/questions/169520/warning-when-using-mysql- fetch-assoc-in-php) – jprofitt 2012-04-19 14:36:27

+0

http://stackoverflow.com/search?q=Warning%3A+mysql_fetch_assoc%28%29%3A+supplied+argument+is+not+a+valid+MySQL+result+resource – deceze 2012-04-19 14:36:34

+0

查詢後檢查'mysql_error()'。有一些問題是你無法獲取的原因。 – Cfreak 2012-04-19 14:37:17

回答

2

最有可能的事情是空的,更新腳本,以查找問題:

if($_SERVER['REQUEST_METHOD'] =='POST'){ 
    $userid = (int) $_SESSION['userid']; // Cast to (int) to make it safe 
    if (empty($userid)) 
     die('Invalid User ID'); 

    $todo = $_POST['todo']; 
    if (empty($todo)) 
     die('Invalid todo'); 

    $description = $_POST['description']; 
    if (empty($description)) 
     die('Invalid description'); 

    $date = $_POST['date']; 
    if (empty($date)) 
     die('Invalid date'); 

    $priority = $_POST['priority']; 
    if (empty($priority)) 
     die('Invalid priority'); 

    $username = $_SESSION['username']; 
    if (empty($todo)) 
     die('Invalid username'); 

    $get_product = mysql_query("SELECT * FROM inlog WHERE userid = '".$userid."' ORDER BY id DESC"); // See how I changed $_SESSION['userid'] to $userid 
} 

另外,還要確保你逃避的變量,你做一個查詢與他們之前。就像我將用戶標識轉換爲整數一樣。

關於第二個問題:

下一個問題:IM呼應,(而不是dieing)我錯了待辦事項等。但 問題是,他還補充說在我的DB。任何人都可以解釋什麼 要做到這一點,我明白,如果他不死,他仍然會 添加它,但只給出一條消息。根據我

最佳的解決方案:

if($_SERVER['REQUEST_METHOD'] =='POST'){ 
    $errors = array(); 

    $userid = (int) $_SESSION['userid']; // Cast to (int) to make it safe 
    if (empty($userid)) 
     $errors[] = 'Invalid User ID' 

    $todo = $_POST['todo']; 
    if (empty($todo)) 
     $errors[] = 'Invalid todo'; 

    $description = $_POST['description']; 
    if (empty($description)) 
     $errors[] = 'Invalid description'; 

    $date = $_POST['date']; 
    if (empty($date)) 
     $errors[] = 'Invalid date'; 

    $priority = $_POST['priority']; 
    if (empty($priority)) 
     $errors[] = 'Invalid priority'; 

    $username = $_SESSION['username']; 
    if (empty($todo)) 
     $errors[] = 'Invalid username'; 

    // Only do the query when there are no errors  
    if (count($errors) <= 0) { 
     $get_product = mysql_query("SELECT * FROM inlog WHERE userid = '".$userid."' ORDER BY id DESC"); // See how I changed $_SESSION['userid'] to $userid 
    } else { 
     echo implode('<br />', $errors); // or return is also a possibility 
    } 
} 
[使用PHP mysql_fetch_assoc時警告](的
相關問題