PHP INSERT不起作用。我还使用了Jquery ajax,因此它不会刷新

时间:2015-12-13 09:36:30

标签: php jquery ajax

当我将它传递给我的php时,我的ajax帖子工作正常,只是回显了ajax发送给它的数据。我希望它现在插入到我的数据库,似乎我的PHP代码不起作用...

HTML

<form id="member" method="post" action="../php/connect.php">
                    <fieldset style="width:400; border-width:6px;" align="center"   >
                <legend><h3>Member Log</h3><br><br></legend>
                    <p id="get_member"></p>

                 <input class="fname" type='text' name='firstname' id='firstname' placeholder="First Name" required/>

                </br><input class="lname" type='text' name='lastname' id='lastname' placeholder="Last Name" required /></br>

                </br><label for="male">Male  </label><input type="radio" name="gender" id="male" required value="Male" />
                     <label for="female">Female  </label><input type="radio" name="gender" id="female" value="Female" /><br />
                     <input type="hidden" name="date" id="date" value="">
                     <input type="hidden" name="time" id="time" value="">

                <br><br><input class="submit" type="submit" name="submit">
            </fieldset>
</form>

jquery ajax post ..

<script type="text/javascript">

        $(document).ready(function(){



            var my_date_format = function(){
    var d = new Date();
    var month = ['Jan ', 'Feb ', 'Mar ', 'Apr ', 'May ', 'Jun ', 'Jul ', 'Aug ', 'Sep ', 'Oct ', 'Nov ', 'Dec '];
    var date = d.getDate() + " " + month[d.getMonth()] + ", " + d.getFullYear();
    var time = d.toLocaleTimeString().toLowerCase().replace(/([\d]+:[\d]+):[\d]+(\s\w+)/g, "$1$2");
    return (date);  
};

var my_time_format = function(){
    var d = new Date();
    var time = d.toLocaleTimeString().toLowerCase().replace(/([\d]+:[\d]+):[\d]+(\s\w+)/g, "$1$2");
    return (time);  
};

//console.log(my_date_format());
// output 6 Jul, 2014 11:28 am



         $('#member').submit(function(){

                $('#date').val(my_date_format);
                $('#time').val(my_time_format);


                  $.ajax({
                           data: $(this).serialize(),
                           type: $(this).attr('method'),
                           url: $(this).attr('action'),
                           success: function(data){
                                    $('#get_member').html(data);
                            }

                  });

                  $("#member")[0].reset();
                event.preventDefault();
         });

});

    </script>

php connect.php

<?php
error_reporting(0);



    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
    $gender = $_POST['gender'];
    $time = $_POST['time'];
    $date = $_POST['date'];

    $servername = "localhost";
    $username = "root";
    $password = "";
    $db = "rmsdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$sql = "INSERT INTO walkin_member (id,firstname, lastname, gender, time, datestamp)
VALUES ('',$fname', '$lname', '$gender', '$time', '$date')";

if ($conn->query($sql) === TRUE) {
    echo "Recent Log" . "</br>First Name: " . $fname . "</br>Last Name:   " . $lname . "</br>Gender: " . $gender . "<br />Time: " . $time . "<br />Date: " . $date;

   } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$connect->close();


?>

2 个答案:

答案 0 :(得分:1)

请检查$fname'此处'$fname'缺少引号。

sql 应该是

$sql = "INSERT INTO walkin_member (id,firstname, lastname, gender, time, datestamp)
VALUES ('','$fname', '$lname', '$gender', '$time', '$date')";

答案 1 :(得分:1)

如果 id 字段是自动完成的主键,则不会插入ID。

你的sql就像

$sql = "INSERT INTO `walkin_member` (firstname, lastname, gender, time, datestamp) 
VALUES ('".$fname."', '".$lname."', '."$gender."', '."$time."', '."$date."')";