Jump to content

Shopping Cart issues; some variable isn't passing through


lucius

Recommended Posts

I'm completely new to PHP as of a few weeks ago, so a lot of things still aren't making sense.  I'm going through a book right now and have been doing pretty good at figuring out problems as they've arrived but as I get deeper there's so much more to keep track of.  I've been going through my following codes trying to find the issue, and I believe it is in cart.php but I included the other codes so you can see what all I'm dealing with.

 

I appreciate any help.  I'm sorta stuck since I can't figure this out.

 

Here's my "shop": http://lwrussell.com/phptesting/comicsite/chapt15/cbashop.php

 

Here's my code for cart.php:

<?php

if (!session_id()) {

session_start();

}

require_once 'conn.php';

?>

<html>

<head>

<title>Here is your shopping cart!</title>

</head>

<body>

<div align="center">

You currently have

<?php

$sessid = session_id();

 

//display number of products in cart

$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";

$results = mysql_query($query) or die(mysql_error());

$rows = mysql_num_rows($results);

echo $rows;

?>

 

product(s) in your cart.<br />

 

<table border="1" align="center" cellpadding="5">

<tr>

<td>Quantity</td>

<td>Item Image</td>

<td>Item Name</td>

<td>Price Each</td>

<td>Extended Price</td>

<td></td>

<td></td>

</tr>

<?php

$total = 0;

while ($row = mysql_fetch_array($results)) {

echo "<tr>";

extract($row);

$prod = "SELECT * FROM products WHERE products_prodnum='$carttemp_prodnum'";

$prod2 = mysql_query($prod);

$prod3 = mysql_fetch_array($prod2);

extract($prod3);

echo "<td>

<form method=\"post\" action=\"modcart.php?action=change\">

<input type=\"hidden\" name=\"modified_hidden\" value=\"$carttemp_hidden\">

<input type=\"text\" name=\"modified_quan\" size=\"2\" value=\"$carttemp_quan\">";

echo "</td>";

echo "<td>";

echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";

echo "THUMNAIL<br />IMAGE</a></td>";

echo "<td>";

echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";

echo $products_name;

echo "</td>";

echo "<td aligh=\"right\">";

//get extended price

echo $extprice;

echo "</td>";

echo "<td>";

echo "<input type=\"submit\" name=\"Submit\" value=\"Change Qty\"></form></td>";

echo "<td>";

echo "<form method=\"post\" action=\"modcart.php?action=delete\">

<input type=\"$carttemp_hidden\" name=\"modified_hidden\" value=\"$carttemp_hidden\">";

echo "<input type=\"submit\" name=\"Submit\" value=\"Delete Item\"></form></td>";

echo "</tr>";

//add extended price to total

$total = $extprice + $total;

}

?>

<tr>

<td colspan="4" align="right">

Your total before shipping is:</td>

<td align="right"><?php echo number_format($total, 2); ?></td>

<td></td>

<td>

<?php

echo "<form method=\"post\" action=\"modcart.php?action=empty\">

<input tye=\"hidden\" name=\"carttemp_hidden\" value=\"";

if (isset($carttemp_hidden)) {

echo $carttemp_hidden;

}

echo "\">";

echo "<input type=\"submit\" name=\"Submit\" value=\"Empty Cart\"></form>";

?>

</td>

</tr>

</table>

<form method="post" action="checkout.php">

<input type="submit" name="Submit" value="Proceed to Checkout" />

</form>

<a href="cbashop.php">Go back to the main page</a>

</div>

</body>

</html>

 

Here's my code for modcart.php:

 

<?php

session_start();

require_once 'conn.php';

if (isset($_POST['qty'])) {

  $qty = $_POST['qty'];

}

if (isset($_POST['products_prodnum'])) {

  $prodnum = $_POST['products_prodnum'];

}

if (isset($_POST['modified_hidden'])) {

  $modified_hidden = $_POST['modified_hidden'];

}

if (isset($_POST['modified_quan'])) {

  $modified_quan = $_POST['modified_quan'];

}

$sess = session_id();

$action = $_REQUEST['action'];

 

switch ($action) {

  case "add":

    $query = "INSERT INTO carttemp (

              carttemp_sess,

              carttemp_quan,

              carttemp_prodnum)

              VALUES ('$sess','$qty','$prodnum')";

    $message = "<div align=\"center\"><strong>Item

                added.</strong></div>";

    break;

 

  case "change":

    $query = "UPDATE carttemp

              SET carttemp_quan = '$modified_quan'

            WHERE carttemp_hidden = '$modified_hidden'";

    $message = "<div align='center'><strong>Quantity

                changed.</strong></div>";

    break;

 

  case "delete":

    $query = "DELETE FROM carttemp

              WHERE carttemp_hidden = '$modified_hidden'";

    $message = "<div align='center'><strong>Item

                deleted.</strong></div>";

    break;

 

  case "empty":

    $query = "DELETE FROM carttemp WHERE carttemp_sess = '$sess'";

    $message = "<div align='center'><strong>Cart

                emptied.</strong></div>";

    break;

 

}

 

$results = mysql_query($query)

  or die(mysql_error());

 

echo $message;

 

include("cart.php");

?>

 

And here's my code for getprod.php:

<?php

session_start();

require_once 'conn.php';

 

//get our variable passed through the URL

$prodid = $_REQUEST['prodid'];

 

//get information on the specific product we want

$query = "SELECT * FROM products WHERE products_prodnum='$prodid'";

$results = mysql_query($query)

  or die(mysql_error());

$row = mysql_fetch_array($results);

extract($row);

 

?>

<html>

<head>

<title><?php echo $products_name; ?></title>

</head>

<body>

<div align="center">

<table cellpadding="5" width="80%">

  <tr>

    <td>PRODUCT IMAGE</td>

    <td><strong><?php echo $products_name; ?></strong><br>

      <?php echo $products_proddesc; ?><br />

      <br>Product Number: <?php echo $products_prodnum; ?>

      <br>Price: $<?php echo $products_price; ?><br>

      <form method="POST" action="modcart.php?action=add">

        Quantity: <input type="text" name="qty" size="2"><br>

        <input type="hidden" name="products_prodnum"

          value="<?php echo $products_prodnum; ?>">

        <input type="submit" name="Submit" value="Add to cart">

      </form>

 

      <form method="POST" action="cart.php">

        <input type="submit" name="Submit" value="View cart">

      </form>

    </td>

  </tr>

</table>

<hr width="200">

<p><a href="cbashop.php">Go back to the main page</a></p>

</div>

</body>

</html>

 

 

Thanks for any help!  I deeply appreciate it!

Luke

Link to comment
Share on other sites

Sorry!  I thought I had stated my issue, my apologies.

When you try to add something to the cart, nothing is added.  It is stored in the database with the session id and quantity, but then when I try to call it to the page it shows you have 0 products in your cart.

Link to comment
Share on other sites

session_start();//start session so you can have the value from going to other page

$_SESSION['sample']='your value here';//put the value on the session

 

//now print the

echo $_SESSION['sample'];

//you will have the 'your value here' :-*

Link to comment
Share on other sites

But is that taking place of the session_id?  I'm using the session_id to put in my tables, and I need them unique.  I guess I'm just confused as to what the value is that I'm placing in the brackets of $_SESSION[].

Link to comment
Share on other sites

Would it be something along these lines:

 

session_start();

$carttemp_sess = $_SESSION['carttemp_sess'];

 

Or do I have it all wrong?

 

carttemp_sess is the column name in my table in which I'm wanting to store the session id.

Link to comment
Share on other sites

session_start();

$carttemp_sess = $_SESSION['carttemp_sess'];

 

^^ that is good if you have already assigned the the value for  $_SESSION['carttemp_sess']

 

like you have done this on the previous page

$_SESSION['carttemp_sess']='si teng ako ';

 

 

now on your code the value of $carttemp_sess is si teng ako

Link to comment
Share on other sites

So if I want $_SESSION to have the value of the session id, would I:

 

$_SESSION['sessid'] = session_id();

 

 

I would like to figure out how to make it the value of the session id since I'm putting that into my database. Plus, I figure it's a good thing to know how to do since I intend on implementing such a feature in the near future.  I hate to be wasting your time with a guess and check constant. 

Link to comment
Share on other sites

My apologies for misunderstanding.

 

So for my shopping cart, how do I make it so that I track the current session's additions to their cart?  Right now I use a session id assigned by the browser and store it in a table.  If I set $_SESSION['value'] = 'value'; when I go to access my table for information on the specific session, won't I end up retrieving all the rows in my table?

Link to comment
Share on other sites

i dont get it that much but i think what you mean is an a value for session

 

then you can have

 

$_SESSION['value1']='xxxxxxxxxxxxx';

$_SESSION['value2']='xxxxxxxxxx';

$_SESSION['value3']='vvvvvvvvvv';

 

 

Link to comment
Share on other sites

As is, it assigns each visitor a session_id and so it's unique.  If I set the value of the session, then it won't be unique for each time a person opens their browser and tries adding things to the cart.  It will have the same value as before, and it will have the same value for someone else on a different computer.  That's correct, isn't it?

Link to comment
Share on other sites

My apologies for being slow.

 

I'm confused on the $_SESSION['value'] = 'something';

 

Once I set that, won't 'something' be the value of each session?  I'm lost entirely on how to set this up.  What would be the overall code I would have to do to set that up?

 

How would I use the $_SESSION to give each session a unique value in my table?  I understand the destroy you just mentioned.

 

Again, my apologies for being slow; all of this is so new to me.

Link to comment
Share on other sites

Okay, short lived victory...I didn't realize I was still having the same session for each person no matter where they access it.  How do I have it so it assigns a new session_id for each person who comes and destroy the one when the browser is closed like normal?

 

Do I need to code it to destroy the session?  If so, where do I put it so it does so tactfully and doesn't destroy it while the person is still on the website?

 

Thanks--

luke

Link to comment
Share on other sites

I think I'm starting to see what's going on.

 

So in the check out process, it deletes the current session information in the table.  But that doesn't help if someone doesn't go through the check out process.  How do I delete the information if someone doesn't go through the check out process?

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.