zed420 Posted April 19, 2009 Share Posted April 19, 2009 Hi All Can someone please help me out here, I can not seem to update this query. if I print out the query it has 'basket_id' and 'qty' empty. Where am I going wrong?? /********************************************* Amend customer's basket *****************************************/ function amendBasketqty(){ if($_POST['update']){ $basket_id = $_GET["basket_id"]; $qty=$_GET["qty"]; $query = "UPDATE basket SET qty='$qty' WHERE basket_id = '$basket_id'"; echo "$query"; if (mysql_affected_rows() > 0) { echo "<center><font color=red size=2>No. = $basket_id has been Updated</font></center>"; showBasket(); balanceToPay(); } } } /******************************************** Show Customer's Basket ***********************************************/ function showBasket(){ $name = $_SESSION['name']; $query = "select basket_id,cat.cat_id,cat.name,item.cat_id,item.item_id,item_name,item.des,basket.price,basket.qty,basket.sname FROM cat,item,basket,user WHERE cat.cat_id = item.cat_id AND item.item_id = basket.item_id AND basket.sname = '$name' GROUP BY basket_id DESC"; $result = mysql_query($query)or die(mysql_error()); ?> <form action="<?php echo $PHP_SELF ?>" method="POST"> <TABLE BORDER=0 WIDTH=100% CELLSPACING=1 CELLPADDING=3 ALIGN=CENTER bgcolor="#BBBBBB"> <TR> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Remove </b></font></TD> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Item ID </b></font></TD> <td width="25%" bgcolor="#F0C9BF"><font color=blue><b>Item Name</b></font></TD> <td width="35%" bgcolor="#F0C9BF"><font color=blue><b>Description</b></font></TD> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Price</b></font></TD> <td width="0%" bgcolor="#F0C9BF"><font color=blue><b>Qty</b></font></TD> </tr> <? while ($row = mysql_fetch_array($result)) { extract($row); echo "<tr class=\"td\"> <td><a href=detail.php?basket_id=" . $row['basket_id'] . ">Remove</a></td> <td>" . $row['item_id'] . "</td> <td>" . $row['item_name'] . "</td> <td><font size=2>" . $row['des'] . "</font></td> <td>" . $row['price'] . "</td> <input name=\"qty\" type=\"hidden\" value=" . $row['basket_id'] . "> <td><input name=\"qty\" type=\"text\" size=\"2\" value=" . $row['qty'] . "></td> </tr>"; }// end of while ?> </table><input name="update" type="Submit" value="Update Cart"/> </form><? } //end of function showbasket Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/ Share on other sites More sharing options...
jOE :D Posted April 19, 2009 Share Posted April 19, 2009 You're submitting the data from a form so you need to use the proper method. Change $_GET['basket_id'] and $_GET['qty'] to $_POST['basket_id'] and $_POST['qty'] Good luck Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-813965 Share on other sites More sharing options...
jamesxg1 Posted April 19, 2009 Share Posted April 19, 2009 $sql = "UPDATE `basket` SET qty = '$qty' WHERE basket_id = '$basket_id'" or die(mysql_error()); $query = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-813973 Share on other sites More sharing options...
zed420 Posted April 19, 2009 Author Share Posted April 19, 2009 Thanks James and Joe I'm so near but so far , when I'm printing the query I'm getting some thing like this, UPDATE `basket` SET qty = '6' WHERE basket_id = '3'Query was empty and it's NOT updating the record in Database. I've used the post method now thats why qty and basket_id is not empty. Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-814011 Share on other sites More sharing options...
Maq Posted April 19, 2009 Share Posted April 19, 2009 Post your current code please. Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-814012 Share on other sites More sharing options...
zed420 Posted April 19, 2009 Author Share Posted April 19, 2009 Here it is /********************************************* Amend customer's basket *****************************************/ function amendBasketqty(){ if($_POST['update']){ $item_id = $_POST["item_id"]; $qty=$_POST["qty"]; $query = "UPDATE `basket` SET qty = '$qty' WHERE item_id = '$item_id'" or die(mysql_error()); echo "$query"; $query = mysql_query($sql) or die(mysql_error()); if (mysql_affected_rows() > 0) { echo "<center><font color=red size=2>No. = $item_id has been Updated</font></center>"; showBasket(); balanceToPay(); } } } /******************************************** Show Customer's Basket ***********************************************/ function showBasket(){ $name = $_SESSION['name']; $query = "select basket_id,cat.cat_id,cat.name,item.cat_id,item.item_id,item_name,item.des,basket.price,basket.qty,basket.sname FROM cat,item,basket,user WHERE cat.cat_id = item.cat_id AND item.item_id = basket.item_id AND basket.sname = '$name' GROUP BY basket_id DESC"; $result = mysql_query($query)or die(mysql_error()); ?> <form action="<?php echo $PHP_SELF ?>" method="POST"> <TABLE BORDER=0 WIDTH=100% CELLSPACING=1 CELLPADDING=3 ALIGN=CENTER bgcolor="#BBBBBB"> <TR> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Remove </b></font></TD> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Item ID </b></font></TD> <td width="25%" bgcolor="#F0C9BF"><font color=blue><b>Item Name</b></font></TD> <td width="35%" bgcolor="#F0C9BF"><font color=blue><b>Description</b></font></TD> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Price</b></font></TD> <td width="0%" bgcolor="#F0C9BF"><font color=blue><b>Qty</b></font></TD> </tr> <? while ($row = mysql_fetch_array($result)) { extract($row); echo "<tr class=\"td\"> <td><a href=detail.php?basket_id=" . $row['basket_id'] . ">Remove</a></td> <td>" . $row['item_id'] . "</td> <td>" . $row['item_name'] . "</td> <td><font size=2>" . $row['des'] . "</font></td> <td>" . $row['price'] . "</td> <input name=\"item_id\" type=\"hidden\" value=" . $row['item_id'] . "> <td><input name=\"qty\" type=\"text\" size=\"2\" value=" . $row['qty'] . "></td> </tr>"; }// end of while ?> </table><input name="update" type="Submit" value="Update Cart"/> </form><? } //end of function showbasket Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-814016 Share on other sites More sharing options...
Maq Posted April 19, 2009 Share Posted April 19, 2009 You give the mysql_query function the wrong string... Change the name of these strings: $sql = "UPDATE `basket` SET qty = '$qty' WHERE item_id = '$item_id'" or die(mysql_error()); echo $sql; Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-814022 Share on other sites More sharing options...
soak Posted April 19, 2009 Share Posted April 19, 2009 Looks like the " or die(mysql_error());" after assigning a string value. That line should read: <?php $sql = "UPDATE `basket` SET qty = '$qty' WHERE item_id = '$item_id'"; You should also be sanitising your data. Anyone remotely skilled in SQL injection could delete your whole database just by changing the form parameters. Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-814031 Share on other sites More sharing options...
meomike2000 Posted April 19, 2009 Share Posted April 19, 2009 the update query in your case should look as follows and will work with the code you posted in the first post. $query = "update basket set qty = ' " . $qty . " ' where basket_id = ' " . $basket_id . " ' "; you dont have to space it all out though could be '".$qty."' like that as well. mike.... Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-814049 Share on other sites More sharing options...
zed420 Posted April 20, 2009 Author Share Posted April 20, 2009 Thank you all for helping me specially Maq, Soak, and meomike2000 Zed Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-814981 Share on other sites More sharing options...
Maq Posted April 20, 2009 Share Posted April 20, 2009 Thank you all for helping me specially Maq, Soak, and meomike2000 Zed I assume this is solved then? Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-814987 Share on other sites More sharing options...
zed420 Posted April 22, 2009 Author Share Posted April 22, 2009 I thought I was finished but unfortunately I have another problem now. I can not Decrease the item 'qty' or their 'price'. Increase is working fine. /********************************************* Amend customer's basket *****************************************/ function amendBasketqty(){ if($_POST['update']){ $item_id = $_POST["item_id"]; $qty=$_POST["qty"]; $price += ($_POST['price'] * $_POST['qty']); $sql = "UPDATE `basket` SET qty = '$qty', price = '$price' WHERE item_id = '$item_id'" or die(mysql_error()); $query = mysql_query($sql) or die(mysql_error()); if (mysql_affected_rows() > 0) { echo "<center><font color=red size=2>No. = $item_id has been Updated</font></center>"; showBasket(); balanceToPay(); } } } /******************************************** Show Customer's Basket ***********************************************/ function showBasket(){ $name = $_SESSION['name']; $query = "select basket_id,cat.cat_id,cat.name,item.cat_id,item.item_id,item_name,item.des,basket.price,basket.qty,basket.sname FROM cat,item,basket,user WHERE item.item_id = basket.item_id AND basket.sname = '$name' GROUP BY basket_id "; $result = mysql_query($query)or die(mysql_error()); ?> <form action="<?php echo $PHP_SELF ?>" method="POST"> <TABLE BORDER=0 WIDTH=100% CELLSPACING=1 CELLPADDING=3 ALIGN=CENTER bgcolor="#BBBBBB"> <TR> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Remove </b></font></TD> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Item ID </b></font></TD> <td width="25%" bgcolor="#F0C9BF"><font color=blue><b>Item Name</b></font></TD> <td width="35%" bgcolor="#F0C9BF"><font color=blue><b>Description</b></font></TD> <td width="10%" bgcolor="#F0C9BF"><font color=blue><b>Price</b></font></TD> <td width="0%" bgcolor="#F0C9BF"><font color=blue><b>Qty</b></font></TD> </tr> <? while ($row = mysql_fetch_array($result)) { extract($row); echo "<tr class=\"td\"> <td><a href=detail.php?basket_id=" . $row['basket_id'] . ">Remove</a></td> <td>" . $row['item_id'] . "</td> <td>" . $row['item_name'] . "</td> <td><font size=2>" . $row['des'] . "</font></td> <td><input name=\"price\" type=\"text\" value=" . $row['price'] . " readonly=\"readonly\" /></td> <input name=\"item_id\" type=\"hidden\" value=" . $row['item_id'] . "> <td><input name=\"qty\" type=\"text\" size=\"2\" value=" . $row['qty'] . "></td> </tr>"; } ?> </table><input name="update" type="Submit" value="Update Cart"/> </form><? } //end of function showbasket Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-816776 Share on other sites More sharing options...
radi8 Posted April 22, 2009 Share Posted April 22, 2009 Take the sql that is being displayed and put it into MYSQL Query Analyzer and run it from there. If there are errors in the table or something else, it will tell you more information. It must be an issue in the SQL but i can;t see it. Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-816788 Share on other sites More sharing options...
zed420 Posted April 22, 2009 Author Share Posted April 22, 2009 Thanks I'll try that Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-816798 Share on other sites More sharing options...
Maq Posted April 22, 2009 Share Posted April 22, 2009 Yeah, don't do this: $sql = "UPDATE `basket` SET qty = '$qty', price = '$price' WHERE item_id = '$item_id'" or die(mysql_error()); Also before looking at your code put this at the top and post if anything is displayed: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/154786-update-query-not-working/#findComment-816799 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.