2016-12-27 62 views
0

I am trying to insert data into a table but getting this error- Catchable fatal error: Object of class mysqli_result could not be converted to string on line 44 the error is showing for the following line.捕獲的致命錯誤:類mysqli_result的對象不能被轉換成字符串上線44

$query= "insert into product(proname,prodesc,proprice,proimg,catid) values('$proname','$prodesc','$proprice','$file_path','$catid')"; 
      if(mysqli_query($conn,$query)){ 
       header('location:product.php'); 
      } 
      else{ 
       echo 'cant insert data'.mysqli_error($conn); 
      } 
     } 
    } 
    ?> 

following is the entire program

$errors=array(); 
if(!(empty($_POST))){ 
    $proname=$_POST['proname']; 
    $proprice=$_POST['proprice']; 
    $prodesc=$_POST['prodesc']; 
    $catname=$_POST['cat']; 
    $catid=mysqli_query($conn,"select catid from category where catname='$catname'") or die('cant get category id'.mysqli_error($conn)); 
    if(empty($proname) or empty($proprice)){ 
     $errors[]='Please enter product name and price.'; 
    } 
    if(isset($_FILES['proimg'])){ 
     $file_name = $_FILES['proimg']['name']; 
     $file_size =$_FILES['proimg']['size']; 
     $file_tmp =$_FILES['proimg']['tmp_name']; 
     $file_type=$_FILES['proimg']['type']; 
     $file_ext=strtolower(end(explode('.',$_FILES['proimg']['name']))); 
     $expensions= array("jpeg","jpg","png"); 
     $file_path='imgproduct/'.$file_name; 
     if(in_array($file_ext,$expensions)=== false){ 
      $errors[]="extension not allowed, please choose a JPEG or PNG file."; 
     } 
    } 
    else{ 
     $errors[]='Please upload an image of product'; 
    } 
    if(!(empty($errors))){ 
     print_r($errors); 
    } 
    else{ 
     if(!(move_uploaded_file($file_tmp,$file_path))) { 
      echo '1.file not uploaded'; 
      exit; 
     } 
     $query= "insert into product(proname,prodesc,proprice,proimg,catid) values('$proname','$prodesc','$proprice','$file_path','$catid')"; 
     if(mysqli_query($conn,$query)){ 
      header('location:product.php'); 
     } 
     else{ 
      echo 'cant insert data'.mysqli_error($conn); 
     } 
    } 
} 
?> 

回答

-1

變化

$query= "insert into product(proname,prodesc,proprice,proimg,catid) values('$proname','$prodesc','$proprice','$file_path','$catid')"; 

$query= "insert into product(proname,prodesc,proprice,proimg,catid) values($proname,$prodesc,$proprice,$file_path,$catid)"; 
0

索爾ry以前的回答。現在希望它能解決問題。

$row = mysqli_fetch_array($result); $catid = $row['catid'];

$errors=array(); 
if(!(empty($_POST))){ 
    $proname=$_POST['proname']; 
    $proprice=$_POST['proprice']; 
    $prodesc=$_POST['prodesc']; 
    $catname=$_POST['cat']; 
    $result =mysqli_query($conn,"select catid from category where catname='$catname'") or die('cant get category id'.mysqli_error($conn)); 
    $row = mysqli_fetch_array($result); 
    $catid = $row['catid']; 

    if(empty($proname) or empty($proprice)){ 
     $errors[]='Please enter product name and price.'; 
    } 
    if(isset($_FILES['proimg'])){ 
     $file_name = $_FILES['proimg']['name']; 
     $file_size =$_FILES['proimg']['size']; 
     $file_tmp =$_FILES['proimg']['tmp_name']; 
     $file_type=$_FILES['proimg']['type']; 
     $file_ext=strtolower(end(explode('.',$_FILES['proimg']['name']))); 
     $expensions= array("jpeg","jpg","png"); 
     $file_path='imgproduct/'.$file_name; 
     if(in_array($file_ext,$expensions)=== false){ 
      $errors[]="extension not allowed, please choose a JPEG or PNG file."; 
     } 
    } 
    else{ 
     $errors[]='Please upload an image of product'; 
    } 
    if(!(empty($errors))){ 
     print_r($errors); 
    } 
    else{ 
     if(!(move_uploaded_file($file_tmp,$file_path))) { 
      echo '1.file not uploaded'; 
      exit; 
     } 
     $query= "insert into product(proname,prodesc,proprice,proimg,catid) values('$proname','$prodesc','$proprice','$file_path','$catid')"; 
     if(mysqli_query($conn,$query)){ 
      header('location:product.php'); 
     } 
     else{ 
      echo 'cant insert data'.mysqli_error($conn); 
     } 
+0

當你表達你希望成爲一個優秀的程序員,讓我給你一個提醒:一個優秀的程序員總是讀錯誤消息。 –

+0

@你的常識,是的先生,你的絕對正確。我應該首先關注錯誤消息。感謝您的建議。我非常感謝有人建議像你這樣的人。 –

+0

@你的常識,不僅給出這個問題的答案時間,而且我應該總是遵循錯誤信息,來自系統的錯誤。謝謝你,先生。我很高興我會遇見像你這樣的人,儘管這是一個虛擬的世界。 –

相關問題