从具有多个联接的表单更新数据库

时间:2013-01-10 20:39:40

标签: php

我有一个团队,领导者正在更新团队中的成员。 roleID为11的成员正在等待,我想根据下拉列表中选择的选项更改其rolesID。

以下是表格:

<form action='updatePendingMembers.php' method='post' onsubmit='return checkForm()'>
    <?php
    $pendingMembers = "SELECT m.id, concat(fname, ' ', lname) as mName, email, m.rolesID FROM member m join member_to_groups on m.id = member_to_groups.memberID left join groups on member_to_groups.groupsID = groups.id WHERE m.rolesID = 11 AND groups.memberID = " . $_SESSION['siteMemberId'];
    $rsPendingMembers = mysql_query($pendingMembers);
    //if(!$rsPendingMembers)
        //echo 'the query is: ' . $pendingMembers . '<br/>and the error: ' . mysql_error();
    while($get_row = mysql_fetch_assoc($rsPendingMembers)){
    ?>
    <tr>
      <td colspan="3" class="Sub_Header">Pending Group members</td>
    </tr>
    <tr>
      <td colspan="3"><span class="Maintext"><?php echo $get_row['mName']?></span>
    <select name="rolesID" class="lowermenu" id="rolesID">
        <option value="12">Accept</option>
        <option value="11">Decline</option>
    </select>
      <a href="mailto:<?php echo $get_row['email']?>"><span class="lowermenu">send pending member message</span></a></td>
        </tr>
    <?php } ?>
    <td><span class="Sub_Header">
        <input name="update_pending_member_button" type="submit" id="update_pending_member_button" value="Update Member">
      </span></td>
      </form>

到目前为止,这是updatePendingMembers.php文件:

<?php
session_start();
include("global.php");
$myQuery1 = "UPDATE member SET rolesID = " .  mysql_real_escape_string($_POST['rolesID']) WHERE id = ;
$rs1 = mysql_query($myQuery1);
?>

如何将ID设置为等于所选成员以及该特定组中的成员?感谢。

1 个答案:

答案 0 :(得分:1)

对表单元素使用类似数组的名称:

<select name="rolesID[<?php echo $get_row['id']; ?>]" class="lowermenu" id="rolesID">
    <option value="12">Accept</option>
    <option value="11">Decline</option>
</select>

您的$_POST数据将如下所示:

$_POST['rolesID'] = array(
   '1' => '11',
   '2' => '12'
);

您可以遍历它并更新每条记录:

foreach ($_POST['rolesID'] as $id => $role) {
    $id = (int)$id;
    $role = (int)$role;
    $sql = "UPDATE member SET roleID = {$role} WHERE id = {$id}";
}