我创建了一个网站,您可以在单击提交按钮后将单选按钮数据插入数据库。
然而,问题是每当我点击提交按钮时,它会插入两个重复的值而不是一个。
我的代码如下:
<form action="test.php" method ="post" >
<b> what is you fav sport ؟ </b>
</br>
<input type="radio" name="sport1" value="football"> football
<input type="radio" name="sport1" value="basketball"> football
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
mysqli_query($conn, "set names 'utf8'");
?>
<input type="submit" name="submit" value="submit"/>
<?php
if(isset($_POST['submit']) && !empty($_POST)){
$sport1 = $_POST['sport1'];
$SQL = "INSERT INTO userTable (user_q1) VALUES ('$sport1')";
$result = mysqli_query($conn, $SQL);
if ($conn->query($SQL) === TRUE) {
echo "New record created successfully";
}else{
echo "Error: " . $SQL . "<br>" . $conn->error;
}
$conn->close();
}
?>
</form>
任何帮助将不胜感激。
答案 0 :(得分:1)
$result = mysqli_query($conn, $SQL)
//or
$conn->query($SQL)
mysqli_query和$ conn-&gt; query()用于相同目的。
只需删除任何一个声明,即可获得所需内容