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

时间:2018-02-22 12:07:35

标签: php mysql

我正在撰写一份评审表,其中将从文本框中进行评审并插入数据库。但问题是,当我尝试运行代码时,会出现以下错误:

  

警告:mysqli :: query():无法在第12行的C:\ wamp64 \ path \ to \ file中获取mysqli

我为此做的代码如下:

     <?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中存储的所有内容都正确回显。但是当我尝试将存储在变量中的数据插入数据库时​​出现问题。这似乎是非常恶心的话题。我试图在整个前一天解决它但失败了。任何帮助将受到高度赞赏。
此外,我暂时还没有添加预备语句功能,但是我会添加相同的内容,以便在问题解决后立即防止mysql注入攻击。
[ P.S。:我仍然是PHP的初学者,所以很有可能我的错误很愚蠢。原谅如果是这样的话。 ]
connect.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 "";
            }
            ?>

1 个答案:

答案 0 :(得分:1)

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