Jump to content

function


westminster86

Recommended Posts

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :-\

Link to comment
Share on other sites

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 :'(

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.