PHP update several rows in MySQL with one submit -
i have mysql database split 3 tables (tblmaincircles, tblsmallcircles, tbltabs) , php file updates data within these tables.
tblmaincircles:
mcid | mcname ------------- m1 | m1name m2 | m2name m3 | m3name ... | ... tblsmallcircles:
scid | scvisibleid | scvisible | mcid |scname ------------------------------------------------ m1s1 | m1s1v | visible | m1 | m1s1name m1s2 | m1s2v | visible | m1 | m1s2name m1s3 | m1s3v | hidden | m1 | m2s1 | m2s1v | visible | m2 | m2s1name ... | ... | ... | ... | ... tbltabs:
tabid | scid | mcid | tabname | taburlid | taburl ------------------------------------------------------------- m1s1t1 | m1s1 | m1 | m1s1t1name | m1s1t1url | m1s1t1urlname m1s1t2 | m1s1 | m1 | m1s1t2name | m1s1t2url | m1s1t2urlname m1s2t1 | m1s2 | m1 | m1s2t1name | m1s2t1url | m1s2t1urlname ... | ... | ... | ... | ... | ... my php form shows of current values user includes information 3 tables , multiple rows tblsmallcircles , tbltabs. shortened version of form looks this:
<body> <form action="<?php echo $_server['php_self']; ?>" method="post"> <ul> <li> <label class="mctext">main circle:</label> <input type="text" class="textfield" id="mc" name="mc" value="<?php echo htmlspecialchars($m1); ?>"/> </li> <ul> <li class="sctext"> <img src="images/triangle_right.png" alt="none" class="tri" id="sc1tri"> <label>small circle 1:</label> <input type="text" class="textfield" id="sc1" name="sc1" value="<?php echo htmlspecialchars($m1s1); ?>"/> </li> <ul> <div class="doit"> <li> <label class="tabtext">tab 1:</label> <input type="text" class="textfield" id="sc1t1" name="sc1t1" value="<?php echo htmlspecialchars($m1s1t1); ?>"/> </li> <li> <label class="urltext">tab 1 url/file:</label> <input type="text" class="textfield" id="sc1t1url" name="sc1t1url" value="<?php echo htmlspecialchars($m1s1t1url); ?>"/> </li> <li> <label class="pdftext">tab 1 pdf:</label> <input type="button" class="button" id="sc1t1pdf" name="sc1t1pdf" value="view" onclick="openm1s1t1url()"/> <span> </span> <input type="file" name="sc1t1pdfup" id="sc1t1pdfup"/> </li> <li> <label class="tabtext">tab 2:</label> <input type="text" class="textfield" id="sc1t2" name="sc1t2" value="<?php echo htmlspecialchars($m1s1t2); ?>"/> </li> <li> <label class="urltext">tab 2 url/file:</label> <input type="text" class="textfield" id="sc1t2url" name="sc1t2url" value="<?php echo htmlspecialchars($m1s1t2url); ?>"/> </li> <li> <label class="pdftext">tab 2 pdf:</label> <input type="button" class="button" id="sc1t2pdf" name="sc1t2pdf" value="view" onclick="openm1s1t2url()"/> <span> </span> <input type="file" name="sc1t2pdfup" id="sc1t2pdfup"/> </li> <li class="lipsace"></li> </div> <li> <input type="submit" class="button" name='submit' value="submit"/> <span> </span> <input type="button" class="button" name='cancel' value="cancel" onclick="history.go(0)"/> </li> </ul> </ul> </ul> </form> </body> here sql update statements:
$usqlmc = "update tblmaincircles set mcname= '".$_post['mcname']."' mcid=???;"; $usqlsc = "update tblsmallcircles set scvisible= '".$_post['scvisible']."', scname= '".$_post['scname']."' scid=???;"; $usqlt = "update tbltabs set mcname= '".$_post['mcname']."' tabid=???;"; i stuck should place in ??? mcid= because varies based on fields have been complted.
hopefully provided enough information , not either! assistance great. new php , mysql comments welcome. in advance!
update
here updated lines of code have made. changed mcnames fields input value instead of m1.
html
<input type="text" class="textfield" id="mcname" name="mcname" value="<?php echo htmlspecialchars($m1); ?>"/> sql
$usqlmc = "update tblmaincircles set mcname= '".$_post['mcname']."' mcid in ('m1','m2','m3','m4','m5');"; my goal have m1 updated have other inputs m2, m3, etc. on same form may need updated @ same time. there way without writing individual sql m1, m2, etc.
i can achieve writing 5 separate sql statements this:
$usqlm1 = "update tblmaincircles set mcname= '".$_post['mcnamem1']."' mcid='m1';"; $usqlm2 = "update tblmaincircles set mcname= '".$_post['mcnamem2']."' mcid='m2';"; $usqlm3 = "update tblmaincircles set mcname= '".$_post['mcnamem3']."' mcid='m3';"; $usqlm4 = "update tblmaincircles set mcname= '".$_post['mcnamem4']."' mcid='m4';"; $usqlm5 = "update tblmaincircles set mcname= '".$_post['mcnamem5']."' mcid='m5';"; but prefer 1 if possible.
mysql has 'in' syntax if updating several rows same property, can this:
$usqlmc = "update tblmaincircles set mcname= '".$_post['mcname']."' mcid in ('m1', 'm2');";
Comments
Post a Comment