类别数据库表:

id  | name
1   | electronics
2   | Automotive


分类数据库表:

id  user_id  category_id    cover          title       price    description
102  1       2              iamges/1.jpg    blabla     10      blablabla


我收到一条错误消息:注意:第49行上的** \ post.php中的未定义变量:name1

$id = $_GET['id'];
$res2 = mysql_query("SELECT * FROM `classifieds` WHERE `id` = '" . $id . "' LIMIT 1");

if($res2 && mysql_num_rows($res2) > 0){
    while($row = mysql_fetch_assoc($res2)){
        $id = $row['id'];
        $user_id = $row['user_id'];
        $category_id = $row['category_id'];
        $price = $row['price'];
        $cover = $row['cover'];
        $title = $row['title'];
        $description = $row['description'];
        $profile_data = user_data($user_id, 'username');

        $res3 = mysql_query("SELECT * FROM `categories` WHERE `id` = '" . $category_id . "' LIMIT 1");
        if($res3 && mysql_num_rows($res3) > 0){
            while($row1 = mysql_fetch_assoc($res3)){
                $id1 = $row1['id'];
                $name = $row1['name'];
            }
        }
    }
}else{
    echo 'error';
}echo $name;


为什么我得到echo $ name1的错误? ?

最佳答案

您应该打印查询并直接在php myadmin中运行,看看它是否返回正确的结果。

您的whilewhile循环包含相同的$row。请从下面一个改为另一个

 while($row = mysql_fetch_assoc($res2)){

and

  while($row = mysql_fetch_assoc($res3)){
  ------^


改成$row3

  while($row3 = mysql_fetch_assoc($res3)){
        $id = $row3['id'];
        $name = $row3['name'];
    }

10-05 17:54
查看更多