使用ajax用php mysqli更新模态形式的数据

时间:2018-07-29 17:12:52

标签: javascript php jquery ajax mysqli

我设法防止使用提交价值后关闭我的模式表格 无需刷新页面的ajax jQuery

但是现在我的问题是,当我填写模态表单并单击提交以更新数据库中的数据时,我的数据库没有更新。

我认为我的Ajax代码有问题。

希望你能帮助我。谢谢。

这是我的代码:

 <form class="needs-validation " id="contact-form" action="index13.php" method="post" novalidate >
</form>

这是我的index13.php代码:

<?php

 include_once ('connection.php');

if(isset($_POST['submit1']))
{

$username2 = $_POST['username2'];
$password1= $_POST['password1'];
$time=date("H:i:s");

$sql = "select * from visitor_att  where  uname = '$username2' and pass = '$password1'";
$result = mysqli_query($conn,$sql);
 $count = mysqli_num_rows($result);

if ($count == 0) {

echo "No Results";

    } else{
      while ($row = mysqli_fetch_array($result)) {
          $username2 = $row['uname'];
          $password1 = $row['pass'];
          $fname=$row['fname'];
          $lname=$row['lname']; 

         $InsertSql = "Update visitor_att set timeout = '$time' where  uname = '$username2' and pass = '$password1'";
        $res = mysqli_query($conn, $InsertSql); 

        }

         }



  }
?>

这是我的Ajax jQuery代码:

<script>    
$(function() {


 var frm = $("#contact-form");

    frm.submit(function (e) {

        e.preventDefault();

        $.ajax({
            type: frm.attr("post"),
            url: frm.attr("action"),
            data: frm.serialize(),
            success: function (data) {
                console.log('Submission was successful.');
                console.log(data);
            },
            error: function (data) {
                console.log('An error occurred.');
                console.log(data);
            },
           });
        });
    });
</script>

当我在控制台上运行它时,表明提交成功。 但没有数据插入数据库。

1 个答案:

答案 0 :(得分:0)

您似乎需要将id属性添加到表单中,因为您的ajax调用已绑定到该属性...

SELECT      yt.*,
            ca.extractedNumbers
FROM        dbo.YourTable yt
CROSS APPLY
            ( SELECT  STRING_AGG (d.character, '' ) WITHIN GROUP ( ORDER BY d.Num )
                     FROM     ( SELECT character = SUBSTRING(StringField, Nums.Num, 1),
                                       Nums.Num
                                      FROM  (VALUES (01),(02),(03),(04),(05),
                                                    (06),(07),(08),(09),(10),
                                                    (11),(12),(13),(14),(15),
                                                    (16),(17),(18),(19),(20),
                                                    (21),(22),(23),(24),(25),
                                                    (26),(27),(28),(29),(30)
                                       ) Nums(Num)
                                      WHERE   Nums.Num <= LEN(yt.StringField) ) d
                     WHERE    d.character LIKE '[0-9]'
            ) ca(extractedNumbers)