我有index.html在send.php中发布数据 send.php代码是 表格数据正确命名&发送页面没有错误
<?php
$servername = "localhost";
$username = "Blah Blah";
$password = "Blah Blah";
$dbname = "Blah Blah";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$name = $_POST['name'];
$famil = $_POST['famil'];
$email = $_POST['email'];
$amount = $_POST['amount'];
$sql = "INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')";
mysqli_close($conn);
但查询无法将数据插入db!
为什么post方法在这里工作?
答案 0 :(得分:2)
问题是你不是querying - mysqli_query()
,这是必需的部分。
您当前的代码确实有效,但SQL由于缺少mysqli_query()
而无声地失败。
$sql = mysqli_query($conn,"INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')");
确保您的表单元素也已命名,并且您的表单是POST方法。 (洞察)
即:name="name"
,name="famil"
等和<form method="post" action="handler.php">
mysqli
with prepared statements或PDO with prepared statements,将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告应仅在暂存时完成,而不是生产。
您也可以or die(mysqli_error($conn))
使用mysqli_query()
“发送页面没有错误”
要使用现有代码提供额外保护,请使用:
$name = stripslashes($_POST['name']);
$name = mysqli_real_escape_string($conn, $_POST['name']);
这样做也有效:
$sql = "INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')";
mysqli_query($conn,$sql);
或
if (mysqli_query($conn, $sql)) {
echo "Data entered successfully.";
} else {
echo "Error entering data: " . mysqli_error($conn);
}