lucius Posted June 29, 2007 Share Posted June 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
aim25 Posted June 29, 2007 Share Posted June 29, 2007 Okay im just writing as i go on, but u have to use the session_start() before any page that uses sessions, so just put session_start() at the top. Quote Link to comment Share on other sites More sharing options...
aim25 Posted June 29, 2007 Share Posted June 29, 2007 And could you plzz state your problem, it'd help. Quote Link to comment Share on other sites More sharing options...
lucius Posted June 29, 2007 Author Share Posted June 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 i dont see something like this $_SESSION['value here']=somethinghere; do you have something like that Quote Link to comment Share on other sites More sharing options...
lucius Posted June 29, 2007 Author Share Posted June 29, 2007 No, I'm not too good with sessions. I've been thrown so much information about PHP these past couple weeks that I'm having a hard time with that. What do I need to do to access the session/call the session_id? Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 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' Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 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[]. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 for now yes. coz that will be another thing to learn Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 right now you dont need that Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 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? Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 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'; Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 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? Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 session_destroy(); use that to reset the session Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 session is an array $_SESSION['value'] = 'something'; //value is index// and if you talk about array you can have many index having diff value so unique? maybe if u have diff index VALUE Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 I finally figured it out. I needed to simply change: $sessid = session_id(); to: $sessid = $_SESSION['session_id']; Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 ya Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
lucius Posted June 30, 2007 Author Share Posted June 30, 2007 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.