因此,PHP脚本实际上与服务器建立了连接,但是以某种方式无法填充localhost上的数据库。我可能做错了什么?
script.php
<?php
$link = mysqli_connect("127.0.0.1", "root", "", "testdb");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is
great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
$klas = isset($_POST['klasDown']);
$naam = isset($_POST['naamTxt']);
$stmt = $link->prepare("INSERT INTO resultaten (Klas, Naam) VALUES (?, ?");
if (!$stmt) {
echo "false";
} else {
$stmt->bind_param("ss", $klas, $naam);
$stmt->execute();
}
mysqli_close($link);
?>
HTML:
<form id="myForm" method="post" action="script.php">
Naam: <input name="naamTxt" type="text" maxlength="512"
id="naamTxt" class="searchField"/> <br>
Klas: <select name="klasDown">
<option value="H4A" selected="selected">H4A</option>
<option value="H4B" >H4B</option>
<option value="H4C">H4C</option>
<option value="H4C">H4D</option>
<option value="H4C">V4A</option>
<option value="H4C">V4B</option>
<option value="H4C">V4C</option>
<option value="H4C">V4D</option>
</select>
<div class="row">
<div class="col-4">1. Kies 1/5 </div>
<div class="col text-center"><input type="radio" name="v1" id="v1_1"
value="Helemaal mee oneens"></div>
<div class="col text-center"><input type="radio" name="v1" id="v1_2"
value="Deels oneens"></div>
<div class="col text-center"><input type="radio" name="v1" id="v1_3"
value="Neutraal"></div>
<div class="col text-center"><input type="radio" name="v1" id="v1_4"
value="Deels mee eens"></div>
<div class="col text-center"><input type="radio" name="v1" id="v1_5"
value="Helemaal mee eens"></div>
</div>
<input type= "submit" style="text-align: center"
onclick="bereken()">
</form>
^以上是HTML表格
Bereken()函数:
function bereken() {
var e = 0;
var c = 0;
if(document.getElementById('v1_1').checked) {
c = c - 0
}else if(document.getElementById('v1_2').checked) {
c += 1;
}else if(document.getElementById('v1_3').checked) {
c += 2;
}else if(document.getElementById('v1_4').checked) {
c += 3;
}else if(document.getElementById('v1_5').checked){
c += 4;
}
var klas = li.options[li.selectedIndex].value;
var naam = document.getElementById('searchTxt').value;
alert(klas + ", " + naam);
}
^这是Javascript bereken()函数。
显然代码太多,因此必须添加一些文本。
最佳答案
更换
$klas = isset($_POST['klasDown']);
$naam = isset($_POST['naamTxt']);
与
$klas = $_POST['klasDown'];
$naam = $_POST['naamTxt'];
isset
将返回一个布尔值,而不是您期望的字符串。关于php - 为什么HTML表单不将值插入Mysql?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45827119/