2

I'm currently creating some sort of inventory system.

I have master_tbl where in I save the items. In master_tbl, I have column qty_left or the available stock left.

I also have the table issuance_tbl where in I save all the transaction in issuing items. In this table there is issued_qty.

My problem is how can I INSERT a row into issuance_tbl at the same time UPDATE the master_tbl.qty_left. (master_tbl.qty_left - issuance_tbl.issued_qty).

Is it possible?

a_horse_with_no_name
  • 440,273
  • 77
  • 685
  • 758
ChocoMartin
  • 107
  • 1
  • 2
  • 10

4 Answers4

0

I think the best way is using Stored Procedure. You can have bunch of SQL statements with error handling and ACID transactions in one place. This is because if your first query executes and the second fails, you may need to rollback transactions. Stored procedures allow all this fancy but reliable stuff.

You can start here: http://forums.mysql.com/read.php?98,358569

user2483744
  • 273
  • 1
  • 5
  • 11
  • I'm newbie programmer and i have no idea about Stored Procedure. :( – ChocoMartin Aug 14 '13 at 07:11
  • @ChocoMartin: there are millions of tutorials regarding stored procedures on the internet (the MySQL has one as well - and you did read the manual, did you?). You should really read them. – a_horse_with_no_name Aug 14 '13 at 07:16
  • Then I would suggest to invest sometime in it. It will really help you write enterprise level applications in the future. You will find thousands of examples and tutorials on this topic. – user2483744 Aug 14 '13 at 07:16
0

here is the example:

    <form method="post">
    QTY:<input type="text" name="qty_left"></input>
        <input type="submit" name="submit" value="update"></form>

   <?php
         ////////your config db
     $qty = $_POST['qty_left'];
     if(isset($_POST['submit']);
     $sql = mysql_query("insert into issuance_tbl (issued_qty) values (".$qty.") ");
     $sql1 = mysql_query("update master_table set qty_left= ".$qty."");
     ?>
0

I'm not completely confident that 'If there's any way to do such thing': You need to do it in steps, LIKE THIS:

$result = $this->db->insert($table);//Example function to insert inventory item
$insert_id = $this->db->insert_id();//Example function to get the id of inserted item
if($result)
$res = $this->db->update($id,$data,$table);//Example function to update data of quantity table

if(!$res)
{
$this->db->delete($insert_id,$table);s
$result = '-1';
}
return $result;

Hope this might help

K Cloud
  • 271
  • 1
  • 5
  • 15
0
$con = mysqli_connect($host, $user, $password, $database);
...
$issued_qty = '10'; //some value

$insert_query = "insert into issuance_table (issued_qty, ...) values ('$issued_qty', ... ) ;";    
$update_query = "update master_tbl set qty_left = (qty_left - ".$issued_qty.")  where ....";    
mysqli_multi_query($con, $insert_query.$update_query);
Bere
  • 1,402
  • 2
  • 14
  • 19