Jump to content

Can't get this working !!


Super-Jay

Recommended Posts

Hey all, so I'm creating a shopping cart and I'm stuck on the very last part, where the customer and order data is placed into the mysql database. My code is below if anyone could help I would be deep fully grateful.

 

]view_cart.php

<?php

$page_title = 'View Your Shopping Cart';
include ('header.php');

// Check if the form has been submitted (to update the cart):
if (isset($_POST['submitted'])) {

// Change any quantities:
foreach ($_POST['qty'] as $k => $v) {

	// Must be integers!
	$pid = (int) $k;
	$qty = (int) $v;

	if ( $qty == 0 ) { // Delete.
		unset ($_SESSION['cart'][$pid]);
	} elseif ( $qty > 0 ) { // Change quantity.
		$_SESSION['cart'][$pid]['quantity'] = $qty;
	}

} // End of FOREACH.
} // End of SUBMITTED IF.

// Display the cart if it's not empty...
if (!empty($_SESSION['cart'])) {

// Retrieve all of the information for the items in the cart:
require_once ('mysqli_connect.php');
$q = "SELECT item_id, CONCAT_WS(' ', manufacture_name) AS manufacture, item_name FROM manufactures, items WHERE manufactures.manufacture_id = items.manufacture_id AND items.item_id IN (";
foreach ($_SESSION['cart'] as $pid => $value) {
	$q .= $pid . ',';
}
$q = substr($q, 0, -1) . ') ORDER BY manufactures.manufacture_name ASC';
$r = mysqli_query ($dbc, $q);

// Create a form and a table:
echo '<form action="view_cart.php" method="post">
<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
	<td align="left" width="30%"><b>Manufacture</b></td>
	<td align="left" width="30%"><b>Item Name</b></td>
	<td align="right" width="10%"><b>Price</b></td>
	<td align="center" width="10%"><b>Qty</b></td>
	<td align="right" width="10%"><b>Total Price</b></td>
</tr>
';

// Print each item...
$total = 0; // Total cost of the order.
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {

	// Calculate the total and sub-totals.
	$subtotal = $_SESSION['cart'][$row['item_id']]['quantity'] * $_SESSION['cart'][$row['item_id']]['price'];
	$total += $subtotal;



	// Print the row.
	echo "\t<tr>
	<td align=\"left\">{$row['manufacture']}</td>
	<td align=\"left\">{$row['item_name']}</td>
	<td align=\"right\">£{$_SESSION['cart'][$row['item_id']]['price']}</td>
	<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['item_id']}]\" value=\"{$_SESSION['cart'][$row['item_id']]['quantity']}\" /></td>
	<td align=\"right\">£" . number_format ($subtotal, 2) . "</td>
</tr>\n";



} // End of the WHILE loop.

mysqli_close($dbc); // Close the database connection.

// Print the footer, close the table, and the form.
echo '<tr>
	<td colspan="4" align="right"><b>Total:</b></td>
	<td align="right">£' . number_format ($total, 2) . '</td>
</tr>
</table>
<div align="center"><input type="submit" name="submit" value="Update My Cart" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form><p align="center">Enter a quantity of 0 to remove an item.
<br /><br /><a href="checkout.php">Checkout</a></p>';

} else {
echo '  <p>Your cart is currently empty.</p>';
}


?>

 

checkout.php

 

<?php

include ('header.php');
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Checkout</title>
</head>


<center>
<form action="final-checkout.php" method="post">


<body>

First Name: <input type="text" name="first_name" />
<br />
<br />
Last Name: <input type="text" name="last_name" />
<br />
<br />
Address Line 1: <input type="text" name="lineone" />
<br />
<br />
Address Line 2: <input type="text" name="linetwo" />
<br />
<br />
Postcode: <input type="text" name="pcode" />
<br />
<br />
Phone Number: <input type="text" name"pnumber" />
<br />

<input type="submit" name="Submit">
</form>

<?

?>
</center>
</body>
</html>

 

final_checkout.php

 

<?php 
$page_title = 'Order Confirmation';
include ('header.php');

$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['lineone'] = $_POST['lineone'];
$_SESSION['linetwo'] = $_POST['linetwo'];
$_SESSION['pcode'] = $_POST['pcode'];
$_SESSION['pnumber'] = $_POST['pnumber'];

$total = $_SESSION['total']; 

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$lineone = $_POST['lineone'];
$linetwo = $_POST['linetwo'];
$pcode = $_POST['pcode'];
$pnumber = $_POST['pnumber'];

require_once ('mysqli_connect.php'); 

// Turn autocommit off.
mysqli_autocommit($dbc, FALSE);

// Add the order to the orders table...
$q = 'INSERT INTO order (first_name, last_name, lineone, linetwo, pcode, pnumber, total) VALUES ("'.$first_name.'", "'.$last_name.'", "'.$lineone.'", "'.$linetwo.'", "'.$pcode.'", "'.$pnumber.'", "'.$total.'")';
$r = mysqli_query($dbc, $q);

if (mysqli_affected_rows($dbc) == 1) {

// Need the order ID:
$oid = mysqli_insert_id($dbc);

// Insert the specific order contents into the database...

// Prepare the query:
$q = "INSERT INTO order_contents1 (order_id, item_id, quantity, price) VALUES (?, ?, ?, ?)";
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 'iiid', $oid, $iid, $qty, $price);

// Execute each query, count the total affected:
$affected = 0;
foreach ($_SESSION['cart'] as $iid => $item) {
	$qty = $item['quantity'];
	$price = $item['price'];
	mysqli_stmt_execute($stmt);
	$affected += mysqli_stmt_affected_rows($stmt);
}

// Close this prepared statement:
mysqli_stmt_close($stmt);

// Report on the success....
if ($affected == count($_SESSION['cart'])) { 

	// Commit the transaction:
	mysqli_commit($dbc);

	// Clear the cart.
	unset($_SESSION['cart']);


	// Message to the customer:
	echo '<p>Thank you for your order. You will be notified when the items ship.</p>';

	// Send emails and do whatever else.

} else { // Rollback and report the problem.
	mysqli_rollback($dbc);

	echo '<p>Your order could not be processed due to a system error. You will be contacted in order to have the problem fixed. We apologize for the inconvenience.</p>';
	// Send the order information to the administrator.

}

} else { // Rollback and report the problem.

mysqli_rollback($dbc);

echo '<p>Your order could not be processed due to a system error. You will be contacted in order to have the problem fixed. We apologize for the inconvenience.</p>';

// Send the order information to the administrator.

}

mysqli_close($dbc);


?>

 

The errors I get are on the final-checkout.php page it states

 

Notice: Undefined index: pnumber in F:\wamp\final-checkout.php on line 10

 

Notice: Undefined index: total in F:\wamp\final-checkout.php on line 18

 

Notice: Undefined index: pnumber in F:\wamp\final-checkout.php  on line 25

 

Below I have listed the mysql database setup incase I made any mistakes.

 

orders1

 

CREATE TABLE orders1 (
order_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
customer_id INT(5) UNSIGNED NOT NULL,
total DECIMAL(10,2) UNSIGNED NOT NULL,
order_date TIMESTAMP,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
lineone VARCHAR(40) NOT NULL,
linetwo VARCHAR(40) NOT NULL,
pcode VARCHAR(12) NOT NULL,
pnumber VARCHAR(11) NOT NULL,
PRIMARY KEY (order_id),
INDEX (customer_id),
INDEX full_name (first_name, last_name),
INDEX (customer_id),
INDEX (order_date)
) ENGINE=InnoDB;

 

order_contents1

 

CREATE TABLE order_contents (
oc_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
order_id INT(10) UNSIGNED NOT NULL,
print_id INT(4) UNSIGNED NOT NULL,
quantity TINYINT UNSIGNED NOT NULL DEFAULT 1,
price DECIMAL(6,2) UNSIGNED NOT NULL,
ship_date DATETIME default NULL,
PRIMARY KEY (oc_id),
INDEX (order_id),
INDEX (print_id)
) ENGINE=InnoDB;

 

 

Forgot to mention that the session_start is stored in the header

Link to comment
https://forums.phpfreaks.com/topic/198656-cant-get-this-working/
Share on other sites

Phone Number: <input type="text" name"pnumber" />

Phone Number: <input type="text" name="pnumber" />

 

(after // End of the WHILE loop. in view_cart.php)

$_SESSION['total']=$total;

 

lol can't belive I missed those two out :(

 

The php errors have  gone,but i'm now get my own error comming up which is

 

Your order could not be processed due to a system error. You will be contacted in order to have the problem fixed. We apologize for the inconvenience.

 

 

before:

mysqli_close($dbc);

put an:

echo mysql_error();

 

No errors produced just same message

Your order could not be processed due to a system error. You will be contacted in order to have the problem fixed. We apologize for the inconvenience.

 

Unless I need to enable the server to report mysql errors? Although I thought that was enabled by default in a wamp installation

Should this:

// Execute each query, count the total affected:
$affected = 0;
foreach ($_SESSION['cart'] as $iid => $item) {
	$qty = $item['quantity'];
	$price = $item['price'];
	mysqli_stmt_execute($stmt);

$affected += mysqli_stmt_affected_rows($stmt);
}

 

update the $stmt variable before executing, filling the values for $qty and $price?

 

if so I would throw this:

$q = "INSERT INTO order_contents1 (order_id, item_id, quantity, price) VALUES (?, ?, ?, ?)";

$stmt = mysqli_prepare($dbc, $q);

mysqli_stmt_bind_param($stmt, 'iiid', $oid, $iid, $qty, $price);

into a function that you pass the $qty and $price as args.

 

Should this:

// Execute each query, count the total affected:
$affected = 0;
foreach ($_SESSION['cart'] as $iid => $item) {
	$qty = $item['quantity'];
	$price = $item['price'];
	mysqli_stmt_execute($stmt);

$affected += mysqli_stmt_affected_rows($stmt);
}

 

update the $stmt variable before executing, filling the values for $qty and $price?

 

if so I would throw this:

$q = "INSERT INTO order_contents1 (order_id, item_id, quantity, price) VALUES (?, ?, ?, ?)";

$stmt = mysqli_prepare($dbc, $q);

mysqli_stmt_bind_param($stmt, 'iiid', $oid, $iid, $qty, $price);

into a function that you pass the $qty and $price as args.

 

This is my first real programming project been learning from a book. How would I implament what your saying there?

No problem.  Go ahead and replace:

$stmt = mysqli_prepare($dbc, $q);

mysqli_stmt_bind_param($stmt, 'iiid', $oid, $iid, $qty, $price);

 

// Execute each query, count the total affected:

$affected = 0;

foreach ($_SESSION['cart'] as $iid => $item) {

$qty = $item['quantity'];

$price = $item['price'];

mysqli_stmt_execute($stmt);

$affected += mysqli_stmt_affected_rows($stmt);

}

 

with:


// Execute each query, count the total affected:
$affected = 0;
foreach ($_SESSION['cart'] as $iid => $item) {


	$qty = $item['quantity'];
	$price = $item['price'];
                 $stmt = mysqli_prepare($dbc, $q);
        	mysqli_stmt_bind_param($stmt, 'iiid', $oid, $iid, $qty, $price);
//              echo $stmt;                                // uncomment this line if it doesnt work at first and re run.
	mysqli_stmt_execute($stmt);
	$affected += mysqli_stmt_affected_rows($stmt);
}

No problem.  Go ahead and replace:

$stmt = mysqli_prepare($dbc, $q);

mysqli_stmt_bind_param($stmt, 'iiid', $oid, $iid, $qty, $price);

 

// Execute each query, count the total affected:

$affected = 0;

foreach ($_SESSION['cart'] as $iid => $item) {

$qty = $item['quantity'];

$price = $item['price'];

mysqli_stmt_execute($stmt);

$affected += mysqli_stmt_affected_rows($stmt);

}

 

with:


// Execute each query, count the total affected:
$affected = 0;
foreach ($_SESSION['cart'] as $iid => $item) {


	$qty = $item['quantity'];
	$price = $item['price'];
                 $stmt = mysqli_prepare($dbc, $q);
        	mysqli_stmt_bind_param($stmt, 'iiid', $oid, $iid, $qty, $price);
//              echo $stmt;                                // uncomment this line if it doesnt work at first and re run.
	mysqli_stmt_execute($stmt);
	$affected += mysqli_stmt_affected_rows($stmt);
}

 

hmm still does not wanna work.

The line that is commented, go ahead and uncomment it now and post the results.  Go ahead and throw a:

echo mysql_error();

after mysqli_stmt_execute($stmt);

 

Catchable fatal error: Object of class mysqli_stmt could not be converted to string in F:\wamp\www\final-checkout.php  on line 54

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.