Jump to content

if...else


gaya

Recommended Posts

Hi, I need to know some logic please any1 help me. Now i'd merged some values from 2 different tables and inserted the merged values in 3rd table with some values.for eg if i've given some values like,id=1,name=tweety,subject=sst,maxmark=5 & flag=0The above values i've already inserted in the 3rd table.The second time when inserting into the table with same values i got them insertd again in 3rd table.Now i want, not the same values to insert into the table again and again. I want to update them.I dont knw how to implement them using if condition.Please any1 tell me the logic.

Link to comment
Share on other sites

Set an primary key or unique constraint to one of your field to determine each of them as unique after than you can use "REPLACE" (google it) to insert data. it will try to insert data first if it fails in PK or unique column it will try to update it.

  • Like 1
Link to comment
Share on other sites

http://www.google.co...357700187,d.bmk <=replace http://www.google.co...357700187,d.bmk <=insert on duplicate REPLACE and INSERT ON DUPLICATE are work in same ways. They both need one query to do the job.but if you use insert and update separately it will need two query to do it. every query makes a transaction over network. so reducing those transaction helps to optimize the performance. Edited by birbal
Link to comment
Share on other sites

Please tell me a thing. My tables questionpaper,ShortQues and paper_valuation_inter. I'd merged some values from questionpaper and ShortQues then inserted into paper_valuation_inter. The following is my code

$query2="SELECT B.Subject, A.subject, A.orderno, A.qpid, A.qno, A.tablename, A.part,A.flag,A.maxmark,A.flagFROM questionpaper AS A, ShortQues AS BWHERE A.subject = '$Subject' && B.Subject = '$Subject' && A.orderno =$orderno && A.qpid = '$qpid' && A.qno ='$quesnum' && A.tablename = 'ShortQues' && A.part = '$part' && A.qno=B.QNo ";   //gayu   $result2=mysql_query($query2);   //echo "$query2"; //gayuwhile($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)){//echo "<script>alert('$result2')</script>";//$StudentId=$row2['StudentId'];$orderno=$row2['orderno'];$quesnum = $row2['qno'];$Subject=$row2['subject'];$qpid=$row2['qpid'];$part=$row2['part'];$maxmark=$row2['maxmark'];$flag=$row2['flag'];$tablename=$row2['tablename'];if($orderno=="null")							  //here i dnt knw wat to use to check the condition{$query3="insert into paper_valuation_inter values(null,'$StudentId','$orderno','$quesnum','$Subject','$qpid','$tablename','$part','$maxmark','$flag')";$result3=mysql_query($query3);//echo "$query3";echo "<script>alert('Question inserted into the table')</script>";}else{$query3="update paper_valuation_inter set flag=1 where orderno='$orderno' && userid='$StudentId' && qno='$quesnum' && subject='$Subject' && qpid='$qpid' && tablename='$tablename' && part='$part' && maxmark='$maxmark' && flag='$flag'";$result3=mysql_query($query3);echo "$query3";echo "<script>alert('update table')</script>";}

In that i dnt knw how to use if condition. Is there the possibility of doing this with "REPLACE" or shall i do with this code itself. please tell me. If means please tell me wat i did wrong in this code.

Edited by gaya
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...