Hi,
I'm working on a PHP shopping cart, but I'm having some troubles getting all the items in the users cart to insert into a receipts table.
Currently, only one item in the cart will insert into the receipts table, (i.e. if there are 5 items in my cart, only the first one is inserted in the receipts table). The goal is to have each item in the cart insert into the receipts table as a unique record (i.e. if there are 5 items in my cart, there should be 5 records inserted into the receipts table.
Below is a portion of the code that I am currently using.
$query = "SELECT * FROM cartitems WHERE Userid='$UserID'";
$result = mysql_query($query, $db_conn);
while ($row = mysql_fetch_row($result)) {
$CUI=$row[0];
$CII=$row[1];
$CIQ=$row[2];
$CDa=$row[3];
$CCI=$row[4];
$CAT=$row[5];
$query = "SELECT * FROM items WHERE ItemID='$CII'";
$result2 = mysql_query($query, $db_conn);
while ($row2 = mysql_fetch_row($result2)) {
$IIS=$row2[0];
$IIN=$row2[1];
$IID=$row2[2];
$IIC=$row2[3];
$ICa=$row2[4];
$ISC=$row2[5];
$III=$row2[6];
$To[$i]=(($CIQ * $IIC)+($CIQ * $ISC));
$query = "INSERT INTO receipts VALUES ";
$query .= "('$BID','$UserID','$ONu','$Date','$III','$IIS','$IIN','$IIC','$ISC','$CIQ','$To[$i]','$CAT')";
$result = mysql_query($query, $db_conn);
while ($row = mysql_fetch_row($result))
$i++;
}
}
Can anyone help?
Please note: Some of the variables ($BID, $UserID, $ONu, and $Date) are defined elsewhere. If you need more of the code, I will gladly post it.
Thanks!Incase anyone was curious, I figured out a solution.
I had to rename the last $result variable.
The code below works now.
$query = "SELECT * FROM cartitems WHERE Userid='$UserID'";
$result = mysql_query($query, $db_conn);
while ($row = mysql_fetch_row($result)) {
$CUI=$row[0];
$CII=$row[1];
$CIQ=$row[2];
$CDa=$row[3];
$CCI=$row[4];
$CAT=$row[5];
$query = "SELECT * FROM items WHERE ItemID='$CII'";
$result2 = mysql_query($query, $db_conn);
while ($row2 = mysql_fetch_row($result2)) {
$IIS=$row2[0];
$IIN=$row2[1];
$IID=$row2[2];
$IIC=$row2[3];
$ICa=$row2[4];
$ISC=$row2[5];
$III=$row2[6];
$To[$i]=(($CIQ * $IIC)+($CIQ * $ISC));
$query = "INSERT INTO receipts VALUES ";
$query .= "('$BID','$UserID','$ONu','$Date','$III','$IIS','$IIN','$IIC','$ISC','$CIQ','$To[$i]','$CAT')";
$result3 = mysql_query($query, $db_conn);
while ($row = mysql_fetch_row($result3))
$i++;
}
}actually you don't even need it. also don't need the while thre either.
$query = "INSERT INTO receipts VALUES ";
$query .= "('$BID','$UserID','$ONu','$Date','$III','$IIS','$IIN','$IIC','$ISC','$CIQ','$To[$i]','$CAT')";
mysql_query($query, $db_conn);
that works just fineOrginally I had this, which I thought should work but it didn't.
$query = "INSERT INTO receipts VALUES ";
$query .= "('$BID','$UserID','$ONu','$Date','$III','$IIS','$IIN','$IIC','$ISC','$CIQ','$To[$i]','$CAT')";
$result = mysql_query($query, $db_conn);
I appreciate your help and will go with your recommendation.
Say, do you know a good PHP book? I've been using a PHP Bible, but it's a bit dated which has caused me some troubles.
Thanks again!well you really didn't even need the variable there at all. I know that $result was messing it up, either rename it or take it out. as you don't need it I would just take it out.
as far as good books, hmmm with all the changes you would have to look for one that includes php3.x
jsut a few brand names come in mind. Wrox and O'rielly (sp). they usually have good qaulity info in them.I'll have to head to the bookstore this weekend. Right now, you're the best source of knowledge on PHP that I've found! With all my coding blunders that's not a good person to be!
Hopefully a good book or two with help.
Thanks again Scoutt!
I'm working on a PHP shopping cart, but I'm having some troubles getting all the items in the users cart to insert into a receipts table.
Currently, only one item in the cart will insert into the receipts table, (i.e. if there are 5 items in my cart, only the first one is inserted in the receipts table). The goal is to have each item in the cart insert into the receipts table as a unique record (i.e. if there are 5 items in my cart, there should be 5 records inserted into the receipts table.
Below is a portion of the code that I am currently using.
$query = "SELECT * FROM cartitems WHERE Userid='$UserID'";
$result = mysql_query($query, $db_conn);
while ($row = mysql_fetch_row($result)) {
$CUI=$row[0];
$CII=$row[1];
$CIQ=$row[2];
$CDa=$row[3];
$CCI=$row[4];
$CAT=$row[5];
$query = "SELECT * FROM items WHERE ItemID='$CII'";
$result2 = mysql_query($query, $db_conn);
while ($row2 = mysql_fetch_row($result2)) {
$IIS=$row2[0];
$IIN=$row2[1];
$IID=$row2[2];
$IIC=$row2[3];
$ICa=$row2[4];
$ISC=$row2[5];
$III=$row2[6];
$To[$i]=(($CIQ * $IIC)+($CIQ * $ISC));
$query = "INSERT INTO receipts VALUES ";
$query .= "('$BID','$UserID','$ONu','$Date','$III','$IIS','$IIN','$IIC','$ISC','$CIQ','$To[$i]','$CAT')";
$result = mysql_query($query, $db_conn);
while ($row = mysql_fetch_row($result))
$i++;
}
}
Can anyone help?
Please note: Some of the variables ($BID, $UserID, $ONu, and $Date) are defined elsewhere. If you need more of the code, I will gladly post it.
Thanks!Incase anyone was curious, I figured out a solution.
I had to rename the last $result variable.
The code below works now.
$query = "SELECT * FROM cartitems WHERE Userid='$UserID'";
$result = mysql_query($query, $db_conn);
while ($row = mysql_fetch_row($result)) {
$CUI=$row[0];
$CII=$row[1];
$CIQ=$row[2];
$CDa=$row[3];
$CCI=$row[4];
$CAT=$row[5];
$query = "SELECT * FROM items WHERE ItemID='$CII'";
$result2 = mysql_query($query, $db_conn);
while ($row2 = mysql_fetch_row($result2)) {
$IIS=$row2[0];
$IIN=$row2[1];
$IID=$row2[2];
$IIC=$row2[3];
$ICa=$row2[4];
$ISC=$row2[5];
$III=$row2[6];
$To[$i]=(($CIQ * $IIC)+($CIQ * $ISC));
$query = "INSERT INTO receipts VALUES ";
$query .= "('$BID','$UserID','$ONu','$Date','$III','$IIS','$IIN','$IIC','$ISC','$CIQ','$To[$i]','$CAT')";
$result3 = mysql_query($query, $db_conn);
while ($row = mysql_fetch_row($result3))
$i++;
}
}actually you don't even need it. also don't need the while thre either.
$query = "INSERT INTO receipts VALUES ";
$query .= "('$BID','$UserID','$ONu','$Date','$III','$IIS','$IIN','$IIC','$ISC','$CIQ','$To[$i]','$CAT')";
mysql_query($query, $db_conn);
that works just fineOrginally I had this, which I thought should work but it didn't.
$query = "INSERT INTO receipts VALUES ";
$query .= "('$BID','$UserID','$ONu','$Date','$III','$IIS','$IIN','$IIC','$ISC','$CIQ','$To[$i]','$CAT')";
$result = mysql_query($query, $db_conn);
I appreciate your help and will go with your recommendation.
Say, do you know a good PHP book? I've been using a PHP Bible, but it's a bit dated which has caused me some troubles.
Thanks again!well you really didn't even need the variable there at all. I know that $result was messing it up, either rename it or take it out. as you don't need it I would just take it out.
as far as good books, hmmm with all the changes you would have to look for one that includes php3.x
jsut a few brand names come in mind. Wrox and O'rielly (sp). they usually have good qaulity info in them.I'll have to head to the bookstore this weekend. Right now, you're the best source of knowledge on PHP that I've found! With all my coding blunders that's not a good person to be!
Hopefully a good book or two with help.
Thanks again Scoutt!