Jump to content

ok, this is weird (Shop/Session problem...)


rootKID

Recommended Posts

Like thescientist said, this is why you're getting an error: $catID = $_GET['cat_id']; You're not using isset to check if cat_id exists before you try to access it. You use isset later down in the code, but that doesn't really matter if you're not using it everywhere. You only need to use it once, when you get cat_id the first time:

if (isset($_GET['cat_id']))  $catID = intval($_GET['cat_id']);else  $catID = 0;

Now you've checked if cat_id exists and you either set it to the integer value of what was passed, or 0 if it wasn't passed. That should be the only time you access $_GET['cat_id'] in your code. Everywhere else in your code you should use $catID instead. If you want to check if it was passed or not, check if $catID is 0. If it's 0 then it wasn't passed.

Link to comment
Share on other sites

hmm... ok, i think i understand now... i have now fixed it all, but cannot add anything to the cart yet... here is shop code:

<?phpecho "<pre>";print_r($_SESSION);echo "</pre>";include ("shop_categories.php");echo "<br />";var_dump($_GET);  /*$catID checking...*/if (isset($_GET['cat_id']))$catID = intval($_GET['cat_id']);else$catID = 0;/*$catID checking...*/if(isset($_GET['cat_id'])){$query = "SELECT * FROM shop_items WHERE FK_catID = " . $_GET['cat_id'];}else {$query = "SELECT * FROM shop_items";}$result = mysql_query($query)or die(mysql_error());/*ECHO THE CAT_ID FOR SEEING IF ANYTHING COMES OUT...*/if(!$catID == ""){echo "Succes! [CAT_ID ECHO]";}else {echo "Noob! [CAT_ID ECHO]";}echo $catID;/*ECHO THE CAT_ID FOR SEEING IF ANYTHING COMES OUT...*/ if(mysql_num_rows($result)>0)//Checking to see if there is any rows to take...{echo "<form action='' method='post'>\n";echo "<table class='shop_table_100'>\n";echo "<tr class='shop_tr_holder_css'>\n";echo "<td>Add</td>\n";echo "<td>Amount</td>\n";echo "<td>Product Name</td>\n";echo "<td>Price</td>\n";echo "<td>More Info</td>\n";echo "</tr>\n";while ($row = mysql_fetch_assoc($result)){echo "<tr>\n";echo "<td><input type='checkbox' name='check_addbox[".$row['item_id']."]' /></td>\n"; ////extra array...echo "<td><input type='text' name='amount[".$row['item_id']."]' size='1' value='1' /></td>\n"; //With extra array...echo "<td>".$row['item_name']."</td>\n";echo "<td>".$row['item_price']." USD</td>\n";echo "<td><a href='shop_details.php?item_id=".$row['item_id']."'>Info</a></td>\n";echo "</tr>\n";}echo "</table>\n";echo "</form>\n"; echo "<form action='' method='post'>\n";echo "<table class='shop_table_100'\n";echo "<tr>\n";echo "<td><input type='submit' name='add_item' id='submit' class='shop_add_submit_button' value='Update Cart' /></td>\n";echo "</tr>\n";echo "</table>\n";echo "</form>\n";}//Ending IF (Num_Rows...)else{echo "<form action='' method='post'>\n";echo "<table class='shop_table_100'>\n";echo "<tr class='shop_tr_holder_css'>\n";echo "<td>ERROR!</td>\n";echo "</tr>\n";echo "</table>\n";echo "</form>\n";echo "<form action='' method='post'>\n";echo "<table class='shop_table_100'>\n";echo "<tr>\n";echo "<td>There are no Products to show yet.</td>\n";echo "</tr>\n";echo "</table>\n";echo "</form>\n";} if(isset($_POST['add_item'])){if(!empty($_POST['check_addbox'])){  foreach($_POST['check_addbox'] as $p_id => $check)  {   $amount = $_POST['amount'][$p_id]; //Getting the Amount from the box... [p_id] is indicating what product it is (the checkbox...)   $_SESSION['cart'][$p_id] += $amount; //adding them to the session...   header("location: shop.php");  }}}?>

Link to comment
Share on other sites

Guest So Called
if (isset($_GET['cat_id']))  $catID = intval($_GET['cat_id']);else  $catID = 0;

Or this:
$catID = isset($_GET['cat_id']) ? $_GET['cat_id'] : 0;

I like things simple.

Link to comment
Share on other sites

hmm... ok ;)...

Link to comment
Share on other sites

Why are you still using $_GET['cat_id'] here: if(isset($_GET['cat_id'])){$query = "SELECT * FROM shop_items WHERE FK_catID = " . $_GET['cat_id'];}else {$query = "SELECT * FROM shop_items";}$result = mysql_query($query)or die(mysql_error()); Like I said before, the only time you access $_GET['cat_id'] is when you set the $catID variable. After that, you only use $catID. If you want to know if it was sent in the URL then check if the value is 0, like you see in the if statement that checks for it if it isn't found then it gets set to 0. Also, what is the value of $catID when you print it out later in the code?

Link to comment
Share on other sites

Guest So Called

Simple code is easier to read, easier to understand, easier to debug and easier to maintain. Of course it's a matter of opinion as to what's simpler.

Link to comment
Share on other sites

sorry jsg... did not see that part as a must add... but added now, and getting those errors: Notice: Undefined index: cat_id in C:\xampp\htdocs\shop\shop.php on line 97You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Here are code... tell me... i did insert it right this time, right?:

<?phpecho "<pre>";print_r($_SESSION);echo "</pre>"; include ("shop_categories.php");echo "<br />";var_dump($_GET);   /*$catID checking...*/if (isset($_GET['cat_id'])){$catID = intval($_GET['cat_id']);}else{$catID = 0;}/*$catID = isset($_GET['cat_id']) ? $_GET['cat_id'] : 0;*//*$catID checking...*/if(isset($catID)){$query = "SELECT * FROM shop_items WHERE FK_catID = " . $_GET['cat_id'];}else {$query = "SELECT * FROM shop_items";}$result = mysql_query($query)or die(mysql_error()); /*ECHO THE CAT_ID FOR SEEING IF ANYTHING COMES OUT...*/if(!$catID == ""){echo "Succes! [CAT_ID ECHO]";}else {echo "Noob! [CAT_ID ECHO]";}echo $catID;/*ECHO THE CAT_ID FOR SEEING IF ANYTHING COMES OUT...*/  if(mysql_num_rows($result)>0)//Checking to see if there is any rows to take...{echo "<form action='' method='post'>\n";echo "<table class='shop_table_100'>\n";echo "<tr class='shop_tr_holder_css'>\n";echo "<td>Add</td>\n";echo "<td>Amount</td>\n";echo "<td>Product Name</td>\n";echo "<td>Price</td>\n";echo "<td>More Info</td>\n";echo "</tr>\n"; while ($row = mysql_fetch_assoc($result)){echo "<tr>\n";echo "<td><input type='checkbox' name='check_addbox[".$row['item_id']."]' /></td>\n"; ////extra array...echo "<td><input type='text' name='amount[".$row['item_id']."]' size='1' value='1' /></td>\n"; //With extra array...echo "<td>".$row['item_name']."</td>\n";echo "<td>".$row['item_price']." USD</td>\n";echo "<td><a href='shop_details.php?item_id=".$row['item_id']."'>Info</a></td>\n";echo "</tr>\n";}echo "</table>\n";echo "</form>\n"; echo "<form action='' method='post'>\n";echo "<table class='shop_table_100'\n";echo "<tr>\n";echo "<td><input type='submit' name='add_item' id='submit' class='shop_add_submit_button' value='Update Cart' /></td>\n";echo "</tr>\n";echo "</table>\n";echo "</form>\n"; }//Ending IF (Num_Rows...)else{echo "<form action='' method='post'>\n";echo "<table class='shop_table_100'>\n"; echo "<tr class='shop_tr_holder_css'>\n";echo "<td>ERROR!</td>\n";echo "</tr>\n"; echo "</table>\n";echo "</form>\n";    echo "<form action='' method='post'>\n";echo "<table class='shop_table_100'>\n"; echo "<tr>\n";echo "<td>There are no Products to show yet.</td>\n";echo "</tr>\n"; echo "</table>\n";echo "</form>\n";}  if(isset($_POST['add_item'])){if(!empty($_POST['check_addbox'])){  foreach($_POST['check_addbox'] as $p_id => $check)  {   $amount = $_POST['amount'][$p_id]; //Getting the Amount from the box... [p_id] is indicating what product it is (the checkbox...)   $_SESSION['cart'][$p_id] += $amount; //adding them to the session...   header("location: shop.php");  }}}?>

EDIT: im going to bed for today, im going up early tomorrow... asking my teachers about this tomorrow also... maybe they can help me on the road again... in anyway, ill post a feedback tomorrow if they did not. and well, if they did... im still going to post feedback ;)... see ya tomorrow ;)...Regards... (From Cellphone...)

Edited by rootKID
Link to comment
Share on other sites

Like JSG said... set $catID ONCE and then use it. You still have stuff like this in your code.

if(isset($catID)){  $query = "SELECT * FROM shop_items WHERE FK_catID = " . $_GET['cat_id'];}else {  $query = "SELECT * FROM shop_items";}$result = mysql_query($query)or die(mysql_error());

He said to check for the value and use $catID accordingly

if($catID != 0){  $query = "SELECT * FROM shop_items WHERE FK_catID = " . $catID;}else {  $query = "SELECT * FROM shop_items";}$result = mysql_query($query)or die(mysql_error());

just use $catID. I would have figured by now you would understand the undefined index error, because the cause has and solution been the same everytime.

Edited by thescientist
Link to comment
Share on other sites

EDIT: lol...we have located the problem... it was the submit place... i have created 2 forms... so the submit in a form for itself, in that case it did not submit the other form with the products -.-'... so case closed :)...

Edited by rootKID
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...