【发布时间】:2018-05-30 17:57:14
【问题描述】:
我一直在尝试从我从表单收集的数据中将数据插入数据库。我使用了 $_POST(id) 但它给出了一个错误,说未定义的索引。所以我使用了 isset 函数。它修复了未定义的索引问题,但现在 isset 返回 false,因此它始终插入变量 $a(即'n')的默认值。
这是html文件:
<html>
<head></head>
<form action="test.php">
<input type='text' id='a'/>
<input type='submit' value='sub'/>
</form>
</html>
这里是 test.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$a="n";
if (isset($_POST['a']))
$a=$_POST['a'];
$sql = "INSERT INTO test
VALUES ('$a')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
whats wrong?
【问题讨论】:
-
在控制结构中使用方括号。这就是您省略它们时会发生的情况。
-
括号不一样
-
你没有为你的字段提供有效的名字......因此空的 $_POST 问题。