你可以建议我同时进行多次INSERT和UPDATE的最佳解决方案吗?
我使用SELECT从表中检索记录,如果用户选中“insert”复选框,我会让用户更新每条记录的内容。 目前,我为每组记录使用表单按钮。
由于可能有很多记录,我想只为所有数据使用一个“表单”和一个“表单按钮”,并在用户选中复选框时仅更新或插入记录集。可能吗?如果有,怎么样?你能给我任何建议吗?
$sql = "select * from table1 where nome_area like '%$nome_area%' AND nome_voce like '%$nome_voce%'";
$rs = mysql_query( $sql ) or die('Database Error: ' . mysql_error());
$num = mysql_num_rows( $rs );
if($num >= 1 ){
echo "<table align=\"center\" cellspacing=0 cellpadding=100>";
echo "<tbody><tr><td width=\"20%\"> </td><td>";
while($row = mysql_fetch_array( $rs )){
echo "<form name=\"form\" id=\"form\" action=\"update.php\" method=\"post\">";
echo "<table data-role=\"table\" class=\"ui-responsive\" cellspacing=\"20\" ";
echo "<tbody>";
echo "<tr>";
echo "<td>";
echo "<font text color=\"red\">Insert</font> </td><td><input type=\"checkbox\" name=\"insert\" value=\"1\" id=\"insert\"></td></tr>";
echo "<tr><td><b>Tipo Area</b>:</td><td>".$row['nome_area']. " </td></tr><tr><td><b>Nome della voce</b>: </td><td> " .$row['nome_voce'] . "</td></tr>";
echo "<tr><td>";
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"id_voce\" maxlength=\"50\" value=\"" . $row['ID'] . "\">";
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"nome_azienda\" maxlength=\"50\" value=\"" .$name. "\">"; // azienda
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"nome_area\" maxlength=\"50\" value=\"" .$row['nome_area']. "\">";
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"nome_voce\" maxlength=\"50\" value=\"" .$row['nome_voce']. "\">";
echo "</td></tr>";
echo "<tr><td colspan=\"2\"> <input type=\"submit\" value=\"Associa la voce di contratto all'azienda\" id=\"search-btn\" style=\"height:50px; \"; /></td></tr>";
echo "</tbody></table>";
echo "</form>";
}
echo "</td></tr></tbody></table>";
}else{
// if no records found
echo "<br><br><b>Nessun risultato trovato!</b></div>";
}
在我的update.php页面中,我只是读取了表单值并将它们插入到我的表中:
.....
mysql_query("INSERT INTO table2 (id_voce,associazione, nome_azienda, nome_area, nome_voce, created_date) VALUES('$id_voce','$associa','$nome_azienda','$nome_area','$nome_voce','$time')");
答案 0 :(得分:1)
一种方法是为每一行生成一个顺序行id#。我们假设这是您从数据库中收集的行:
$rs = array(
array('ID'=>'111', 'nome_area'=>'first nome area', 'nome_voce'=>'first nome voce'),
array('ID'=>'222', 'nome_area'=>'second nome area', 'nome_voce'=>'second nome voce'),
array('ID'=>'333', 'nome_area'=>'third nome area', 'nome_voce'=>'third nome voce'),
);
现在使用具有顺序ID号的复选框创建表:
$rowcounter = 0;
foreach ($rs as $row) {
$rowcounter++;
echo <<<HERE
<tr>
<td><input type="checkbox" name="row_{$rowcounter}_checkbox" value="1" /></td>
<td>
ID: {$row['ID']}
<input type="hidden" name="row_{$rowcounter}_ID" value="{$row['ID']}" />
</td>
<td>
Tipo Area: {$row['nome_area']}
<input type="hidden" name="row_{$rowcounter}_nome_area" value="{$row['nome_area']}" />
</td>
<td>
Nome della voce: {$row['nome_voce']}
<input type="hidden" name="row_{$rowcounter}_nome_voce" value="{$row['nome_voce']}" />
</td>
</tr>
HERE;
}
提交表单后,您可以找到所有选中的复选框,然后处理与该唯一行ID相关的其他表单值:
foreach ($_POST as $key=>$val) {
if (preg_match("/^row_(\d+)_checkbox$/",$key,$matches) && $val == "1") {
$rowid = $matches[1];
$ID = $_POST["row_{$rowid}_ID"];
$nome_area = $_POST["row_{$rowid}_nome_area"];
$nome_voce = $_POST["row_{$rowid}_nome_voce"];
print "The checkbox was checked for the row with this data:\n";
print "ID: $ID\n";
print "nome_area: $nome_area\n";
print "nome_voce: $nome_voce\n";
}
}
编辑对于您自己的HTML代码,它将是这样的:
$rowcounter = 0;
echo "<form name=\"form\" id=\"form\" action=\"update.php\" method=\"post\">";
echo "<table data-role=\"table\" class=\"ui-responsive\" cellspacing=\"20\" ";
echo "<tbody>";
while($row = mysql_fetch_array( $rs )){
$rowcounter++;
echo "<tr>";
echo "<td>";
echo "<font text color=\"red\">Insert</font> </td><td><input type=\"checkbox\" name=\"row_{$rowcounter}_insert\" value=\"1\" id=\"insert\"></td></tr>";
echo "<tr><td><b>Tipo Area</b>:</td><td>".$row['nome_area']. " </td></tr><tr><td><b>Nome della voce</b>: </td><td> " .$row['nome_voce'] . "</td></tr>";
echo "<tr><td>";
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"row_{$rowcounter}_id_voce\" maxlength=\"50\" value=\"" . $row['ID'] . "\">";
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"row_{$rowcounter}_nome_azienda\" maxlength=\"50\" value=\"" .$name. "\">"; // azienda
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"row_{$rowcounter}_nome_area\" maxlength=\"50\" value=\"" .$row['nome_area']. "\">";
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"row_{$rowcounter}_nome_voce\" maxlength=\"50\" value=\"" .$row['nome_voce']. "\">";
echo "</td></tr>";
}
echo "<tr><td colspan=\"2\"> <input type=\"submit\" value=\"Associa la voce di contratto all'azienda\" id=\"search-btn\" style=\"height:50px; \"; /></td></tr>";
echo "</tbody></table>";
echo "</form>";
和这样的表单处理器:
foreach ($_POST as $key=>$val) {
if (preg_match("/^row_(\d+)_insert$/",$key,$matches)) {
$rowid = $matches[1];
$nome_id_voce = $_POST["row_{$rowid}_nome_id_voce"];
$nome_area = $_POST["row_{$rowid}_nome_area"];
$nome_voce = $_POST["row_{$rowid}_nome_voce"];
}
}
答案 1 :(得分:0)
使用ON DUPLICATE KEY UPDATE语法执行新记录的插入或现有记录的更新(基于PK)
答案 2 :(得分:0)
您需要将记录ID添加到元素名称。例如:
echo "<input type=\"hidden\" style=\"width:0; height:0; border:0; background-color:inherit; overflow:hidden;\" name=\"nome_voce_". $row['ID'] ."\" maxlength=\"50\" value=\"" .$row['nome_voce']. "\">";
您可以在update.php中获取所有变量并准备多个insert命令。 还有基于js的表编辑器,你可以使用它们。简单的一个:http://codepen.io/ashblue/pen/mCtuA