使用ajax和php在MYSQL中插入数据

时间:2014-02-17 12:33:48

标签: php jquery mysql

这是我的html / ajax / jquery和php文件。我必须在数据库中插入数据 但是数据没有插入我的数据库

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js</script>
<script>
function add() {
    $("#register-form").validate({
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            },
            Budget: {
                required: true,
            },
            phone: "required",
            budget: "required",
        },
        messages: {
            name: "Please enter your Name",
            email: "Please enter a valid Email address",
            phone: "Please enter a valid Phone Number",
            Budget: "Please Select a Budget",
        },
        submitHandler: function (form) {
            //alert("success")
            $.ajax({
                url: "insert2.php",
                type: "POST",
                success: function (response) {
                    alert("Data Save: " + response)
                }
            });
        }
    });
}
</script>
</head>
<body>
    <form action="" method="post" id="register-form" >
        <div class="label">Name</div><input type="text" id="name" name="name" /><br />
        <div class="label">Email</div><input type="text" id="email" name="email" /><br />
        <div class="label">Phone Number</div><input type="text" id="phone" name="phone" /><br />
        <div class="label">budget</div>
        <select id="Budget" name="Budget">
            <option value="">select</option>
            <option value="1">0-100</option> <!-- first option contains value="" -->
            <option value="2">100-200</option> 
            <option value="3">200-300</option> 
        </select>
        <br />
        <div style="margin-left:140px;">
        <input type="submit"  onclick="add()" name="submit" /></div>
    </form>
</body>

这是我的html / ajax / jquery和php文件。我必须在数据库中插入数据 但是数据没有插入我的数据库中。

       <?php
     $con=mysqli_connect("xxxxxxxx","xxx","xx","xx");
        if (mysqli_connect_errno())
        {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
    $sql="INSERT INTO form (name, email, phone)
 VALUES
  ('$_POST[name]','$_POST[email]','$_POST[phone]')";

   if (!mysqli_query($con,$sql))
     {
      die('Error: ' . mysqli_error($con));
       }
      echo "1 record added";

       mysqli_close($con);
       ?>

3 个答案:

答案 0 :(得分:2)

使用此

传递表单数据
data:$('#register-form').serialize(),

试试这段代码

$.ajax({
    url: "insert2.php",
    type: "POST",
    data: $('#register-form').serialize(),
    success: function (response) {
        alert("Data Save: " + response)
    }
});

答案 1 :(得分:0)

尝试在ajax中添加序列化数据:

 submitHandler: function(form) {
        //alert("success")
        $.ajax({
            url:"insert2.php",
            type:"POST",
            data: $('#register-form').serialize(), // <---here
            success: function(response){
            alert( "Data Save: " + response)
            }
        });        
    }

你可以加上这个:

 data: $('#register-form').serialize(),

或者这个:

 data: $(this).closest('#register-form').serialize(),

答案 2 :(得分:-2)

试试这个

 <?php
$con=mysqli_connect("xxxx","xxx","xxx","xx");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = mysqli_real_escape_string($con, $_POST['name']); // to restrict from mysql injections
$email = mysqli_real_escape_string($con, $_POST['email']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$sql="INSERT INTO form (name, email, phone) VALUES('$name','$email','$phone')";

if (!mysqli_query($con,$sql))
{
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>