INSERT AND UPDATE AT THE SAME TIME IN Php

UPDATE In Mysql w.r.t Php:-

                    UPDATE command in Mysql plays an important role. As we know that we use Xampp in our system for various purposes such as for browsing in localhost, for database point of view we go through http://localhost/phpmyadmin for the use of database as generation/creations/modifying /deleting….. of commands.


                    Meanwhile why I am concentrate on UPDATE rather than other commands (INSERT, CREATE, DELETE) because today so many people facing problems with UPDATE command. If the UPDATE command relating to one table it's easy, but if UPDATE command related to more than one table is difficult.

FAQ:-
What is the difficulty during Update command?
Answer:-
The difficulty is when you face UPDATE command with two or more tables,
for example there are 3 values in one table it should be INSERT ed and another table it should be UPDATE ed .

With INSERT there is no problem the values are INSERT ed , In the same time the values are also UPDATE ed in another table as you see the particular record in a Database but other values(fields) turns to ‘0’.

Solution:-

What to Do?
Here I am taken the data related to fee.
In Step1:-The data should be INSERT in fee record.
In Step2:-The same data should be UPDATED in Student record without disturbance of other fields in that record such as name ,class, roll no and so on..

How To Do?
Here we give importance to merging concept, asmall code w.r.t ‘C’.
Source Code as follows:-

$result=mysql_query("SELECT receipt_no from feebilling ORDER BY receipt_no DESC LIMIT 1 \n");
$fet=mysql_fetch_array($result);

extract($_POST);
if($_POST['submit']=='pay')
{
mysql_query("INSERT INTO feebilling VALUES(`receipt_no`,'$date','$addmission_no','$name','$class','$installment_1',
'$installment_2','$installment_3','$installment_4')");
$result1=mysql_query("SELECT installment_1,installment_2,installment_3,installment_4 from $class where addmissionno='$addmission_no'");
$fet1=mysql_fetch_array($result1);
$t1=$fet1['installment_1'];
$t2=$fet1['installment_2'];
$t3=$fet1['installment_3'];
$t4=$fet1['installment_4'];

$result2=mysql_query("SELECT installment_1,
installment_2,installment_3,installment_4
from feebilling ORDER BY
receipt_no DESC LIMIT 1 \n");
$fet2=mysql_fetch_array($result2);
$r1=$fet2['installment_1'];
$r2=$fet2['installment_2'];
$r3=$fet2['installment_3'];
$r4=$fet2['installment_4'];

if($t1 >= $r1)
{
$f1=$t1;
}
else
{$f1=$r1;}

if($t2 >= $r2)
{
$f2=$t2;
}
else
{$f2=$r2;}

if($t3 >= $r3)
{
$f3=$t3;
}
else
{$f3=$r3;}

if($t4 >= $r4)
{
$f4=$t4;
}
else
{$f4=$r4; }

mysql_query("UPDATE `school`.`$class` SET `installment_1` = '$f1',
`installment_2` = '$f2',
`installment_3` = '$f3',
`installment_4` = '$f4' WHERE `$class`.`addmissionno` ='$addmission_no'");

No comments: