Why does merging these arrays cause an error?
My front-end and back-end code is listed below. It's working perfectly. And from this page I'm sending 2 arrays to another page. There I want to merge the arrays and insert into a MySQL table. Here it my front page.
<?php
include("db.php");
$team_id=$_GET['team_id'];
$sql1=mysql_query("select members from team where team_开发者_StackOverflowid='$team_id'");
$sql=mysql_query("select user_id,full_name from users where school_id= '1'");
while($array=mysql_fetch_assoc($sql))
{
$x[] = $array['user_id'];
}
echo "</br>";
$row1=mysql_fetch_array($sql1);
$member=unserialize($row1['members']);
echo "<form action='ad_team_mem_db.php' method='post'>";
echo "<select name='mem[]' size='25' multiple='multiple'>";
foreach(array_diff_assoc($x ,$member) as $item)
{
$sqlf=mysql_query("select user_id,full_name from users where school_id=1 and user_id='$item'");
if($roww=mysql_fetch_array($sqlf)){
echo "<option value='".$roww['user_id']."'>".$roww['full_name']."</option>";
}
}
echo "</select></br>";
echo "<input type='hidden' value='$member' name='member[]'>";
echo "<input type='submit'>";
echo "</form>";
echo "</br>";
?>
And this is my backend page. In this page I couldn't merge 2 arrays.
<?
include("db.php");
$mem=$_POST['mem'];
$member=$_POST['member'];
**$members = array_merge ($member, $mem);**
?>
Error message,
Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\AppServ\www\teen\ad_team_mem_db.php on line 5
At the point you do this:
echo "<input type='hidden' value='$member' name='member[]'>";
$member
is a PHP array, and what you'll end up in the form will look like:
<input type='hidden' value='Array' name='member[]'>
which upon POSTing will be a simple string, which is why your array_merge is failing.
I think you probably need to unserialise the POST vars before merging.
try
$members = array_merge ((array) $member, (array) $mem);
精彩评论