【问题标题】:i want to add/pass the value to my radio button to database我想将值添加/传递到我的单选按钮到数据库
【发布时间】:2021-10-26 17:49:30
【问题描述】:
<body>
    <div>
        <form id = "login" action="#" method="POST" >
            <input type = "text" name = "Name" required placeholder="Name" class="input"><br />
                <div>
                    <?php
                        $sql = "SELECT * FROM cand ";
                            $result  = $con->query($sql);
                        if ($result->num_rows > 0) {
                            echo "<table border='2'". 'class="container2"'.">
                            <tr>
                            <th>Number</th>
                            <th>Name</th>
                            <th>Photo</th>
                            <th>Position</th>
                            <th>Voters</th>
                            <th>Vote</th>
                            </tr>";
                                while($row = $result->fetch_assoc()){
                                    echo "<tr>
                                    <td>" . $row["ID"]."</td>
                                    <td>" . $row["cName"]. "</td>
                                    <td>" ."<img src='img/" . $row["cPic"]. "'>"."</td>
                                    <td>" . $row["Position"]."</td>
                                    <td>" . $row["Vcount"   ]."</td>
                                    <td>".'
                                    <input type="checkbox" id="vote" name="vote" value="vote">'."</td>
                                    </tr>";
                        }
                        echo "</table>";  
                        }
                    ?>
    </div>
            <button name = "login" type="submit">Vote</button> 
        </form>
    </div>

</body>
</html>
<?php 
 
  if(isset($_POST['login'])){
    $vote = $_POST['vote'];
    $sql = "INSERT into cand (Vcount) VALUES ('$vote')";
    $con->query($sql);
    }

?>

每次我提交时,它都不会将单选按钮的值添加到名为cand 的数据库表中,列Vcount。它仍然显示0,但每次我提交表单时它都会添加一个新的空白行。

【问题讨论】:

  • 我忘记从复选框更改为单选类型
  • idname 应该是唯一的。 radio 类型更改已解决问题?您对 SQL 注入持开放态度。此外,如果你想update 你不应该有insert 查询,但不清楚是否不正确。
  • 是的,ID是AI,名字是唯一的

标签: php html mysql


【解决方案1】:

你没有正确连接字符串,你应该这样做:

$sql = "INSERT into cand (Vcount) VALUES ('" . $vote . "')";

【讨论】:

  • 他们所拥有的和这个一样有效......都不应该使用。
猜你喜欢
  • 1970-01-01
  • 2012-04-12
  • 2021-09-17
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多