hanadeka80 Posted October 7, 2008 Share Posted October 7, 2008 I'm building an add to shopping cart function..and i got this error..please help many thanks in advance.. !! Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:Program Filesxampphtdocsxamppcartmayshoppingcart1.php on line 19 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Cart</title> </head> <body> <form method="post" action="shoppingcart1.php"> ORDERS<br /> <center><input type="submit" name="done" value="CONFIRM ORDERS" /></center> <center><table border="2"><tr><td width="200"><center>PRODUCT NAME</center></td><td width="200"><center>PRODUCT PRICE</center></td><td width="200"><center>QUANTITY ORDERED</center></td><td width="200"><center>TOTAL PRICE</center></td></tr> <? require("connection.php"); $cookie = $_COOKIE['cart']; $getprod = "SELECT productcar.PRODUCT_ID,productcar.PROD_NAME,productcar.PROD_PRICE,orders.QUANTITY,orders.PRICE FROM orders,productcar WHERE orders.CART_ID = '$cookie' and orders.PROD_ID = productcar.PROD_ID"; $val1 = mysql_query($getprod); while($val= mysql_fetch_array($val1)) { $viewProdName = $val['PROD_NAME']; $viewProdPrice = $val['PROD_PRICE']; $viewQUANTITY = $val['QUANTITY']; $viewPRICE = $val['PRICE']; $viewPRODUCT = $val['PRODUCT']; ?><tr><center><td width="200"><center><?=$viewProdName?></center></td><td width ="200"><center><?=$viewProdPrice?></center></td><td width="200"><center><?=$viewQUANTITY?></center></td><td width="200"><center><?=$viewPRICE?></center><td><img width="60" height="60" src=<?=$viewPRODUCT?> /></td> </td></center></tr> <? } ?></table></center> <center><input type="submit" name="done" value="CONFIRM ORDERS" /></center> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/127419-helpshopping-cartwarning-mysql_fetch_array/ Share on other sites More sharing options...
F1Fan Posted October 7, 2008 Share Posted October 7, 2008 Hmm... It sounds like the connection object isn't being accessed or something. Also, you should be using mysql_fetch_assoc rather than mysql_fetch_array. fetch_assoc will return values with the key being the column name, while fetch_array will use an incrementing number for the key (0, 1, 2, ...). Link to comment https://forums.phpfreaks.com/topic/127419-helpshopping-cartwarning-mysql_fetch_array/#findComment-659117 Share on other sites More sharing options...
hanadeka80 Posted October 7, 2008 Author Share Posted October 7, 2008 yup this is is! thanks for the help about the assoc and your right i can't connect to the dB cause of the wrong column name. I'll keep this thread posted..cause i really need help i'm just a student and i really want to learn.. so much thanks to you! Link to comment https://forums.phpfreaks.com/topic/127419-helpshopping-cartwarning-mysql_fetch_array/#findComment-659212 Share on other sites More sharing options...
hanadeka80 Posted October 8, 2008 Author Share Posted October 8, 2008 regarding with the shopping cart.. uhm i have this code that would insert a statement in the sql server..when you click the buy button now the problem is this error.. Parse error: parse error, unexpected $end in C:\Program Files\xampp\htdocs\xampp\GPS_Php4\checkout.php on line 106 i don't really know how to fix it and i check the closing tags of php and html . and i think i also have some errors regarding the insert statement.. please help me thank you so much in advance well here's the code <?PHP session_start(); $fname=$_SESSION['user']; /*if(!(isset($_SESSION['valid']))) { header('Location:login.php'); exit(); }*/ require 'list.php'; if(@$_POST['submit']=='clear cart') { unset($_SESSION['cart']); } elseif(isset($_POST['update'])) { foreach(@$_POST['update'] as $id=>$p) { if(preg_match('/[0-9]/',$p)) { if($p=='0') { unset($_SESSION['cart'][$id]); } else { $_SESSION['cart'][$id]=$p; } } } } ?> <?php require("connection.php"); if(isset($_POST['buy'])) { if($insert = 1) { $cid=$_POST['cart_id']; $id=$_POST['prod_id']; $name=$_POST['prod_name']; $price=$_POST['prod_price']; $qty=$_POST['quantity']; //$total=$_POST['total']; $sql= "INSERT INTO cart_order VALUES ('".$cid."','".$id."','".$name."',".$price.",".$qty.")"; mysql_query($sql); $resultInsert = $connection ->query($sql); echo "Record save sucessfully"; require("pay.php"); ?> <html> <form action="<?=$_SERVER['PHP_SELF'] ?>" method="post"> <table border=1> <tr> <td>ITEMS</td> <td>Name</td><td>Price</td> <td>Amount</td> </tr> <?php $counter=0; if(@is_array($_SESSION['cart'])) { foreach(@$_SESSION['cart'] as $id=>$count) { $counter++; echo "<tr><td>{$counter}</td><td>{$product[$id]['desc']}</td><td>{$product[$id]['price']}</td><td><input type=\"text\" name=\"update[{$id}]\" value=\"{$count}\"></td></tr>"; $total_value+=$product[$id]['price']*$count; } } ?> <tr><td colspan=3>Total </td><td><?=$total_value ?></td></tr> <tr> <td><input type="button" name="continue" value="continue shopping" onclick="javascript:window.location.href='new_arrival.php' "</td> <td><input type="submit" name="submit" value="clear cart" ></td> <td><input type="submit" name="submit" value="update"></td> <td><input type="button" name="buy" value="buy" onclick="javascript:window.location.href='pay.php' "></td> </tr> </table> </form> </body> </html> please please i relly need help :'( Link to comment https://forums.phpfreaks.com/topic/127419-helpshopping-cartwarning-mysql_fetch_array/#findComment-659733 Share on other sites More sharing options...
hanadeka80 Posted October 8, 2008 Author Share Posted October 8, 2008 update: as of the moment i figured out the error : Parse error: parse error, unexpected $end in C:\Program Files\xampp\htdocs\xampp\GPS_Php4\checkout.php on line 106 and i'm so so happy..hehehe basically i forgot to put some end curly bracket.. but my problem is still i can't insert any data to the database.. is my insert statement right?? please do check it or the connections is the problem?? thanks so much!! more power !! Link to comment https://forums.phpfreaks.com/topic/127419-helpshopping-cartwarning-mysql_fetch_array/#findComment-659769 Share on other sites More sharing options...
F1Fan Posted October 8, 2008 Share Posted October 8, 2008 When running a query, you should ALWAYS do this: <?php mysql_query($sql) or die(mysql_error()); ?> It sounds like your query is failing, so this will tell you the error. Link to comment https://forums.phpfreaks.com/topic/127419-helpshopping-cartwarning-mysql_fetch_array/#findComment-659905 Share on other sites More sharing options...
philipolson Posted October 8, 2008 Share Posted October 8, 2008 Or something like: <?php $sql = "SELECT foo FROM bar"; $res = mysql_query($sql); if (!$res) { echo "Unable to execute query ($sql) and here is the error: " . mysql_error(); exit; } ?> It's basically the same as the use of "or die()" above except you can do more, like handle the error more gracefully (Not that I did above) and by gracefully I mean not show users your query or ugly mysql errors but instead you might have: <?php if (!$res) { // A function to call during errors, it might log error, email you, show pretty output, etc. db_error(); // And/or output funny text echo 'We are sleeping'; // And/or include a file instead of a function like db_error() include 'db_error_stuff.php'; } ?> This is also true for using mysql_connect(), mysql_select_db(), etc. Also, always check the results of your queries and never assume they are perfect valid mysql resources. Link to comment https://forums.phpfreaks.com/topic/127419-helpshopping-cartwarning-mysql_fetch_array/#findComment-659923 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.