PHP购物车内容打印输出

时间:2013-11-19 18:13:53

标签: php html sql postgresql xhtml

我有一个简单的商店,显示来自PostgreSQL数据库的数据:

$database  = pg_connect("host= port= ");
$query = 'SELECT * FROM table1 ';

$query .= ' ORDER BY Price';
$result = pg_query($database, $query);
echo '<form name="basket" action="basket.php" method="post">' . '<table border="1" class="center">';
echo "<tr></tr>";
while ($a = pg_fetch_row($result)) {
echo "<tr>";
echo "<td><input type='checkbox' name='games[]' value='.$id.' ></td>";
for ($j = 0; $j < pg_num_fields($result); $j++) {
    echo "<td>" . $a[$j] . "</td>";
}
echo "</tr>\n";
}
echo '</table>';
?>
<input type="submit" name="Add To Basket" value="Add To Basket" />
</form>

当选中某些复选框并点击提交时,我想在cart.php上看到已检查的产品。

我当前的cart.php代码是:

$games = $_POST['games'];
.......
var_dump($games);
print_r($games);

但是这给出了输出:

array(4) { [0]=> string(2) ".." [1]=> string(2) ".." [2]=> string(2) ".." [3]=> string(2) ".." } Array ( [0] => .. [1] => .. [2] => .. [3] => .. )

有人可以帮我解答

2 个答案:

答案 0 :(得分:1)

您正在为字段值添加.,例如value=".7."

echo "<td><input type='checkbox' name='games[]' value='.$id.' ></td>";
                                                       ^---^--

你也没有在任何地方定义$id,所以你输出的是一个未定义的变量,它会产生一个空白点。

答案 1 :(得分:1)

复选框中没有$id值,添加为$id = $a[0];

    while ($a = pg_fetch_row($result)) {
       $id = $a[0];
        echo "<tr>";
        echo "<td><input type='checkbox' name='games[]' value='$id' ></td>";
        for ($j = 0; $j < pg_num_fields($result); $j++) {
            echo "<td>" . $a[$j] . "</td>";
        }
        echo "</tr>\n";
    }
相关问题