我有一个包含许多插入内容的表单,其中包括许多单选按钮对,由于某些原因,在选择时它们在数据库中没有显示任何更改。我使用与单选按钮相同的方法,就像我用于任何其他输入一样。
HTML表单
<form action="" method="post">
<input type="hidden" name="apartmentID" value="<?php get_text_byID("SELECT apartmentID from apartment_details WHERE apartmentID=?", "apartmentID"); ?>"/>
<input id="name" name="name" value="<?php get_text_byID("SELECT name from apartment_details WHERE apartmentID=?", "name"); ?>"/> <div id="amenities">
<table>
<tr>
<th>Y</th>
<th>N</th>
<th>Amenity</th>
</tr>
<tr>
<td><input type="radio" value="1" name="wifi"/></td>
<td><input type="radio" value="0" name="wifi"/></td>
<td>Wifi</td>
</tr>
<tr>
<td><input type="radio" value="1" name="phone"/></td>
<td><input type="radio" value="0" name="phone"/></td>
<td>Phone</td>
</tr>
<tr>
<td><input type="radio" value="1" name="TV"/></td>
<td><input type="radio" value="0" name="TV"/></td>
<td>Television</td>
</tr>
<tr>
<td><input type="radio" value="1" name="washing_machine"/></td>
<td><input type="radio" value="0" name="washing_machine"/></td>
<td>Washing machine</td>
</tr>
<tr>
<td><input type="radio" value="1" name="ac"/></td>
<td><input type="radio" value="0" name="ac"/></td>
<td>Air Conditioner</td>
</tr>
</table>
</div>
<button type="submit" name="apartment_details">Save Details</button>
</form>
PHP
if (isset($_POST['apartment_details'])) {
include 'connect.php';
$apartmentID = (isset($_POST['apartmentID']) ? $_POST['apartmentID'] : null);
$name = (isset($_POST['name']) ? $_POST['name'] : null);
$wifi = (isset($_POST['wifi']) ? $_POST['wifi'] : null);
$phone = (isset($_POST['phone']) ? $_POST['phone'] : null);
$TV = (isset($_POST['TV']) ? $_POST['TV'] : null);
$washing_machine = (isset($_POST['washing_machine']) ? $_POST['washing_machine'] : null);
$ac = (isset($_POST['ac']) ? $_POST['ac'] : null);
$stmt = $conn->prepare("INSERT INTO apartment_details (apartmentID, name, wifi, phone, TV, washing_machine, ac) VALUES (?, ?, ?, ?, ?, ? ,? ) ON DUPLICATE KEY UPDATE apartmentID = VALUES(apartmentID), name= VALUES(name), wifi = VALUES(wifi), phone = VALUES(phone), TV = VALUES(TV), washing_machine = VALUES(washing_machine), ac = VALUES(ac)");
$stmt->bind_param('sssssss', $apartmentID, $name, $wifi, $phone, $TV, $washing_machine, $ac);
$stmt->execute();
$conn->close();
}
答案 0 :(得分:0)
您的单选按钮设置为值0或1;它们与复选框不同。请尝试此操作(编辑设置值:
if ($_POST['wifi'] == 0) {
$wifi = 0;
}
else {
$wifi = 1;
}
注意:您可以将它们设置为表单中的默认选定值,或者只是从数据库中提取值并将其作为表单中的选定值。