插入数据ajax时出错

时间:2017-09-06 08:34:38

标签: php ajax

你好我试图用ajax创建一个帐户。一切正常,买的问题是年龄和contactnumber不工作注册每个数据都可以插入除了年龄和联系号码,所以我有一个问题, 我的问题是年龄和联系电话号码无法插入数据库 上 的index.php

  <div class="col-md-4">
          <label for="createage">Age</label>
          <input id="createage" name="createage" class="form-control" type="text" >
      </div>


        <div class="col-md-6">
            <label for="createcontactnumber">Contact Number</label>
            <input id="createcontactnumber"
              name="createcontactnumber"
              class="form-control"
              oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
              type = "number" maxlength = "11"/>
        </div>

表示register.js

  $(document).ready(function(e){
$('#register').click(function(){

  var Bdate = document.getElementById('createbday').value;
  var Bday = +new Date(Bdate);
  Q4A = ~~ ((Date.now() - Bday) / (31557600000));
  var theBday = document.getElementById('createage');
  theBday.innerHTML = Q4A.val;


   var createusername = $('#createusername').val();
   var createpassword = $('#createpassword').val();
   var creategivenname = $('#creategivenname').val();
   var createmiddlename = $('#createmiddlename').val();
   var createlastname = $('#createlastname').val();
   var createbday = $('#createbday').val();
   var age = Q4A;
   var creategender = $('#creategender').val();
   var contactnumber = $('#createcontactnumber').val();
   var createaddress = $('#createaddress').val();
   var createcity = $('#createcity').val();
   $.ajax({
     type   : 'POST',
     data   :{createusername:createusername,
              createpassword:createpassword,
              creategivenname:creategivenname,
              createmiddlename:createmiddlename,
              createlastname:createlastname,
              createbday:createbday,
              age:age,
              creategender:creategender,
              contactnumber:contactnumber,
              createaddress:createaddress,
              createcity:createcity},
     url     :"insert.php",
    success : function(result){
      if(result)
      {
       $('#error').html("<span>Success Man</span>");
       $('#createusername').val('');
       $('#createpassword').val('');
       $('#creategivenname').val('');
       $('#createmiddlename').val('');
       $('#createlastname').val('');
       $('#createbday').val('');
       $('#createage').val('');
       $('#creategender').val('');
       $('#createcontactnumber').val('');
       $('#createaddress').val('');
       $('#createcity').val('');
      }
    }
   })
});

});

register.php

<?php

$connect = mysqli_connect("localhost", "root", "", "thaidatabase");

   $username = $_POST['createusername'];
   $password = $_POST['createpassword'];
   $givenname = $_POST['creategivenname'];
   $middlename = $_POST['createmiddlename'];
   $lastname = $_POST['createlastname'];
   $bday = $_POST['createbday'];
   $age = $_POST['createage'];
   $gender = $_POST['creategender'];
   $contactnumber = $_POST['createcontactnumber'];
   $address = $_POST['createaddress'];
   $city = $_POST['createcity'];

 mysqli_query($connect,"insert into account (username,password,givenname,middlename,lastname,bday,age,gender,contactnumber,address,city)
          values
          ('$username','$password','$givenname','$middlename','$lastname','$bday','$age','$gender','$contactnumber','$address','$city City')");


 mysqli_close($connect);


?>

1 个答案:

答案 0 :(得分:0)

您在POST请求(JavaScript)中将年龄作为年龄属性传递,并尝试从php中的_POST数组中读取 createage 。 对于contactnumber也一样。

必须使用相同的名称接收发送的属性。解决它,它必须解决。

<?php

$connect = mysqli_connect("localhost", "root", "", "thaidatabase");

   $username = $_POST['createusername'];
   $password = $_POST['createpassword'];
   $givenname = $_POST['creategivenname'];
   $middlename = $_POST['createmiddlename'];
   $lastname = $_POST['createlastname'];
   $bday = $_POST['createbday'];

   //Edit This
   $age = $_POST['age'];
   $gender = $_POST['creategender'];

   //Edit This
   $contactnumber = $_POST['contactnumber'];

   $address = $_POST['createaddress'];
   $city = $_POST['createcity'];

 mysqli_query($connect,"insert into account (username,password,givenname,middlename,lastname,bday,age,gender,contactnumber,address,city)
          values
          ('$username','$password','$givenname','$middlename','$lastname','$bday','$age','$gender','$contactnumber','$address','$city City')");


 mysqli_close($connect);


?>