Eliminar el artículo usando unset () del carrito de compras crea un error de mysql

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>&pound;" .$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 ??

preguntado el 08 de noviembre de 11 a las 13:11

4 Respuestas

You don't appear to have unset($cart_array[$u]), just the two keys it contains. Don't bother unsetting $cart_array[$u]['0'] y '1', just unset $cart_array[$u].

respondido 08 nov., 11:17

Hay dos formas de solucionar esto:

1) on your first part:

foreach($cart_array as $row){
  if(empty($row[0])
    continue;
  $sql = "SELECT * FROM prod_table WHERE id=".$row['0']."";
  [...]

2) when you remove the item, instead of unset($cart_array[$u]['0']); why don't you do unset($cart_array[$u]); ?

respondido 08 nov., 11:17

Probablemente esto

while ($array = mysql_fetch_array($cart_result)) {

is causing the problem. based on error you see. before you try fetch result check if there is any results available by

if(mysql_num_rows($cart_result)>0){
  while ($array = mysql_fetch_array($cart_result)) {
//rest code here
}

respondido 08 nov., 11:17

yeah php is saying that it is this line of code. However, there is another part of code which is destroying session if shopping cart is empty - and that's work fine, so the problem is not displaying empty cart. Thank you for your input - Jestem_z_Kozanowa

Ok, but if it's this line, there has to be something wrong with the query result because if that foreach loop is triggered than he still trying to get cart_result. I don't see whole code that's why im guessing right know ;o) did you try echo the $sql to see how query looks, before this error? - Kokers

I assume that...

that is what you are doing wrong.
A programmer should never "assume". A programmer can (and should) be a ciertos.
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 depuración - an investigation to find the causa real del problema. supplied argument is not a valid MySQL result resource error 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 be

$cart_result = mysql_query($sql, $conn) or trigger_error(mysql_error()." in ".$sql);

y vuelva a ejecutar su código.
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 supuestos. Gotcha?

respondido 08 nov., 11:17

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.