我将MySQL数据库拆分为三个表(tblmaincircles,tblsmallcircles,tbltabs)和一个更新这些表中数据的PHP文件。
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
... | ... | ... | ... | ... | ...
我的php表单向用户显示所有当前值,其中包括来自所有三个表的信息以及来自tblsmallcircles和tbltabs的多行。我的表单的缩短版本如下所示:
<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>
以下是SQL更新语句:
$usqlMC = "UPDATE tblmaincircles SET mcName= '".$_POST['mcName']."' WHERE mcID=???;";
$usqlSC = "UPDATE tblsmallcircles SET scVisible= '".$_POST['scVisible']."', scName= '".$_POST['scName']."' WHERE scID=???;";
$usqlt = "UPDATE tbltabs SET mcName= '".$_POST['mcName']."' WHERE tabID=???;";
我被困在???应该放在???对于mcID =因为它根据已经完成的字段而变化。
希望我提供了足够的信息,这也不算太多!对此的任何帮助都会很棒。我是PHP和MySQL的新手,欢迎任何评论。提前谢谢!
更新
这是我更新的代码行。现在,这已将所有mcNames更改为字段输入值,而不仅仅是M1。
HTML
<input type="text" class="textfield" id="mcName" name="mcName" value="<?php echo htmlspecialchars($M1); ?>"/>
SQL
$usqlMC = "UPDATE tblmaincircles SET mcName= '".$_POST['mcName']."' WHERE mcID IN ('M1','M2','M3','M4','M5');";
我的目标是只更新M1,但我目前在同一表格上有M2,M3等的其他输入,因此可能需要同时更新。有没有办法在不为M1,M2等编写单独的SQL的情况下执行此操作。
我可以通过编写5个单独的SQL语句来实现这一点:
$usqlM1 = "UPDATE tblmaincircles SET mcName= '".$_POST['mcNameM1']."' WHERE mcID='M1';";
$usqlM2 = "UPDATE tblmaincircles SET mcName= '".$_POST['mcNameM2']."' WHERE mcID='M2';";
$usqlM3 = "UPDATE tblmaincircles SET mcName= '".$_POST['mcNameM3']."' WHERE mcID='M3';";
$usqlM4 = "UPDATE tblmaincircles SET mcName= '".$_POST['mcNameM4']."' WHERE mcID='M4';";
$usqlM5 = "UPDATE tblmaincircles SET mcName= '".$_POST['mcNameM5']."' WHERE mcID='M5';";
但如果可能的话我会更喜欢。
答案 0 :(得分:0)
MySQL具有'IN'语法,因此如果您使用相同的属性更新多个行,则可以这样执行:
$usqlMC = "UPDATE tblmaincircles SET mcName= '".$_POST['mcName']."' WHERE mcID IN ('M1', 'M2');";