[英]Multiple INSERT/UPDATE with SQL and PHP
can you suggest me the best solution to make multiple INSERT and UPDATE at the same time, please? 您能建议我同时进行多个INSERT和UPDATE的最佳解决方案吗?
I use a SELECT to retrieve records from a table and I let the user to update the content of each record if he checks the checkbox "insert". 我使用SELECT从表中检索记录,并且如果用户选中“插入”复选框,则允许用户更新每个记录的内容。 At the moment, I use a form button for each set of records.
目前,我为每组记录使用一个表单按钮。
Since there can be lots of records, I would like to use only a single "form" and a single "form button" for all the data and to update or insert only the set of records when the user checks the checkbox. 由于可以有很多记录,因此我只想对所有数据使用一个“表单”和一个“表单按钮”,并在用户选中复选框时仅更新或插入一组记录。 Is it possible?
可能吗? If yes, how?
如果是,怎么办? Can you give me any suggestions, please?
请给我任何建议吗?
$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>";
}
In my update.php page, I just read the form values and INSERT them into my table: 在我的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')");
One way would be to generate a sequential row id # for each row. 一种方法是为每一行生成一个顺序的行ID#。 Let's assume this is your collection of rows from the database:
假设这是您从数据库中收集的行:
$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'),
);
Now create the table with a checkbox that has a sequential ID number: 现在,使用具有顺序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;
}
When the form is submitted, you can find all checkboxes that are checked, then process the other form values related to that unique row ID: 提交表单后,您可以找到所有选中的复选框,然后处理与该唯一行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";
}
}
EDIT For your own HTML code, it would be something like this: 编辑对于您自己的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>";
and a form processor something like this: 和一个表单处理器是这样的:
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"];
}
}
使用ON DUPLICATE KEY UPDATE语法执行新记录的插入或现有记录的更新(基于PK)
You need adding record id to element name. 您需要将记录ID添加到元素名称。 Example:
例:
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']. "\">";
You can get all variables in update.php and prepare multiple insert command. 您可以在update.php中获取所有变量,并准备多个insert命令。 Also there are js based table editor, you can use them.
也有基于js的表编辑器,您可以使用它们。 Easy one: http://codepen.io/ashblue/pen/mCtuA
简单的一种: http : //codepen.io/ashblue/pen/mCtuA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.