警告:mysql_fetch_assoc():

时间:2012-04-19 14:33:58

标签: php mysql

警告:mysql_fetch_assoc():提供的参数不是第35行 * 中的有效MySQL结果资源

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
      ?>

我想如果有人能解释我做错了什么,搜索网络,但无法解决我的问题。那就是为什么我在这里:) /////////问题解决了

下一个问题: 我回应,(而不是死)我错误的待办事项等。但问题是,他仍然在我的数据库中添加它。任何人都可以解释如何对付它,我明白如果他不死,他仍然会添加它,但只会给出一个信息。

我猜我没有必要把脚本放在这里。但如果是这样。我加上它。

1 个答案:

答案 0 :(得分: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
}   

还要确保在使用它们进行查询之前转义变量。就像我将userid转换为整数的方式一样。

关于第二个问题:

  

下一个问题:我回应,(而不是死)我错误的待办事项等。但是   问题是,他仍然在我的数据库中添加它。谁能解释一下   反对它,我明白,如果他不死,他仍然会   添加它,但只给出一条消息。

根据我的最佳解决方案:

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
    }
}