简体   繁体   English

使用PHP和AJAX插入数据时出现问题

[英]Issue when inserting data using PHP and AJAX

I'm running an AJAX call that passes some variables to a PHP script which is supposed to INSERT into a table. 我正在运行一个AJAX调用,该调用将一些变量传递给应该插入表中的PHP脚本。 But it doesn't work ? 但这行不通吗?

Code : 代码:

    <?php
$cn = mysql_connect('localhost','root','');

if ($cn) {
  mysql_select_db('test',$cn);
}

if (isset($_POST['saverecord'])) {
  $name = mysql_real_escape_string($_POST['name']);
  $gender = mysql_real_escape_string($_POST['gender']);
  $phone = mysql_real_escape_string($_POST['phone']);

  mysql_query('INSERT INTO `ajax_php` (name,gender,phone) VALUES ($name,$gender,$phone)');
  echo "IT WORK";
  exit;
}

 ?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><?php echo "Ajax + PHP Tutorial"; ?></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  </head>
  <body>
    <table id="data">
      <tr>
        <td>
          Name :
          <input type="text" name="name" id="name">
        </td>
        <td>
          Gender :
          <select id="gender" name="gender">
            <option value="0">Male</option>
            <option value="1">Female</option>
          </select>
        </td>
        <td>
          Name :  <input type="text" name="phone" id="phone">
        </td>
      </tr>
    <input type="button" value="save" id="save">
    </table>
  </body>
</html>

<script type="text/javascript">
  $(function(){
    // Save Data
    $('#save').click(function(){
      //get values
      var name = $('#name').val();
      var gender = $('#gender').val();
      var phone = $('#phone').val();

      alert(name);
      $.ajax({
        url: 'index.php',
        type: 'POST',
        async: false,
        data: {
          'saverecord' : 1,
          'name': name,
          'gender': gender,
          'phone': phone
        },
        success: function(re) {
          if (re==0) {
            alert('success');
            $('#name').val('');
            $('#gender').val('');
            $('#phone').val('');
          }
        }
      });
    });
    // End
  });
</script>

Also it doesn't display IT WORK , but Success is alerted. 此外,它也不显示IT WORK ,但会警告Success I think that the problem is located in the given javascript code ... 我认为问题出在给定的javascript代码中...

Problem in PHP Code PHP代码中的问题

You need to use double quotes in the query SQL so that the php variables are parsed properly and again you need single quotes around values. 您需要在查询SQL中使用双引号,以便正确解析php变量,并且再次需要在值周围使用单引号。

mysql_query("INSERT INTO `ajax_php` (name,gender,phone) VALUES ('$name','$gender','$phone')");

Note: mysql_* functions are deprecated, so consider moving to mysqli_* functions ASAP. 注意:不建议使用mysql_ *函数,因此请考虑尽快迁移到mysqli_ *函数。

Problem in JS Code JS代码中的问题

Your success function alerts success , doesn't alert the response from the server. 您的成功功能将警告success ,而不警告服务器的响应。

   success: function(re) {
      alert(re); // it will alert the response from the server.
    }
mysql_query('INSERT INTO ajax_php (name, ...) VALUES ($name, ...)');

You need to use double quotes. 您需要使用双引号。 Otherwise everybody is called $name in your data base because your variables are not evaluated. 否则在您的数据库中,每个人都称为$ name,因为您的变量没有被求值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM