更新多行(PHP + MySQL)

时间:2012-02-27 16:57:17

标签: php mysql

我正在研究一个潜在客户管理系统 - 随着它的数据库的增长,出现了对更多批量功能的需求 - 不幸的是我遇到了其中一个。数据库存储了许多不同的潜在客户 - 每个潜在客户被分配给特定的密切关系;因此,数据库为每个潜在客户存储潜在客户ID,名称,更接近的名称和其他信息。主引导列表显示每个引导旁边的复选框,它将引导ID提交到数组中:

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

现在这一切都转到了以下页面:

<?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>";

?>

我希望能够使用此页面上的表单选择离数据库更近的表格 - 然后将其分配给更接近已选择的每个潜在客户。这是我不知道如何继续的地方。

2 个答案:

答案 0 :(得分:1)

实际上 - 我明白了。我不知道为什么我没有早点想到它。首先,我将原始的$ 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"; };
?>

此前更新的页面:     

$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\">";

?> 

答案 1 :(得分:0)

选择潜在客户并提交表单后,您的脚本应在带有隐藏输入的列表中显示它们(名称= leads []和值= the_lead's_id),每个潜在客户旁边会有一个下拉框( )将填充所有关闭器。 选择并发送第二个表单后,您的脚本将“遍历”lead'ids数组并更新每个表单。

有想法还是想要一些代码?