2012-04-19 57 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时警告](的
相关问题