我无法将数据插入数据库'justrated'。一旦用户输入了他们的商业名称,它应该在表'business'中创建一个新条目。出于某种原因,我无法得到它,以便在表格中输入数据。任何建议都很高兴。
CODE:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form>
<input type="text" name="BusinessName" method="POST">
<input type="Submit" value="submit" name="submit" method="POST">
</form>
<?php
if (isset($_POST["submit"])){
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('".$_POST['BusinessName']."' )";
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
答案 0 :(得分:1)
您的一个问题是$_POST['BusinessName']
为空,因为表单是使用GET请求而非POST请求提交的。 method=POST
元素位于<form>
元素上。例如:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit">
</form>
此外,在将数据插入数据库之前,您应该正确地转义数据:
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('" . $conn->real_escape_string ($_POST['BusinessName']) . "' )";
此外,在这两行中:
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
您尝试使用MySQL和MySQLi扩展两次执行相同的查询。你应该删除第一行。
答案 1 :(得分:0)
Html:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit" >
</form>
腓: 使用
$conn->query($sql); not mysql_query()
答案 2 :(得分:0)
HTML代码
<form method="post" action="test1.php">
<input type="text" name="BusinessName" >
<input type="Submit" value="submit" name="submit" >
</form>
PHP代码
if (isset($_POST["submit"]))
{
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."' )";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
不要混用mysql
&amp; mysqli
...
答案 3 :(得分:0)
您好,请检查一下,希望这对您有用
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."' )";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}