简体   繁体   English

数据没有有趣地插入到mysql表中

[英]Data not inserting into mysql table interestingly

I was making a review form in which a review will be taken from a textbox and inserted into the database.我正在制作一个评论表单,其中将从文本框中获取评论并插入到数据库中。 But the problem is that when I try running the code it gives the following error:但问题是,当我尝试运行代码时,它会出现以下错误:

Warning: mysqli::query(): Couldn't fetch mysqli in C:\\wamp64\\path\\to\\file on line 12警告:mysqli::query(): 无法在第 12 行的 C:\\wamp64\\path\\to\\file 中获取 mysqli

The code that I wrote for doing the same is given below:我为做同样的事情而编写的代码如下:

     <?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>

It is interesting to note that everything that is stored in $personName and $value are being echoed correctly.有趣的是,存储在$personName$value都得到了正确的回显。 But the problem appears when I try inserting the data stored in the variable into the database.但是当我尝试将存储在变量中的数据插入数据库时​​出现问题。 This seems pretty disgusting topic.这看起来很恶心的话题。 I tried to solve it the whole previous day but failed.前一天我试图解决它,但失败了。 Any help will be highly appreciated.任何帮助将不胜感激。
Also, I haven't added prepared statements feature for the time being but I will add the same to prevent it from mysql injection attacks as soon as this problem is solved.另外,我暂时没有添加准备好的语句功能,但我会添加相同的功能,以防止此问题解决后立即受到 mysql 注入攻击。
[ PS: I am still a beginner in PHP, So there are high chances that my mistakes are silly. [ PS:我仍然是 PHP 的初学者,所以我的错误很可能是愚蠢的。 Pardon if it is so.如果是这样,请原谅。 ] ]
connect.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM