老是显示Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource,但是我又查不出来是什么原因,数据能正常显示,下面附源代码数据库无法插入数据,然后加@后警告消失,但是数据库也无法插入<?php
  include ("include/setting.inc.php");
  include ("include/conn.php");
  mysql_query("SET NAMES 'UTF8'");
  $_SESSION['admin']=false;
  session_start();
  
  $name=$_SESSION['u_name'];      
  $str="select u_id from user where u_name=".$name;     //这句搜索的字段无误
  $result=mysql_query($str);
  $row=mysql_fetch_array($result);      //这句显示有问题
  $user=$row['u_id'];
  
  $time=date("Y-m-d H:i:s");     
  $state="未发";     //订单状态
  $total=$_POST['subtotal'];
  $orderid=date("YmdHis").$user;    
  
  $cartCheck=explode("@",$_SESSION['cart']);
  for($i=0;$i<count($cartCheck)-1;$i++){
  $str1="select * from books where b_id=".$cartCheck[$i];
  $result1=mysql_query($str1);
  while($row1=mysql_fetch_array($result1)){      //这句也是有问题
  $bookname=$row1['b_name'];    
  }
  $p="price_".$cartCheck[$i];
  $n="num_".$cartCheck[$i];
  $price=$_POST[$p];   
      $num=$_POST[$n];   
  
  //插入booking表
  $str2="insert into booking(o_id,o_user,o_time,o_state,o_total) values('$orderid','$user','$time','$state','$total')";
  $result2=mysql_query($str2);
  
  //插入information表
  $str3="insert into information(i_orderid,i_name,i_number,i_price) values('$orderid','$bookname','$num','$price')";
  $result3=mysql_query($str3);
  }
?>

解决方案 »

  1.   

    可能是数据库连接没成功,也可能是SQL语句有语法错误
      

  2.   

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource提示你,你的查询结果急为空。//另外:这样不好,反复查询数据库
     for($i=0;$i<count($cartCheck)-1;$i++){
          $str1="select * from books where b_id=".$cartCheck[$i];
     }//用group by 来处理这个。
    $str1="select * from books where b_id in($bid_list) group by b_id 
      

  3.   

    1 查一下mysql_connect做了没有?
    2 如果没问题 查一查你的session是否取到值!可以试一下
     
      $name=$_SESSION['u_name'];
      $cart = $_SESSION['cart'];      
      if(strlen($name)==0 || strlen($cart)==0){
          echo "SESSION value is NG!";
          exit;
      }else{
          //continue
      }
     
    3 如果是字符串 用"select u_id from user where u_name='".$name."'";
      

  4.   

    $str="select u_id from user where u_name=".$name;这句取不到值,奇怪了
      

  5.   

    $str="select u_id from user where u_name='".$name."'";这样应该可以