gpt4 book ai didi

php - 有趣的是,数据没有插入到 mysql 表中

转载 作者:行者123 更新时间:2023-11-29 07:35:46 25 4
gpt4 key购买 nike

我正在制作一个评论表格,其中将从文本框中获取评论并将其插入数据库。但问题是当我尝试运行代码时出现以下错误:

Warning: mysqli::query(): Couldn't fetch mysqli in C:\wamp64\path\to\file on line 12

我为此编写的代码如下:

     <?php  
require_once('data.php');
require_once('connect.php');
$personName = $_GET['name'];
$value = $_POST['review'] ?? '';
echo "<p>".$personName;
echo "<p>".$value;



$sql = "INSERT INTO reviews (name, review) VALUES ('$personName', '$value')";
if($connection->query($sql) === TRUE) {
echo "Inserted";
} else {
echo "Not inserted";
}

?>
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button[type=submit] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}

input[type=submit]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<form class="" method="post" >
<label for="form-element"></label>
<input type="text" name="review" class="form-control" id="review" placeholder="Enter anonymous review">

<button type="submit" class="menu">Submit</button>
</form>
</div>
</body>
</html>

有趣的是,存储在 $personName$value 中的所有内容都被正确回显。但是当我尝试将存储在变量中的数据插入数据库时​​出现问题。这似乎很恶心的话题。前一天我试图解决它但失败了。任何帮助将不胜感激。
另外,我暂时没有添加prepared statements功能,但是一旦这个问题解决了,我会添加同样的功能来防止mysql注入(inject)攻击。
[P.S.: 我仍然是 PHP 的初学者,所以我的错误很可能是愚蠢的。如果是这样请原谅。 ]
连接.php:

 <?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "";
}

$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "";
}
?>

最佳答案

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "invoice";
$personName = "Bhaskar";
if(isset($_POST['submit'])){
$value = $_POST['review'];
echo "<p>".$personName;
echo "<p>".$value;


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql ="INSERT INTO tbl_review (name, review) VALUES ('$personName', '$value')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button[type=submit] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}

input[type=submit]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<form class="" method="post" action="" >
<label for="form-element"></label>
<input type="text" name="review" class="form-control" id="review" placeholder="Enter anonymous review">

<button type="submit" name="submit" class="menu">Submit</button>
</form>
</div>
</body>
</html>

关于php - 有趣的是,数据没有插入到 mysql 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48927019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com