简体   繁体   English

更新多行(PHP + MySQL)

[英]Update Multiple Rows (PHP + MySQL)

I am working on a lead management system - and as the database for it grows the need for more bulk functions appears - and unfortunately I am getting stuck with one of them. 我正在开发一个潜在客户管理系统-随着数据库的增长,对更多批量功能的需求也随之出现-不幸的是,我陷入了其中之一。 The database stores many different leads - with each lead being assigned to a specific closer; 该数据库存储许多不同的潜在客户-每个潜在客户都分配给特定的近距离; thus the database stores for each lead the lead id, name, closer name, and other info. 因此,数据库为每个潜在客户存储潜在客户ID,名称,更接近的名称和其他信息。 The main lead list shows a checkbox next to each lead which submits the lead id into an array: 主销售线索列表在每个销售线索旁边显示一个复选框,该复选框将销售线索ID提交到数组中:

<input type=\"checkbox\" name=\"multipleassign[]\" value=\"$id\" />

Now this all goes to the following page: 现在,所有这些都转到以下页面:

<?php
include_once"config.php";
$id = $_POST['multipleassign'];
$id_sql = implode(",", $id);
$list = "'". implode("', '", $id) ."'";

$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);

if ($num > 0 ) {
$i=0;
while ($i < $num) {


$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");


echo "$closer - $businessname";
echo"<br>";

++$i; } } else { echo "The database is empty"; };



echo "<select name=\"closer\" id=\"closer\">";

$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);

if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {

$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");

echo "<option value=\"$fullname\">$fullname</option>";

++$i2; } } else { echo "The database is empty"; }
echo "</select>";

?>

I want to be able to use the form on this page to select a closer from the database - and then assign that closer to each of the leads that have been selected. 我希望能够使用此页面上的表单从数据库中选择一个更接近的对象-然后将该更接近的对象分配给已选择的每个潜在客户。 Here is where I have no idea how to continue. 这是我不知道如何继续的地方。

Actually - i got it. 其实-我明白了。 I don't know why I didn't think of it sooner. 我不知道为什么我不早想到这一点。 First off I passed the original $list variable over to the new page - and then: 首先,我将原始的$ list变量传递给新页面-然后:

<?php
include_once"config.php";
$ids = $_POST['list'];
$closer = $_POST['closer'];
$query = "UPDATE `promises` SET `closer` = '$closer' WHERE id IN ($ids) ";
mysql_query($query) or die ('Error updating closers' . mysql_error());
echo "A new closer ($closer) was assigned to the following accounts:";

$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);

if ($num > 0 ) {
$i=0;
while ($i < $num) {
$businessname = mysql_result($result,$i,"business_name");
echo "<li>$businessname";

++$i; } } else { echo "The database is empty"; };
?>

The updated page before this: 之前的更新页面:

$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);

if ($num > 0 ) {
$i=0;
while ($i < $num) {


$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");


echo "$closer - $businessname";
echo"<br>";

++$i; } } else { echo "The database is empty"; };


echo "<form name=\"form1\" method=\"post\" action=\"multiple_assign2.php\">";
echo "<input type=\"hidden\" name=\"list\" value=\"$list\" />";
echo "<select name=\"closer\" id=\"closer\">";

$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);

if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {

$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");

echo "<option value=\"$fullname\">$fullname</option>";

++$i2; } } else { echo "The database is empty"; }
echo "</select>";
echo "<input name=\"submit\" type=\"submit\" id=\"submit\" value=\"Reassign Selected Leads\">";

?> 

After you select the leads and submit the form , your script should show them in a list with hidden inputs (with name=leads[] and value=the_lead's_id) and next to each lead there will be a dropdown box () which will be populated with all the closers. 选择销售线索并提交表单后,脚本应将其显示在带有隐藏输入的列表中(名称为Leads []和value = the_lead's_id),每个销售线索旁边将有一个下拉框(),与所有的关闭者。 After choosing and sending the second form your script will "run" all-over the leads' ids array and update each and every one of them. 选择并发送第二种形式后,您的脚本将在线索的id数组中“运行”并更新其中的每一个。

Got the idea or you want some code? 有这个主意还是想要一些代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM