SQL - 将表单插入数据库 [英] SQL - Insert form into a database

查看:93
本文介绍了SQL - 将表单插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php脚本,用于将表单输入插入一个名为XXXX_comments的数据库中,名为Comment,其名称和注释列作为列。当用户点击保存按钮时,表单应插入到数据库中。我使用connect.php连接到数据库:

I have a php script to insert form input into a database called XXXX_comments into the Table called Comments that has Name and Comment as columns.When a user hits Save button the form should be inserted into the DB. I connect to the database using 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文件中,我有以下形式:

in my index.php file i have the following form:

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

连接成功但数据未被插入。
<对于任何一个intrested我已经解决了这个问题,它似乎像提交按钮没有响应所以我添加了一个javascript onclick函数强制提交表单。

The connection is successful but the data is not being inserted.

推荐答案

/ p>

For any one intrested I have solved the issue, It seemed like the submit button wasn't responding So i added a javascript onclick function to force submit the form.

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

感谢您的所有答案。

这篇关于SQL - 将表单插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆