Reputation: 581
This is my table 'tbl_size'
**Col Name** **Data Type**
size_id int(11)
pd_id int(11)
size_name varchar(20)
size_chart int(6)
qty text
Now I'm trying to update this table with following query
$query="update tbl_size set `qty`='10' where `size_id`=335";
When i run this query in mysql, it works fine but when same thing i tries to do from php then it is updating qty
to '0' and not to '10'.
I'm not getting why this is happening. Please help me to solve this problem.
New Updated Code :
// $rowvo['od_qty']=10;
// $row_1['qty']=20;
$sql = "SELECT qty FROM tbl_size WHERE size_id =".$rowvo['size_id'];
$result = dbQuery($sql);
$row_1 = dbFetchAssoc($result);
$pqty=$row_1['qty']-$rowvo['od_qty'];
echo "Total Quantity Available Before Diduction : ".$row_1['qty'];
echo "Sold Quantity : ".$rowvo['od_qty'];
echo "Remaining Quantity : $pqty";
$sql_qty="update tbl_size set `qty`=$pqty where `size_id`=".$rowvo['size_id'];
echo "$sql_qty<br>";
$result_qty=mysql_query($sql_qty) or die('Error : '.mysql_error());
echo "No of Rows Affected : ".mysql_affected_rows()."<br>".mysql_error(); // 1
New Code with PDO :
try {
$b=$a->prepare("update tbl_size set `qty`=:qty where `size_id`=:size");
$b->bindValue(":size",$size_id);
$b->bindValue(":qty",$pqty);
$b->execute();
} catch(PDOException $ex) {
echo "An Error occured!"; //user friendly message
}
and var_dump($ex)=NULL
Upvotes: 1
Views: 135
Reputation: 4110
Check the structure of your table you have text type for qty. It's possible that it is not of type char or varchar (or that it is and has a default value set to '0'). To resolve this you can use phpmyadmin, SQLyog or other MySql admin programs.
change your query
$sql_qty="update tbl_size set `qty`=$pqty where `size_id`=".$rowvo['size_id'];
to
$sql_qty="update tbl_size set `qty`='$pqty' where `size_id`='".$rowvo['size_id']."'";
Upvotes: 2
Reputation: 578
try this:
$query="update tbl_size set qty=10 where size_id=335";
Upvotes: 0