SQL - 将表单插入数据库

时间:2017-06-30 14:22:23

标签: php html mysql

我有一个php脚本,将表单输入插入到名为XXXX_comments的数据库中,名为Comments,其中Name和Comment为columns。当用户点击Save按钮时,表单应该插入到DB中。我使用connect.php连接到数据库:

<?php
$servername = "localhost";
$username = "XXXXX";
$password = "XXXXX";
$Dbconnect = "XXX_comments";

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

在我的index.php文件中,我有以下形式:

<?php
    include ('connect.php');
?>

<?php               
        if(isset($_POST['Save'])){
        $sql = $conn->prepare("INSERT INTO Comments (Name, Comment) VALUES (?, ?)");
        $stmt->bind_param("ss", $firstname, $comment);
        $firstname = $_POST['firstname'];
        $comment = $POST['comment'];
        $stmt->execute();  
        Echo "Succefully inserted to table";
 }
 ?>
<div class="container">
  <div class="row" id="post-review-box" style="display:none;">
    <div class="col-md-12">
      <form id="form" accept-charset="UTF-8" action="index.php" method="post">
        <input type="text" class="form-control animated" id="firstname" type="hidden" placeholder="Enter your Name">
        <br>
        <input id="ratings-hidden" name="rating" type="hidden">
        <textarea class="form-control animated" cols="50" id="comment" placeholder="Enter your review here..." rows="5"></textarea>
        <br>
        <div class="text-right">
          <div class="stars starrr" data-rating="0"></div>
          <a class="btn btn-danger btn-sm" href="#" id="close-review-box" style="display:none; margin-right: 10px;">
            <span class="glyphicon glyphicon-remove"></span>Cancel</a>
          <button class="btn btn-success btn-lg" type="submit" name="Save">Save</button>
        </div>
      </form>

    </div>
  </div>

</div>
<div class="display"></div>
</div>
</div>
</div>

连接成功但未插入数据。

4 个答案:

答案 0 :(得分:1)

您需要先分配值,然后才能使用它们,如下所示:

$firstname = $_POST['firstname'];
$comment = $_POST['comment'];
$sql = $conn->prepare("INSERT INTO Comments (Name, Comment) VALUES (?, ?)");
$stmt->bind_param("sss", $firstname, $comment);

答案 1 :(得分:1)

您应该在表单中为您的输入和textarea命名为

<input type="text" name="firstname" class="form-control animated" id="firstname" type="hidden" placeholder="Enter your Name">
<input id="ratings-hidden" name="rating" type="hidden">
<textarea class="form-control animated" cols="50" name="comment" id="comment" placeholder="Enter your review here..." rows="5"></textarea

并且在将值绑定到查询后分配值,您应该在将$firstname$comment绑定到查询之前为其分配值

if(isset($_POST['Save'])){
    $firstname = $_POST['firstname'];
    $comment = $_POST['comment'];
    $sql = $conn->prepare("INSERT INTO Comments (Name, Comment) VALUES (?, ?)");
    $stmt->bind_param("ss", $firstname, $comment);    
    $stmt->execute();  
    echo "Succefully inserted to table";
}

编辑:代码使用$ POST而不是$ _POST。

答案 2 :(得分:1)

几点评论

首先,在connect.php文件中混合OOP和Procedural样式,您可以使用以下代码获得相同的结果:

 $conn = new mysqli($servername, $username, $password, $Dbconnect);

接下来,在index.php中,您已经创建了一个预准备语句,但是您已将其分配给$ sql变量,后来您尝试使用undefined $ stmt变量,所以为了解决这个问题,只需更改前两行

 $stmt = $conn->prepare("INSERT INTO Comments (Name, Comment) VALUES (?, ?)"); 
 $stmt->bind_param("ss", $firstname, $comment);

希望它能解决你的小问题!

答案 3 :(得分:0)

对于任何有兴趣的人我已经解决了这个问题,似乎提交按钮没有响应所以我添加了一个javascript onclick函数来强制提交表单。

<input class="btn btn-default" onclick="myFunction()" value="Submit">
<script>
function myFunction() {
    document.getElementById("form").submit();
}
</script>   

感谢所有答案。