0

我正在尝试在选定的汽车之间创建一个比较选项。

<?php
if(isset($_POST['compares'])) {
$id_nums = array($_POST['cBox']);
//$id_nums = array(1,6,12,18,24);
$id_nums1 = implode(", ", $id_nums);

$query = "SELECT * FROM wp_cars WHERE id in ($id_nums1)";
$cCars = mysql_query($query) or mysql_error();

while($car = mysql_fetch_array($cCars)) {
    echo $car['cartitle']."<br/>";
    echo $car['saleprice']."<br/>";
}
} else {
$query1 = "SELECT * FROM wp_cars";
$allcars = mysql_query($query1) or die(mysql_error());  `

while($car1 = mysql_fetch_array($allcars)) {
    echo "<input type='checkbox' value=".$car1['id']." name='cBox[]' />";
    echo $car1['cartitle']."<br/>";
    echo $car1['saleprice']."<br/>";
}

}
?>

如何根据复选框选择传递复选框名称(cBox[])数组。

<form action="compares.php" method="post">
<button name="compares">Select Cars to Compare</button>
</form>
4

2 回答 2

0

从您的 HTML 部分,以这种方式发送输入:

<form action="compares.php" method="post">
<?php foreach (mysql_fetch_array($allcars) as $car: ?>
    <input type="checkbox" value="<?php echo $car['id']; ?>" name="cBox[]" />
    <?php echo $car['cartitle']; ?><br />
    <?php echo $car['saleprice']; ?><br />
<?php endforeach; ?>
</form>

在您的 PHP 部分,以这种方式接收输入:

$id_nums = implode(",", $_POST['cBox']);
于 2012-10-03T11:02:20.740 回答
0
$id_nums = array($_POST['cBox']);

$_POST['cBox']已经是一个数组,您正在制作一个二维数组。正在做

$id_nums1 = implode(", ", $_POST['cBox']);

会做你想做的。尽管它对 SQL 注入很开放。

于 2012-10-02T22:32:06.263 回答