使用php从数据库中获取并显示多个复选框值

时间:2013-12-08 12:00:36

标签: php mysql

当我选择两个复选框时,我有两个复选框,我想显示与数据库相关的值,但现在我只能打印一个复选框的ID,我想显示两个数据,任何人都可以帮助锄头显示两个值,下面是我的代码:

HTML

 <input type="submit" name="Update" src="/image/exporttt.png" style="margin:0px;cursor:pointer">

<tbody>
<?php

$clientid=$_GET['clientid'];

if($clientid!=""){

$sql = mysql_query("SELECT * FROM clientnetworkpricehistory");


while($rows=mysql_fetch_array($sql))
{
if($alt == 1)
        {
           echo '<tr class="alt">';
           $alt = 0;
        }
        else
        {
           echo '<tr>';
           $alt = 1;
        }

echo ' 
        <td id="CPH_GridView1_clientid" style="width:140px" class=" clientid '.$rows["net_id"].'">'.$rows["clientid"].'</td>
        <td id="CPH_GridView1_country" style="width:160px" class=" country '.$rows['net_id'].'">'.$rows["country"].'</td>       
        <td id="CPH_GridView1_networkname" style="width:156px" class="networkname '.$rows["net_id"].'">'.$rows["networkname"].'</td>
        <td id="CPH_GridView1_mccmnc" style="width:250px" class=" mcc'.$rows["net_id"].'">'.$rows["mcc"].'</td> 
        <td id="CPH_GridView1_mccmnc" style="width:250px" class=" mnc '.$rows["net_id"].'">'.$rows["mnc"].'</td>         
        <td id="CPH_GridView1_oldprice" style="width:320px" class=" oldprice '.$rows["net_id"].'">'.$rows["pricefrom"].'</td>
        <td id="CPH_GridView1_newprice" style="width:129px" class=" newprice '.$rows["net_id"].'">'.$rows["priceto"].'</td> 
        <td id="CPH_GridView1_oldroute" style="width:143px" class="oldsupplierroute '.$rows["net_id"].'">'.$rows["routefrom"].'</td>    
        <td id="CPH_GridView1_newroute" style="width:143px" class=" newsupplierroute '.$rows["net_id"].'">'.$rows["routeto"].'</td>     
        <td id="CPH_GridView1_comments" style="width:143px" class=" comments '.$rows["net_id"].'">'.$rows["status"].'</td>      
        <td id="CPH_GridView1_from" style="width:143px" class=" fromdate '.$rows["net_id"].'">'.date('d.m.Y H:i', $rows["datetime"]).'</td>         

        <td style="width:65px" class=" '.$rows["net_id"].'"><input type="checkbox" name="chk1" value=" '.$rows["net_id"].'"/></td>

        </tr>';
}
}
?>

</tbody>

jssearch.php

<?php
 //connecting to db     
$variable=$_POST['chk1'];
foreach ($variable as $variablename)
{
$sql_select="SELECT * from clientnetworkpricehistory where net_id=$variablename";
$queryRes = mysql_query($sql_select);
print"$sql_select";
}
echo "<table border='1'>
<tr>
<th>country</th>
<th>networkname </th>
<th>mcc</th>
<th>mnc</th> 
        <th>datetime </th>
</tr>";

while($row = mysql_fetch_array($queryRes))
{
echo "<tr>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['networkname'] . "</td>";
echo "<td>" . $row['mcc'] . "</td>";
echo "<td>" . $row['mnc'] . "</td>"; 
echo "<td>" . $row['datetime'] . "</td>"; 
echo "</tr>";
}
echo "</table>";
?> 

我的输出是(我检查了两个复选框,我正在尝试打印两个复选框ID,它只显示一个可以任何人帮我怎么做)

56
Warning: Invalid argument supplied for foreach()

2 个答案:

答案 0 :(得分:4)

在HTML中使用name="chk1[]",以便复选框将作为数组发布。

除此之外:

  1. 不推荐使用mysql_ *函数,请考虑使用mysqli_ *或PDO
  2. where net_id=$variablename"
  3. 上的SQL注入很容易被攻击

答案 1 :(得分:1)

我同意彼得的意见,但你也可能想解决这个小错字:

'.$rows['net_id'].'">'.$rows["country"].'

这是你拥有所有echo的那个大<td>的第2行。 $rows['net_id']应为双引号。

但你知道......:)