【问题标题】:using radio button to insert in database table using php使用单选按钮使用 php 在数据库表中插入
【发布时间】:2017-12-19 08:44:52
【问题描述】:

我有这段代码,我想在数据库中插入项目,但它没有插入。 HTML5 代码:

<form action="simple.php" method="post">
<input type="radio" name="games" value="1" size="20"<?php print $itemname;?
>Buy It</br>
<input type="Submit"name="Submit1" value="Add to cart"></center></a></div> 
</div></td></form>

php:

if (isset($_POST['Submit1'])) {

$selected_radio = $_POST['games'];

if ($selected_radio == '1') {

 $un ='checked';

if ($un='checked')

$value="whatever value";
$sql="INSERT INTO cartorders(id,item,price) VALUES ('$id','$value',250)";
$res=mysqli_query($dbhandle,$sql);

【问题讨论】:

  • 我没有收到任何错误,这就是为什么我不知道自己的错误

标签: php database html mysqli


【解决方案1】:

使用 id 作为自动增量 ID

   <?php

    if (isset($_POST['Submit1'])) {
        $selected_radio = $_POST['games'];
        if ($selected_radio == '1') {
            $value = "whatever value";
            $sql   = "INSERT INTO cartorders(item,price) VALUES ('" . $value . "',250)";
            $res   = mysqli_query($dbhandle,$sql);
        }
    }
     ?>

【讨论】:

    【解决方案2】:

    如果你获得了 $id 的价值,并且它是唯一的,那么它应该可以工作。但正如建议的那样,最好在您的数据库中将 ID 设为自动增量字段,如果您没有唯一字段,则在更新时会出现问题。

    【讨论】:

      猜你喜欢
      • 2013-12-10
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 2022-01-22
      • 2016-02-17
      • 1970-01-01
      相关资源
      最近更新 更多