【发布时间】: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,但每次我提交表单时它都会添加一个新的空白行。
【问题讨论】:
-
我忘记从复选框更改为单选类型
-
id和name应该是唯一的。radio类型更改已解决问题?您对 SQL 注入持开放态度。此外,如果你想update你不应该有insert查询,但不清楚是否不正确。 -
是的,ID是AI,名字是唯一的