Hello everyone this is my first post on stack overflow.com
I am trying to create shopping cart. Values are being stored in a session in two dimension table. First value represents item id in database, second – quantity of item.
foreach($cart_array as $row){
$sql = "SELECT * FROM prod_table WHERE id=".$row['0']."";
$cart_result = mysql_query($sql, $conn);
while ($array = mysql_fetch_array($cart_result)) {
echo "<tr><td>". $array[manufacturer] . "</td>";
echo "<td>". $array[model]."</td>";
echo "<td>".$row['1']."</td>";
$cart_value = $array[price]*$row['1'];
//sum and store value of all products
$total_cart_value += $cart_value;
echo "<td>£" .$cart_value. "</td>";
echo "<td><a href='search.php?kick_id=".$row['0']."'>Remove</a></td></tr>";
OK, to remove item form cart user clicks remove and then he is being send back to the same page ( for some reason I had difficulty to use $_SERVER[‘PHP_SELF’} with GET method… )
and then different part of code is being triggered which is going to remove pair of values from $cart_array, using unset function.
if(isset($_GET['kick_id'])) {
$t = count($cart_array);
for( $u = 0; $u < $t; $u++){
if ($cart_array[$u]['0'] == $_GET['kick_id']){
unset($cart_array[$u]['0']);
unset($cart_array[$u]['1']);
echo " Item has been removed from your cart";
$cart_array = array_values($cart_array);
And it actually removes pair of values but, now instead of running smoothly it displays error message :
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource …
I assume that there must be some “trace” of deleted values in session array which is causing error.
How to fix code ?
Is there any other method/approach to delete values ??
that is what you are doing wrong.
A programmer should never “assume”. A programmer can (and should) be certain.
Assumptions will lead you astray and waste your time (as well as other people’s time when you ask a question).
So, you have to do some debugging – an investigation to find the actual cause of the problem.
supplied argument is not a valid MySQL result resourceerror means there was an error in the query. So, you have to see what error it was. So, run all yoiur queries in the manner which let you to see any error occurred. A simplest way would beand run your code again.
you will find an sql error in their usual place – a screen or a log file.
If you’ll be unable to get the meaning of the error and correct your code yourself – it’s time to ask a question. A certain and direct question, not vague question out of some assumptions. Gotcha?