从引导模式窗体更新数据库

时间:2017-03-13 12:08:33

标签: javascript php jquery ajax twitter-bootstrap

参考我之前的问题> Displaying php echo $row data into bootstrap modal form

当我点击带有$(".saveContact").attr('data-id', result[0]['id']);

的行编辑按钮时,我设置了“保存”按钮数据ID

在#editForm提交的jQuery中,我已经通过var uid = $(".saveContact").attr('data-id');检索了id并将其传递给 updateContact.php ,但是当我点击“保存”按钮时,没有任何反应莫代尔只是保持开放。如果我没有正确传递id或者更新数据库时我的UPDATE语句不正确,我可以知道吗?

tables.php模式

<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form class="form-horizontal" method="POST" id="editForm" role="form">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="myModalLabel">Edit Contact</h4>
  </div>
  <div class="modal-body">
                <div class="form-group animated fadeIn">
                    <label for="nameInput" class="col-sm-2 control-label">Name</label>
                    <div class="col-sm-10">
                    <input type="name" name="name" class="form-control" id="nameInput" placeholder="Name" required>
                    </div>
                </div>

                <div class="form-group animated fadeIn">
                    <label for="companyInput" class="col-sm-2 control-label">Company</label>
                    <div class="col-sm-10">
                    <input type="company" name="company" class="form-control" id="companyInput" placeholder="Company" required>
                    </div>
                </div>

                <div class="form-group animated fadeIn">
                    <label for="posInput" class="col-sm-2 control-label">Position</label>
                    <div class="col-sm-10">
                    <input type="position" name="position" class="form-control" id="posInput" placeholder="Position/Job Title">
                    </div>
                </div>

                <div class="form-group animated fadeIn">
                    <label for="contactInput" class="col-sm-2 control-label">Contact Number</label>
                    <div class="col-sm-10">
                    <input type="number" name="contact" class="form-control" id="contactInput" placeholder="Office/Mobile Number" data-error="Please enter a valid mobile number" required>
                    </div>
                </div>

                <div class="form-group animated fadeIn">
                    <label for="emailInput" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-10">
                    <input type="email" name="email" class="form-control" id="emailInput" placeholder="Email Address">
                    </div>
                </div>

                <div class="form-group animated fadeIn">
                    <label for="genderInput" class="col-sm-2 control-label">Gender</label>
                    <div class="col-sm-10">
                    <input type="gender" name="gender" class="form-control" id="genderInput" placeholder="Male/Female">
                    </div>
                </div>
                    <div class="form-group">
                <input type="hidden" name="id" class="form-control" id="idInput" placeholder="ID">
                </div>
                </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button id="editContact" type="submit" class="saveContact btn btn-primary" data-id="">Save</button>
  </div>
</div>
</form>
</div>
</div>

enter image description here

jQuery提交表单

$("#editForm").submit(function(e){
e.preventDefault();
var uid = $("#idInput").val();
var name = $("#nameInput").val();
var company = $("#companyInput").val();
var position = $("#posInput").val();
var contact = $("#contactInput").val();
var email = $("#emailInput").val();
var gender = $("#genderInput").val();

var dataForm = 'name=' + name + '&company=' + company + '&position=' + position + '&contact=' + contact + '&email=' + email + '&gender=' + gender + '&id=' + uid;

$.ajax({
    type: 'POST',
    url: '../admin/dataprocess/updateContact.php',
    data: dataForm,
    success: function(html){
        if(html == "success"){
            $('#contactsTable').dataTable().reload();
            $('#editModal').modal('toggle');
        }
    }
    });

});

updateContact.php

<?php
include("dbconnect.php");

$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$id = $_POST['id'];

$stmt = $link->prepare("UPDATE businesscontact SET name = ?, company = ?, position = ?, phone = ?, email = ?, gender = ? WHERE id = $id");
$stmt-> bind_param("ssssss", $name, $company, $position, $contact, $email, $gender);
if($stmt->execute()){
    echo "success";
}else{
    echo(mysqli_error($link);
}


mysqli_close($link);
?>

1 个答案:

答案 0 :(得分:2)

正如我在评论中所述;

echo(mysqli_error($link); // <- theoretically missing a )

导致错误,因为缺少括号。

  

@ Fred-ii-感谢您指出这一点,确实只是因为这导致更新无效。 - Kayden&#34;

你甚至不需要&#34; echo&#34;无论如何。

只需使用以下内容即可。它会自行触发一个“回声”错误:

mysqli_error($link);

因为&#34; echo&#34;是一种语言结构,不需要括号。

错误报告会帮助您。