无法使用PDO和ajax插入数据

时间:2016-10-19 20:59:25

标签: html mysql ajax twitter-bootstrap pdo

模态窗体运行良好,但在尝试插入数据时,代码无法正常工作,也没有显示任何错误。我在数据库中使用ajax进行数据插入操作。请帮我解决这个问题。

的index.php

    <!DOCTYPE>
<html>
    <head>
        <title>Student Information</title>
        <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css"/>
        <link rel="stylesheet" type="text/css" href="assets/css/style.css"/>
    </head>
    <body>
        <div class="container">
            <p></p>
            <p></p>
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary " data-toggle="modal" data-target="#myModal">
              Add Record
            </button>

            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog" role="document">
                <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">Add New Record</h4>
                  </div>
                  <form>
                  <div class="modal-body">
                    <div class="form-group">
                        <label for="name">Name</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="Full Name">
                    </div>
                    <div class="form-group">
                        <label for="email">Email</label>
                        <input type="email" class="form-control" id="email" name="email" placeholder="Email Address">
                    </div>
                    <div class="form-group">
                        <label for="phone">Phone</label>
                        <input type="text" class="form-control" id="phone" name="phone" placeholder="Phone Number">
                    </div>
                    <div class="form-group">
                        <label for="address">Address</label>
                        <input type="text" class="form-control" id="address" name="address" placeholder="Address">
                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary" onClick="saveData()">Save changes</button>
                  </div>
                </form>
                </div>
              </div>
            </div>
        </div>
        <!--------->    
        <script src="assets/js/jquery-3.1.1.js"></script>
        <script src="assets/js/bootstrap.min.js"></script>
        <script>
            function saveData(){
                var name = $('#name').val();
                var email = $('#email').val();
                var phone = $('#phone').val();
                var address = $('#address').val();
                $.ajax({
                    type: "POST",
                    url: "server.php?p=add",
                    data: "nm="+name+"&em="+email+"&ph="+phone+"$ad="+address,
                    success:function(msg){
                        alert('Success insert data');
                    }
                }); 
            }
        </script>
    </body>
</html>


    server.php
    ----------

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=db_student", $username, $password);

        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";
    }
    catch(PDOException $e)
    {
        echo "Connection failed: " . $e->getMessage();
    } 
    $page = isset($_GET['p'])?$_GET['p']:'';
   if($page=='add'){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $address = $_POST['address'];
        $stmt = $db->prepare("insert into student (name, email, phone, address) values(?,?,?,?)");
        $stmt->bindParam(1,$name);
        $stmt->bindParam(2,$email);
        $stmt->bindParam(3,$phone);
        $stmt->bindParam(4,$address);
    if($stmt->execute()){
        echo "Saved";
    }else{
        echo "Not Saved";
    }
  }else if($page=='edit'){

  }else if($page=='del'){

}
?>

1 个答案:

答案 0 :(得分:0)

$stmt = $db->prepare('your query')更改为$stmt = $conn->prepare('your query')