Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/154786-update-query-not-working/
Share on other sites

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

 

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

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.

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....

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

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.