我想选择多个成员分配给一个组! 目前,我可以选择一名成员,我可以将他分配到一个组。 我想选择几个成员并将它们分配给一个小组。
<?php
include 'connection.php';
if( isset( $_POST['add_member'] ) )
{
$member_id = $_POST['member_id'];
$group_id = $_POST['group_id'];
$query="SELECT `id` FROM `member_group`
WHERE `member_id` = $member_id AND `group_id` = $group_id";
if(!mysqli_num_rows(mysqli_query($connection,$query)))
{
$query="INSERT INTO `member_group`(member_id,group_id) VALUES( $member_id,$group_id)";
mysqli_query($connection,$query) or die(mysqli_errno($connection));
echo "<div class='alert alert-info'>Member Assigned. </div>";
}
}
elseif( isset( $_POST['remove_member'] ) )
{
$member_id = $_POST['member_id'];
$group_id = $_POST['group_id'];
$query="DELETE FROM `member_group`
WHERE `member_id` = $member_id AND `group_id` = $group_id";
$rows = mysqli_query($connection,$query);
if(!mysqli_affected_rows($connection))
{
echo "<div class='alert alert-info'>Member Already Deleted. </div>";
}
else
{
echo "<div class='alert alert-info'>Member Deleted. </div>";
}
}
?>
<form action="" method="POST">
<label> Add Member to the group: </label>
<select name="member_id" required="required" name="selectionField" multiple="yes">
<option value="">Select a Member</option>
<?php
$member_query = mysqli_query($connection, "select id, member_name , fname , email , mobile from `member`");
while($member_data = mysqli_fetch_object($member_query) ){
echo "<option value='$member_data->id'>$member_data->member_name</option>";
}
?>
</select>
<input type="submit" name="add_member" value="Add" class="btn btn-info" />
<input type="submit" name="remove_member" value="Remove" class="btn btn-info" />
<select name="group_id" required="required" name="selectionField" multiple="yes">
<option value="">Select a Group</option>
<?php
$group_query = mysqli_query($connection, "select id, name from `group`");
while($group_data = mysqli_fetch_object($group_query) ){
echo "<option value='$group_data->id'>$group_data->name</option>";
}
?>
</select>
<br />
</form>
答案 0 :(得分:0)
而不是:
<select name="member_id" required="required" name="selectionField" multiple="yes">
执行:
<select required="required" name="selectionField[]" multiple="yes">
这样你就告诉PHP,select是一个数组,其位置与用户选择的元素一样多。
获取数组中的信息:
$member_id = $_POST['selectionField'];
for ($i=0;$i<count($member_id);$i++)
{
$currentMember = $member_id[$i];
// Your code to insert the member in the DB
}
答案 1 :(得分:0)
使用multiple attribute选择复选框或数组。
$members_id = is_array($_POST['add_member'])
? $_POST['add_member']
: array($_POST['add_member'];
$check_query = "SELECT `id` FROM `member_group` WHERE `member_id` IN(".implode(',', $members_id).") AND `group_id` = $group_id";
$already_added = array();
while($member = mysqli_fetch_object($check_query) ){
$already_added[] = $member->id
}
$members_id = array_diff($members_id, $already_added);
if(count($members_id) > 0) {
// Add members to DB
$query = 'INSERT INTO `member_group`(member_id,group_id) VALUES';
foreach($remove_member as $member_id) {
$query .= " ($member_id, $group_id)";
}
$member_query = mysqli_query($query);
}
第一个查询已经添加了用户,然后将这些数组与$ members_id进行比较。那些不在数据库中的人被添加(第二个查询)。
如果您不保护代码免受SQL injection的攻击,请BTW 。即使用mysqli_real_escape_string。