Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ajax和php在MYSQL中插入数据_Php_Jquery_Mysql - Fatal编程技术网

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

使用ajax和php在MYSQL中插入数据,php,jquery,mysql,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<

这是我的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);
       ?>

使用以下命令传递表单数据

data:$('#register-form').serialize(),
试试这个代码

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

 <?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);
?>

尝试在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: $(this).closest('#register-form').serialize(),

您得到的错误是什么?没有显示错误,但数据库id正在增加,但数据没有显示您没有将数据从ajax代码传递到服务器端PHP文件。您需要传递表单数据,如:data:$register-form.serialize;此处不显示您的ip或密码etcstring mysqli\u real\u escape\u string mysqli$link,string$escapestr
 data: $(this).closest('#register-form').serialize(),