php - 试图让 PHP 复选框保存 MySQL 表中的数据

标签 php mysql database checkbox

我必须为一项作业进行基于网络的结账,但遇到了一个问题,我导入了一个数据库,并在一个表格中设置了数据,并在旁边添加了复选框。我需要使用 session 将引用号(首先存储在数组中)带到另一个页面上。通过使用 var_dump,我似乎无法从表中选择的内容中得到任何东西。 代码:

按钮代码

<p><tr>
<input type="submit" name="Select" id="Select" value="Add Selected To Cart"/>  
</tr></p>

访问数据库代码(为安全更改值)

<?php
Accesses database
$con=pg_connect("host=hostname port=portnumbers
    dbname=name user=user password=password");
    if (!$con)
    {
        die('Could not connect to database');
    }
?>

数据库展示

//Creates table
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Title</th>
<th>Platform</th>
<th>Description</th>
<th>Price</th>
<th>Buy</th>\n";

while($row = pg_fetch_array($res)){
echo"<tr>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo '<td><input type="checkbox" name="selected[]" value="' . $row['0'] .  '" /></td>';      
  echo"</tr>";    
  }

echo"</table>";
?>

最佳答案

好吧,我认为你可以如下操作,因为你正在尝试改进购物车:

//Use the `$_SESSION` var to hold the values
foreach ($_POST['selected'] as $item)
    $_SESSION['cart'][$item] = $item; 

由于项目的 id 将被索引,每次执行提交时:

  • 如果该项目存在,将被替换
  • 如果该项目不存在,将添加

为了删除项目,您应该使用“查看购物车”页面,然后显示项目:

foreach ($_SESSION['cart'] as $item)
    echo '<a href="delete-item?id=' . $item . '">' . $item . '</a>'; 

关于php - 试图让 PHP 复选框保存 MySQL 表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13633481/

相关文章:

php - Zend Framework 2 如何实现用户登录?

mysql - 带有/计算数据的自连接 SQL 语句

mysql - MySQL 中的用户是谁?

php - 大多数客户都知道 CMS 是什么吗?

php - 阻止谷歌索引成员(已将其个人资料设置为私有(private))个人资料

php - 将 mySQL 数据库的日志文件日期时间格式从 12 小时制批量转换为 24 小时制

php - 构建 MySQL 查询(meta_key/meta_value 表)

php - 从mysql表中选择行和最大值

sql - Oracle create table with foreign key 错误 - 标识符无效

mysql - 如何将 BGL 转换为 MySQL 表?