Hello All - I am working on a simple shopping cart and i am having trouble w/ the checking out function?
The Checkout is where you enter you Name, Address,etc...The Problem is - when I submit the info, It is stopping me at - "Could Not Store Data, Please try Again" but when I check my databse - it is there. I am kind of lost. Mabye I am missing something simple? I will post my code:
checkout.php:
<?php
//include our function set
include ('book_sc_fns.php');
// The shopping cart needs sessions, so start one
session_start();
do_html_header('Checkout');
if($_SESSION['cart']&&array_count_values($_SESSION['cart']))
{
display_cart($_SESSION['cart'], false, 0);
display_checkout_form();
}
else
echo '<p>There are no items in your cart</p>';
display_button('show_cart.php', 'continue-shopping', 'Continue Shopping');
do_html_footer();
?>
Code that Processes Order:
<?php
function process_card($card_details)
{
// connect to payment gateway or
// use gpg to encrypt and mail or
// store in DB if you really want to
return true;
}
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 = db_connect();
// insert customer address
$query = "
SELECT customerid
FROM customers
WHERE name = '$name' and address = '$address'
and city = '$city' and state = '$state'
and zip = '$zip' and country = '$country'";
$result = mysql_query($query, $conn)
or die(mysql_error());
if($result->num_rows>0)
{
$customer = mysql_fetch_object($result);
$customerid = $customer->customerid;
}
else
{
$query = "
INSERT INTO customers
VALUES ('', '$name','$address','$city','$state','$zip','$country')";
$result = mysql_query($query, $conn)
or die(mysql_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 = mysql_query($query, $conn);
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 = mysql_query($query, $conn);
if($result->num_rows>0)
{
$order = mysql_fetch_object($result);
$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 = mysql_query($query,$conn);
$query = "insert into order_items values
('$orderid', '$isbn', ".$detail['price'].", $quantity)";
$result = mysql_query($query, $conn);
if(!$result)
return false;
}
return $orderid;
}
?>
And here is the Purchase script that tell me the data is not passing:
<?php
include ('book_sc_fns.php');
// The shopping cart needs sessions, so start one
session_start();
do_html_header("Checkout");
// create short variable names
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
// if filled out
if($_SESSION['cart']&&$name&&$address&&$city&&$zip&&$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(calculate_shipping_cost());
//get credit card details
display_card_form($name);
display_button('show_cart.php', 'continue-shopping', 'Continue Shopping');
}
else
{
echo 'Could not store data, please try again.';
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();
?>
Any help or insight would be much appreciate - i have been searching & reading for awhile and not finding any real solution. Thanks in advance!
Best.
j.