0

我正在尝试创建一个表单,该表单允许用户编辑多个成员的信息,获取该信息,并通过 URL 传递它来编辑数据库。到目前为止我有这个:

 echo "<form id='memberEditor' method='post' action='massEditor.php'>"
 $result = mysql_query("SELECT * FROM `user_trials` ORDER BY `grade` ASC, `lastName` ASC");

            while($row = mysql_fetch_array($result)){
                $studentID = $row['studentid'];
                $ID = array();
                array_push($ID, $studentID);                                        
                $firstName = $row['name'];
                $lastName = $row['lastName'];
                $fullName = $firstName." ".$lastName;
                $money = $row['money'];
                $moneyValue = array();
                array_push($moneyValue, $money);
                $waiver = $row['waiver'];
                $waiverValue = array();
                array_push($waiverValue, $waiver);
                echo "<tr>";
                echo "<td>$studentID</td>";
                echo "<td>$fullName </td>";                 
                echo "<td><input type='checkbox' name='money' id='money'".(($money == 'yes')?'checked ':' ')."value='yes' /></td>";
                echo "<td><input type='checkbox' name='waiver' id='waiver'".(($waiver == 'yes')?'"checked"':' ')."value='yes' /></td>";
                echo "</tr>";

            }
            $queryID = http_build_query($studentID);
            $queryMoney = http_build_query($moneyValue);
            $queryWaiver = http_build_query($waiverValue);

在页面顶部,我试图获取数组以编辑数据库:

 ini_set('display_errors', 'On');
error_reporting(E_ALL);

  $memberNumbers = count($ID);
  for($i = 0; $i < $memberNumbers; $i++) {
      $studentid = $ID[$i];         
      $result = mysql_query("SELECT * FROM `user` WHERE `studentid`='$studentid' LIMIT 1");
      $news = mysql_fetch_array($result);   
      $cash = $moneyValue[$i];
      $permission = $waiverValue[$i];
      mysql_query("UPDATE `user_trials` SET `money`='$cash', `waiver`='$permission' WHERE `studentid`='$studentid' LIMIT 1");
      header("Location: view_members.php"); 
}

我试图将数据从 http_build_query 传递到 URL,然后在顶部检索它,但我不知道应该如何将多个数组添加到 URL 中,然后将三个数组分开以获得三个不同的数组从中获取信息。谢谢!

4

1 回答 1

0

您可以使用序列化和 urlencoding 通过表单标签的隐藏标签元素发送多个数组,并使用 urldecoding 和反序列化来显示数据。例如:

对于发送数据,您可以尝试:

<?php
    $array1["a"] = "Element 1 Of Array 1";
    $array1["b"] = "Element 2 Of Array 1";
    $array1["c"] = "Element 3 Of Array 1";
    $array1["d"] = "Element 4 Of Array 1";
    $str1 = serialize($array1);  //****** To Serialize The Array ****
    $strenc1 = urlencode($str1); //****** For URL Encoding **********

    $array2[1] = "Element 1 Of Array 2";
    $array2[2] = "Element 2 Of Array 2";
    $array2[3] = "Element 3 Of Array 2";
    $array2[4] = "Element 4 Of Array 2";
    $str2 = serialize($array2);  //****** To Serialize The Array ****
    $strenc2 = urlencode($str2); //****** For URL Encoding **********
?>

<form method="post" action="target.php">
    <!-- Passing Array 1 -->
    <input type="hidden" name="arr1" value="<?php echo $strenc1; ?>" /> 
    <!-- Passing Array 2 -->
    <input type="hidden" name="arr2" value="<?php echo $strenc2; ?>" />
    <input type="submit" />
</form>

然后您可以使用以下命令在目标页面上打印数据:

<?php
    $a = unserialize(urldecode($_POST['arr1']));
    foreach($a as $x)
        echo $x . "<br />";

    $a = unserialize(urldecode($_POST['arr2']));
    foreach($a as $x)
        echo $x . "<br />";
?>

希望这能解决你的问题。:)

于 2013-03-08T03:09:00.257 回答