Php isset($\u POST())在表单中返回false
我一直试图将从表单收集的数据插入数据库。我使用了$u POST(id),但它给出了一个错误,表示未定义索引。因此我使用了isset函数。它修复了未定义索引问题,但现在isset返回false,因此它总是插入变量$a(即'n')的默认值。Php isset($\u POST())在表单中返回false,php,database,forms,isset,Php,Database,Forms,Isset,我一直试图将从表单收集的数据插入数据库。我使用了$u POST(id),但它给出了一个错误,表示未定义索引。因此我使用了isset函数。它修复了未定义索引问题,但现在isset返回false,因此它总是插入变量$a(即'n')的默认值。 以下是html文件: <html> <head></head> <form action="test.php"> <input type='text' id='a'/> <input t
以下是html文件:
<html>
<head></head>
<form action="test.php">
<input type='text' id='a'/>
<input type='submit' value='sub'/>
</form>
</html>
<?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?
下面是test.php:
<html>
<head></head>
<form action="test.php">
<input type='text' id='a'/>
<input type='submit' value='sub'/>
</form>
</html>
<?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?
您需要为字段提供名称,目前来看,这些名称无效。正如其他人所说,您还需要确保设置了一个方法。下面是一个例子
<html>
<head></head>
<form action="test.php">
<input type='text' id='a'/>
<input type='submit' value='sub'/>
</form>
</html>
<?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?
<form action="test.php" method="post">
<input type='text' id='a' name='input_a'>
<input type='submit' value='sub'/>
</form>
然后,在$的输出中,POST
应该显示传入的信息。还要将method属性添加到表单中。默认情况下,它是get
。以及输入标记的name属性
<html>
<head></head>
<form action="test.php">
<input type='text' id='a'/>
<input type='submit' value='sub'/>
</form>
</html>
<?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?
<form action='test.php' method='post'>
<input type='text' name='a' id='a' />
<input type='submit' value='sub'/>
</form>
将括号用于控制结构。这就是省略它们时发生的情况。括号中没有相同的问题您没有为字段提供有效的名称。。。因此,空的$\u POST会出现问题。如果你不设置method='POST',你仍然会空手而归。等待它刚刚开始工作……method='POST'和name='input\u a'成功了。谢谢!太好了,如果一个答案帮助你解决了问题,请随意将其标记为有效答案:)