Hello mates i have a new problem in my project for an e-shop.
The message i get is:

Notice: Trying to get property of non-object in C:\Program Files\EasyPHP 2.0b1\www\eshop\order_fns.php on line 40
Could not store data, please try again.

I try to store data in a table if the content is the same of the one i insert in the text box with the php script, else it inserts a new customer and a new order (this two functions work perfectly) but i get this error message and the site doesn't continues. I checked the database and the table and the other files but all seem ok. The problem is in this line... Please help!!!

function insert_order($order_details)
{
  // extract order_details out as variables
  extract($order_details);

  // set shipping address same as address
  if(!$ship_name&&!$ship_address&&!$ship_city&&!$ship_state&&!$ship_zip&&!$ship_country)
  {
    $ship_name = $name;
    $ship_address = $address;
    $ship_city = $city;
    $ship_state = $state;
    $ship_zip = $zip;
    $ship_country = $country;
  }

  $conn =  new mysqli('localhost','root','','ekloges');

 if (mysqli_connect_errno())
{
	echo ' Error no connection';
	exit;
}
  // insert customer address
  $query =  "select costumerid from customers where  
name = '$name' and address = '$address' and city = '$city' and state = '$state' and zip = '$zip' and country = '$country' "; [B] $result = $conn->query($query);[/B] if($result->num_rows>0) { $customer = $result->fetch_object(); $customerid = $customer->customerid; } else { $query = "insert into customers values ('', '$name','$address','$city','$state','$zip','$country')"; $result = $conn->query($query); if (!$result) return false; } $customerid = $conn->insert_id; $date = date('Y-m-d'); $query = "insert into orders values ('', $customerid, ".$_SESSION['total_price'].", '$date', 'PARTIAL', '$ship_name', '$ship_address','$ship_city','$ship_state','$ship_zip', '$ship_country')"; $result = $conn->query($query); if (!$result) return false; $query = "select orderid from orders where customerid = $customerid and amount > ".$_SESSION['total_price']."-.001 and amount < ".$_SESSION['total_price']."+.001 and date = '$date' and order_status = 'PARTIAL' and ship_name = '$ship_name' and ship_address = '$ship_address' and ship_city = '$ship_city' and ship_state = '$ship_state' and ship_zip = '$ship_zip' and ship_country = '$ship_country'"; $result = $conn->query($query); if($result->num_rows>0) { $order = $result->fetch_object(); $orderid = $order->orderid; } else return false; // insert each book foreach($_SESSION['cart'] as $isbn => $quantity) { $detail = get_book_details($isbn); $query = "delete from order_items where
orderid = '$orderid' and isbn = '$isbn'"; $result = $conn->query($query); $query = "insert into order_items values ('$orderid', '$isbn', ".$detail['price'].", $quantity)"; $result = $conn->query($query); if(!$result) return false; } return $orderid; } ?>

    Such an error is typical
    when there was a ->query failure, error
    and so the $result it not really a good result

    For MySQLi is easy to make a control with error message.
    Then if people always put this AFTER each QUERY
    they will know what is the problem.

    	// TEST after all mysqli->query()
    	if( $mysqli->error ) exit( $mysqli->error );

    $mysqli->error
    is a message telling what was trouble.
    If no error, then $mysql->error is empty string.
    http://www.php.net/manual/en/mysqli.error.php

    I would try run this:

    <?php
    
    function insert_order($order_details)
    {
      // extract order_details out as variables
      extract($order_details);
    
      // set shipping address same as address
      if(!$ship_name&&!$ship_address&&!$ship_city&&!$ship_state&&!$ship_zip&&!$ship_country)
      {
        $ship_name = $name;
        $ship_address = $address;
        $ship_city = $city;
        $ship_state = $state;
        $ship_zip = $zip;
        $ship_country = $country;
      }
    
      $conn =  new mysqli('localhost','root','','ekloges');
    
     if (mysqli_connect_errno())
    {
    	echo ' Error no connection';
    	exit;
    }
      // insert customer address
      $query =  "select costumerid from customers where  
    name = '$name' and address = '$address' and city = '$city' and state = '$state' and zip = '$zip' and country = '$country' "; $result = $conn->query($query); // TEST when $result===false if($conn->error) exit($conn->error); if($result->num_rows>0) { $customer = $result->fetch_object(); $customerid = $customer->customerid; } else { $query = "insert into customers values ('', '$name','$address','$city','$state','$zip','$country')"; $result = $conn->query($query); // TEST when $result===false if($conn->error) exit($conn->error); if (!$result) return false; } $customerid = $conn->insert_id; $date = date('Y-m-d'); $query = "insert into orders values ('', $customerid, ".$_SESSION['total_price'].", '$date', 'PARTIAL', '$ship_name', '$ship_address','$ship_city','$ship_state','$ship_zip', '$ship_country')"; $result = $conn->query($query); // TEST when $result===false if($conn->error) exit($conn->error); if (!$result) return false; $query = "select orderid from orders where customerid = $customerid and amount > ".$_SESSION['total_price']."-.001 and amount < ".$_SESSION['total_price']."+.001 and date = '$date' and order_status = 'PARTIAL' and ship_name = '$ship_name' and ship_address = '$ship_address' and ship_city = '$ship_city' and ship_state = '$ship_state' and ship_zip = '$ship_zip' and ship_country = '$ship_country'"; $result = $conn->query($query); // TEST when $result===false if($conn->error) exit($conn->error); if($result->num_rows>0) { $order = $result->fetch_object(); $orderid = $order->orderid; } else return false; // insert each book foreach($_SESSION['cart'] as $isbn => $quantity) { $detail = get_book_details($isbn); $query = "delete from order_items where
    orderid = '$orderid' and isbn = '$isbn'"; $result = $conn->query($query); // TEST when $result===false if($conn->error) exit($conn->error); $query = "insert into order_items values ('$orderid', '$isbn', ".$detail['price'].", $quantity)"; $result = $conn->query($query); // TEST when $result===false if($conn->error) exit($conn->error); if(!$result) return false; } return $orderid; } ?>

      The message i got is: Unknown column 'customerid' in 'field list'
      I wrote it like this : 'costumer' instead of customer and the message is:
      Could not store data, please try again.

      What is the problem???

        And the new message is :
        Cannot add or update a child row: a foreign key constraint fails (eshop/customers, CONSTRAINT customers_ibfk_1 FOREIGN KEY (costumerid) REFERENCES customers (costumerid) ON DELETE CASCADE ON UPDATE CASCADE)
        The foreign key seems to be ok in my database...
        Any suggestions?

          Ok it worked after i deleted all indexes and did it again... strange but worked...
          I have a new problem... what means this?
          Column count doesn't match value count at row 1

          <html>
          <head></head>
          <title>Roads Bookstore Entry Results</head>
          
          <body>
          <h1>Roads Bookstore Entry Results</h1>
          <?php
          $isbn=$_POST["isbn"];
          $author=$_POST["author"];
          $title=$_POST["title"];
          $price=$_POST["price"];
          
          
          if (!$isbn || !$author || !$title || !$price)
          {
          	echo "Not Required details.<br>
          	     Please go back and try again.";
          	exit;
          }
          
          if (!get_magic_quotes_gpc())
          {
          	$isbn = addslashes($isbn);
          	$author = addslashes($author);
          	$title = addslashes($title);
          	$price = addslashes($price);
          
          }
          
          $db = new mysqli('localhost','root','','eshop');
          
          if (mysqli_connect_errno())
          {
          	echo "Error:No Access to Database. Try again later.";
          	exit;
          }
          
          $query = "insert into books values
          	 ('".$isbn."','".$title."','".$author."','".$price."')";
          $result = $db->query($query);
           if($db->error) exit($db->error); 
          if ($result)
          	echo $db->affected_rows." book inserted into database.";
          
          
          $db->close();
          
          ?>
          </body>
          </html>
            Write a Reply...