1

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

15
  • 2
    Something is going wrong in your PHP code. Most likely due to code injection (I'm guessing). Can we see your actual PHP code where you make the call to the DB? Commented Oct 8, 2013 at 8:05
  • 6
    if qty is quantity, then why are you not using int ...? Commented Oct 8, 2013 at 8:05
  • 1
    If the problem is happening with your PHP, then why aren't you showing us your PHP? Commented Oct 8, 2013 at 8:05
  • 1
    You had better post your PHP code. Commented Oct 8, 2013 at 8:06
  • 3
    It's most likely because you're injecting variables. This is why you should bind variables. Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Oct 8, 2013 at 8:17

3 Answers 3

2

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']."'";
Sign up to request clarification or add additional context in comments.

1 Comment

I have updated my code now I have changed data type of my 'qty' col to 'int' from 'text', still i getting same problem.
1
Your Code       ==> `qty`= $pqty

Changes to this ==> `qty`='$pqty'

2 Comments

now i have changed data type of 'qty' to int that's why qty= $pqty
sitll it's not working ? use below query $sql_qty="update tbl_size set qty= '$pqty' where size_id=".$rowvo['size_id'];
0

try this:

$query="update tbl_size set qty=10 where size_id=335";

1 Comment

qty is text value field type so enter like this way '10'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.