我一直在尝试从我从表单收集的数据中将数据插入到数据库中。我使用$ _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>
和heres 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?
答案 0 :(得分:1)
还要将method属性添加到表单中。默认情况下为get
。以及输入标记的name属性
<form action='test.php' method='post'>
<input type='text' name='a' id='a' />
<input type='submit' value='sub'/>
</form>
答案 1 :(得分:0)
您需要为字段提供名称,因为它们无效。正如其他人所说,您还需要确保设置方法。以下是一个例子
<form action="test.php" method="post">
<input type='text' id='a' name='input_a'>
<input type='submit' value='sub'/>
</form>
然后在$_POST
的输出中应显示您的传入信息。