Page 2 of 2
Posted: Wed Jun 16, 2004 1:34 pm
by dardsemail
I've been working forever on this silly code and I'm closer now - but not quite there yet.
I manage to get the data result from the query in one big jumble at the top of my results page - but not in the table that I thought I was placing it in. Here's the code:
Code: Select all
<?php
function ShowCart()
{
$hostname = 'localhost';
$username ='abc';
$password ='123;
$dbName = 'mydb_db';
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect");
@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database");
$result = mysql_query("SELECT * FROM cart INNER JOIN pieces ON cart.piecenum=pieces.piecenum WHERE cookieID='" . GetCartId() . "'") or die(mysql_error());
error_reporting (E_ALL ^ E_NOTICE);
while($row = mysql_fetch_array($result))
{?>
<tr>
<td width="15%" height="25"></td>
<td width="55%" height="25"><?php echo $row['description']; ?></td>
<td width="20%" height="25"><?php echo $row['price']; ?></td>
<td width="10%" height="25"></td>
</tr>
?>
This code is within a table in the page. I'm finally error free - now if I could just get the code to display in the right section of the page, I'd be home free!
Can someone please help?
Posted: Wed Jun 16, 2004 1:39 pm
by feyd
since you are trying to format your results there.. store them into a variable. then tell that variable to echo out..
Posted: Thu Jun 17, 2004 9:17 pm
by dardsemail
Okay - once again I'm mucking with my code. I've now broken the various functions into their own PHP files. So far, I can view the data, but I'm having problems with the additem.php. I've listed the code below:
Code: Select all
<?php
ob_start();
// This script adds items to the shopping cart. It expects the piecenum and qty to be added
include ("session.php");
error_reporting (E_ALL);
$piecenum = ($_GET["piecenum"]);
$qty = ($_GET["qty"]);
//Open a database connection
$hostname = 'localhost';
$username ='abc';
$password ='123';
$dbName = 'mydb_db';
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect");
@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database");
// Check if this item already exists in the users cart table
$query = ("SELECT * FROM cart WHERE cookieID = '" . GetCartId() . "'");
$result = mysql_query ($query) or die("ERROR, ERROR!".mysql_error());
if (mysql_num_rows($result)>0)
{
$row=mysql_fetch_array($result);
mysql_query("INSERT INTO cart(cookieID, piecenum, qty) VALUES('" . GetCartId() . "', $piecenum, $qty)");
}
else
{
mysql_query("UPDATE cart SET qty = $qty WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
}
//Redirect the user to the cart page
header("Location:cart.php");
?>
The piecenum & qty are passed as part of the URL from a 'collections' page. Here is the URL structure from that page:
Code: Select all
<a href="additem.php?piecenum=<?php echo $row_rs_pieceinfoї'piecenum']; ?>&qty=1">Add Item</a>
When I run the code through the debugger, I get an undefined index error on the piecenum and qty variables. What am I doing wrong? I thought I had them.
When I try and add items to the cart, nothing is being added - though I am accurately being redirected. I should also note that GetCartID is the name of the function contained within the session.php file.
Posted: Fri Jun 18, 2004 3:00 pm
by dardsemail
Can someone please help? I'm lost in a sea of code and can't figure it out for the life of me...
Here's my code:
Code: Select all
<?php
ob_start();
// This script adds items to the shopping cart. It expects the piecenum and qty to be added
include ("session.php");
error_reporting (E_ALL);
//Open a database connection
$hostname = 'localhost';
$username ='abc';
$password ='123';
$dbName = 'mydb_db';
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect");
@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database");
$action = $_GET["action"];
if ($action=="additem") AddItem();
function AddItem($piecenum, $qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
// Check if this item already exists in the users cart table
$result = mysql_query("SELECT count(*) FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
@mysql_query("INSERT INTO cart(cookieID, piecenum, qty) VALUES('" . GetCartId() . "', $piecenum, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($piecenum, $qty);
}
}
//Redirect the user to the cart page
header("Location:cart.php");
?>
I'm getting an
undefined index error on action. Here is the URL structure from the page where the function is called:
Code: Select all
<a href="additem.php?action=additem&piecenum=<?php echo $row_rs_pieceinfoї'piecenum']; ?>&qty=1">Add Item</a>
When I run the code, I get to the cart.php page, but no items are added. Help!!!