dongtuo3370 2015-02-03 02:36
浏览 57
已采纳

Ajax请求不向php页面发送数据,但登录控制台正常

I cannot figure out why the data is not getting inserted to my mysql database when it is being logged to the console properly.

My form as follows:

<form id="insertForm" type="post">
name: <input type="text" id="name" name="name">
activity: <input type="text" id="activity" name="activity">
level: <input type="number" id="level" name="level">
<input type="button" name="add" id="add" value="add">
</form>

My ajax call:

<script type="text/javascript">
$(document).ready(function() {
 $("#add").click(function(e){
var Data = $("#insertForm").serializeArray();
 console.log(Data);
 $.ajax({
 url : "insert.php",
 type: "POST",
 data : Data,
 success:function(data, textStatus, jqXHR)
 {
$('#msg').html(data);
 }
 });
 $("#msg").slideDown("slow");
 e.preventDefault();
});
});
</script>

My PHP insert:

<?php
if (!empty($_POST))
{
$dbhost = 'localhost';
$dbuser = 'lot_root';
$dbpass = '';
$con = mysql_connect($dbhost, $dbuser, $dbpass);

$name = mysql_real_escape_string($_POST['name']);
$activity = mysql_real_escape_string($_POST['activity']);
$level = $_POST['level'];

$sql = "INSERT INTO lottt ".
       "(name, activity, level) ".
       "VALUES('$name','$activity', '$level')";
mysql_select_db('lot');
mysql_close($con);
}
?>

When i insert the data via the input fields it gets logged to the js console fine like so [Object, Object, Object]0: Objectname: "character_name"value: "aSD"__proto__: Object1: Object2: Objectlength: 3__proto__: Array[0] but nothing is inserted into the mysql database after a quick check.

  • 写回答

1条回答 默认 最新

  • dongyue9864 2015-02-03 02:45
    关注

    You need to run the query to save it. I'll answer it to the mysql_* library, even though it is deprecated.

    This is how the flow is supposed to go:

    $con = mysql_connect(....) or die();
    mysql_select_db('database');
    
    $name = mysql_real_escape_string($_POST['name']);
    $activity = mysql_real_escape_string($_POST['activity']);
    $level = $_POST['level'];
    $sql = "INSERT INTO lottt ".
           "(name, activity, level) ".
           "VALUES('$name','$activity', '$level')";
    
    if(!mysql_query($sql)) {
        die('Error: ' . mysql_error());
    } else {
        echo 'saved';
    }
    

    You should stop using mysql_* functions. The library is deprecated.

    You should look into using PDO or MySQLi as they are more modern libraries and while you might have to overcome a hurdle to learn it, being competent in those libraries will do you the world of good!

    Resources:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。