這是我寫的代碼:如何在數據庫中提交單選按鈕答案?
的index.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="db.php" method="post">
YES:<input type="radio" name="YES" value="YES">
NO:<input type="radio" name="NO" value="NO" >
<input type="submit" name="submit value="submit">
</form>
</body>
<html>
db.php中
<?php
$con = mysqli_connect('localhost','my user id','my password');
if(!con)
{ echo 'not connected to server';}else { echo 'something else is wrong' ;}
if(!mysqli_select_db($con,'my user id')
{ echo 'Database error selection';}
$YES = $_POST['YES'];
$NO = $_POST['NO'];
$sql = INSERT INTO users (YES,NO) VALUES ('$YES','$NO');
if(!mysqli_query($con,$sql))
{ echo 'Answer not submitted please try again!!!!!!!!';}
else{
echo 'Your answer successfully submitted \n Thanks for participating';}
header(" refresh:2; url=index.html");
?>
也有一些是錯誤的了。 任何人都可以請告訴什麼是錯的?
注意:如果用戶提交YES,那麼在數據庫中是會得到int值作爲2 如果1用戶提交的NO,然後在數據庫中沒有爲1
它究竟有什麼問題?除了不要關閉這個屬性'name =「submit」' – Lixus
首先,你必須給這兩個單選按鈕輸入相同的名字,以供參考http://stackoverflow.com/questions/15868935/adding-radio -button-values-to-mysql-table-using-php –
你的單選按鈕可能是複選框,因爲選擇一個不會取消選擇其他選項。無論如何您都不會將數字值與它們相關聯。 –