[英]Inserting data into a MYSQL database
I am having trouble inserting data into the database 'justrated'.我在将数据插入“justrated”数据库时遇到问题。 Once the user has entered their business name it should create a new entry in the table 'businesses'.
一旦用户输入了他们的企业名称,它应该在“企业”表中创建一个新条目。 For some reason I cannot get it so that the data is entered in the table.
出于某种原因,我无法得到它,以便将数据输入表中。 Any advice is gladly appreciated.
任何建议都非常感谢。
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>
One of your problems is that $_POST['BusinessName']
is empty because the form was submitted using a GET request, not a POST request.您的问题之一是
$_POST['BusinessName']
为空,因为表单是使用 GET 请求而不是 POST 请求提交的。 The method=POST
attribute goes on the <form>
element. method=POST
属性位于<form>
元素上。 Eg:例如:
<form
method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit">
</form>
Also, you should escape the data properly before you insert it into the database:此外,您应该在将数据插入数据库之前正确转义数据:
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('" .
$conn->real_escape_string ($_POST['BusinessName']
) . "' )";
Furthermore, in these two lines:此外,在这两行中:
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
you try to execute the same query twice using both the MySQL and MySQLi extension.您尝试使用 MySQL 和 MySQLi 扩展执行相同的查询两次。 You should remove the first line.
您应该删除第一行。
Html:网址:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit" >
</form>
Php: Use Php:使用
$conn->query($sql); not mysql_query()
HTML Code HTML代码
<form method="post" action="test1.php">
<input type="text" name="BusinessName" >
<input type="Submit" value="submit" name="submit" >
</form>
PHP Code 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();
}
Don't mix the mysql
& mysqli
....不要混合使用
mysql
和mysqli
....
hello please check this one i hope this working for you你好,请检查这个我希望这对你有用
$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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.