westminster86 Posted January 5, 2008 Share Posted January 5, 2008 The line, if(insert_order($_POST)!=false) is evaluating to false for some reason. There maybe something wrong with the function. Any ideas. <?php // The shopping cart needs sessions, so start one session_start(); do_html_header('Checkout'); // create short variable names $title = $_POST['title']; $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $address = $_POST['address']; $city = $_POST['city']; $postcode = $_POST['postcode']; $country = $_POST['country']; // if filled out if($_SESSION['cart']&&$title&&$firstname&&$lastname&&$address&&$city&&$postcode&&$country) { // able to insert into database if(insert_order($_POST)!=false) { // display cart, not allowing changes and without pictures display_cart($_SESSION['cart'], false, 0); display_shipping(10); // get credit card details display_card_form($name); display_button('showCart.php', 'continue-shopping', 'Continue Shopping'); } else { echo 'Could not store data, please try again.'; echo $title; echo $firstname; echo $lastname; echo $address; echo $city; echo $postcode; echo $country; display_button('checkout.php', 'back', 'Back'); } } else { echo 'You did not fill in all the fields, please try again.<hr />'; display_button('checkout.php', 'back', 'Back'); } do_html_footer(); function insert_order($order_details) { // extract order_details out as variables extract($order_details); $shiptitle = $_POST['shiptitle']; $shipfirstname = $_POST['shipfirstname']; $shiplastname = $_POST['shiplastname']; $shipaddress = $_POST['shipaddress']; $shipcity = $_POST['shipcity']; $shippostcode = $_POST['shippostcode']; $shipcountry = $_POST['shipcountry']; // set shipping address same as address if(!$shiptitle&&!$shipfirstname&&!$shiplastname&&!$shipaddress&&!$shipcity&&!$shippostcode&&!$shipcountry) { $shiptitle = $title; $shipfirstname = $firstname; $shiplastname = $lastname; $shipaddress = $address; $shipcity = $city; $shippostcode = $postcode; $shipcountry = $country; } @ $db = mysql_connect(''); mysql_select_db(''); // we want to insert the order as a transaction // start one by turning off autocommit //$db->autocommit(FALSE); // insert customer address $query = "select customerid from customers where title ='$title' and firstname = '$firstname' and lastname = '$lastname' and address = '$address' and city = '$city' and postcode = '$postcode' and country = '$country'"; $result = mysql_query($query); if(mysql_num_rows($result)>0) { $customer = $mysql_fetch_object($result); $customerid = $customer->customerid; } else { $query = "insert into customer values ('', '$title', '$firstname', '$lastname', '$address', '$city', '$postcode', '$country')"; $result = mysql_query($query); if (!$result) return false; } $customerid = $db->insert_id; $date = date('Y-m-d'); $query = "insert into orders values ('', $customerid, ".$_SESSION['total_price'].", '$date', '$shiptitle', '$shipfirstname', '$shiplastname', '$shipaddress', '$shipcity', '$shippostcode', '$shipcountry')"; $result = mysql_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 shiptitle = '$shiptitle' and shipfirstname = '$shipfirstname' and shiplastname = '$shiplastname' and shipaddress = '$shipaddress' and shipcity = '$shipcity' and shippostcode = '$shippostcode' and shipcountry = '$shipcountry'"; $result = mysql_query($query); if(mysl_num_rows($result)>0) { $order = $result->fetch_object(); $orderid = $order->orderid; } else return false; // insert each product foreach($_SESSION['cart'] as $productid => $quantity) { $detail = get_product_details($productid); $query = "delete from order_items where orderid = '$orderid' and productid = '$productid'"; $result = mysql_query($query); $query = "insert into order_items values ('$orderid', '$productid', ".$detail['price'].", $quantity, 1)"; $result = mysql_query($query); if(!result) return false; } // end transaction $db->commit(); $db->autocommit(TRUE); return $orderid; }?> (edited to insert tags) Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/ Share on other sites More sharing options...
hitman6003 Posted January 5, 2008 Share Posted January 5, 2008 if $orderid returned == 0, that evaluates to false...use ===, and also double check you are getting the expected return values (i.e. a positve integer) Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431138 Share on other sites More sharing options...
Barand Posted January 5, 2008 Share Posted January 5, 2008 Whe inserting you need to write null to an auto_increment id field, not ''. Change to $query = "insert into orders values (null, $customerid, .... Why dont you just use insert_id() again to get the new orderid ? Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431327 Share on other sites More sharing options...
westminster86 Posted January 5, 2008 Author Share Posted January 5, 2008 Whe inserting you need to write null to an auto_increment id field, not ''. Change to $query = "insert into orders values (null, $customerid, .... Why dont you just use insert_id() again to get the new orderid ? i dnt think it really matters... '' works just as good as Null does. But i did try changing it around but its echoing the else statement could not store data. Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431378 Share on other sites More sharing options...
john010117 Posted January 5, 2008 Share Posted January 5, 2008 First problem I saw: use: if(insert_order($_POST)!==false) instead of: if(insert_order($_POST)!=false) Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431379 Share on other sites More sharing options...
westminster86 Posted January 5, 2008 Author Share Posted January 5, 2008 First problem I saw: use: if(insert_order($_POST)!==false) instead of: if(insert_order($_POST)!=false) yeh i changed that around... still no joy if i was to change it to !=true then it works fine, but then none of the values get added to the database. Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431382 Share on other sites More sharing options...
Barand Posted January 5, 2008 Share Posted January 5, 2008 There are several places in that function that can potentially return false. First step is to establish which one, then you can look at why. Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431383 Share on other sites More sharing options...
westminster86 Posted January 5, 2008 Author Share Posted January 5, 2008 There are several places in that function that can potentially return false. First step is to establish which one, then you can look at why. yeh im doing that now. looking at each line. probably missing a comma or something somwhere Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431385 Share on other sites More sharing options...
westminster86 Posted January 5, 2008 Author Share Posted January 5, 2008 There are several places in that function that can potentially return false. First step is to establish which one, then you can look at why. yeh im doing that now. looking at each line. probably missing a comma or something somwhere can anyone see anything wrong here, cos i been looking for past 2 hours and i just cnt see nething :-\ Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431452 Share on other sites More sharing options...
westminster86 Posted January 5, 2008 Author Share Posted January 5, 2008 There are several places in that function that can potentially return false. First step is to establish which one, then you can look at why. yeh im doing that now. looking at each line. probably missing a comma or something somwhere can anyone see anything wrong here, cos i been looking for past 2 hours and i just cnt see nething :-\ ok found couple things that i missed out.. missed a few dollar signs and got one of my tabl names wrong but still isnt working....YET :'( Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431457 Share on other sites More sharing options...
Barand Posted January 5, 2008 Share Posted January 5, 2008 You have a hypothetical function which returns false at several places <?php function myFunction() { $res = doSomething(); if (!$res) return false; $res = doSomethingElse(); if (!$res) return false; $res = doAnother(); if (!$res) return false; return true; } $result = myFunction(); Question: What can you do to determine at which point the function is returning false? Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431463 Share on other sites More sharing options...
westminster86 Posted January 5, 2008 Author Share Posted January 5, 2008 You have a hypothetical function which returns false at several places <?php function myFunction() { $res = doSomething(); if (!$res) return false; $res = doSomethingElse(); if (!$res) return false; $res = doAnother(); if (!$res) return false; return true; } $result = myFunction(); Question: What can you do to determine at which point the function is returning false? im just using the function that ive got out of a book and ive changed a few things around. Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431464 Share on other sites More sharing options...
westminster86 Posted January 5, 2008 Author Share Posted January 5, 2008 You have a hypothetical function which returns false at several places <?php function myFunction() { $res = doSomething(); if (!$res) return false; $res = doSomethingElse(); if (!$res) return false; $res = doAnother(); if (!$res) return false; return true; } $result = myFunction(); Question: What can you do to determine at which point the function is returning false? im just using the function that ive got out of a book and ive changed a few things around. ive checked my tables and its adding the customer details so im guessing thats its returning false somewher after these statements Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431491 Share on other sites More sharing options...
westminster86 Posted January 6, 2008 Author Share Posted January 6, 2008 You have a hypothetical function which returns false at several places <?php function myFunction() { $res = doSomething(); if (!$res) return false; ok its now adding values to the orders table so there something wrong with my insert statement to the order_items table $res = doSomethingElse(); if (!$res) return false; $res = doAnother(); if (!$res) return false; return true; } $result = myFunction(); Question: What can you do to determine at which point the function is returning false? im just using the function that ive got out of a book and ive changed a few things around. ive checked my tables and its adding the customer details so im guessing thats its returning false somewher after these statements Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431772 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 Try changing the function so that, prior to returning false, it echos some information so we can see WHERE it is returning false. Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431779 Share on other sites More sharing options...
westminster86 Posted January 6, 2008 Author Share Posted January 6, 2008 Try changing the function so that, prior to returning false, it echos some information so we can see WHERE it is returning false. didnt think of tht... thanks Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431781 Share on other sites More sharing options...
westminster86 Posted January 6, 2008 Author Share Posted January 6, 2008 Try changing the function so that, prior to returning false, it echos some information so we can see WHERE it is returning false. didnt think of tht... thanks ok ive found the statements in the function that are casuing the function to return false You will see that different parts of the insert are enclosed in a transaction beginning with $db->autocommit(FALSE) and ending with $db->commit(); $db->autocommit(TRUE); the message returned is... Fatal error: Call to a member function on a non-object what am i doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/84604-function/#findComment-431807 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.