Jquery ajax添加到mysql数据库

I want to save data from input fields to mysql database so first I create a modal window and input fields:

<!-- Button trigger modal -->
<button class="btn btn-success" data-toggle="modal" data-target="#myModal">
Add new</button>
<div id="output"></div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content">
         <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Add new row</h4>
         </div>
         <div class="modal-body">
            ......
            <div class="input-group">
               <span class="input-group-addon">Ime</span>
               <input type="text" id="Ime" class="form-control" placeholder="Upisi ime">
            </div>
            </br>
            <div class="input-group">
               <span class="input-group-addon">Pol</span>
               <input type="text" id="pol" class="form-control" placeholder="Pol (male/female)">
            </div>
            </br>
            <div class="input-group">
               <span class="input-group-addon">Godine</span>
               <input type="text" id="godine" class="form-control" placeholder="Godine starosti">
            </div>
            </br>
            <div class="input-group">
               <span class="input-group-addon">Broj pojedenih krofni</span>
               <input type="text" id="krofne" class="form-control" placeholder="Pojedene krofne">
            </div>
            </br>
         </div>
         <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button id="newData" class="btn btn-primary">Add new data</button>
         </div>
      </div>
      <!-- /.modal-content -->
   </div>
   <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

Now I write jQuery AJAX code to add data to database:

 <script>
  //add data to database using jquery ajax
$("#newData").click(function() {
    //in here we can do the ajax after validating the field isn't empty.
    if($("#ime").val()!="") {
        $.ajax({
            url: "add.php",
            type: "POST",
            async: true, 
            data: { Name:$("#ime").val(), Gender:$("#pol").val(), Age:$("#godine").val(), Donuts_eaten:$("#krofne").val()}, //your form data to post goes here as a json object
            dataType: "html",

            success: function(data) {
                $('#output').html(data); 
                drawVisualization();   
            },  
        });
    } else {
        //notify the user they need to enter data
    }
});
</script>

and finally I create a php file (add.php)

<?php

$con = mysql_connect('localhost', 'gmaestro_agro', 'pass') or die('Error connecting to server');

mysql_select_db('gmaestro_agro', $con); 

    mysql_select_db('gmaestro_agro', $con);
    $query = "INSERT INTO `stat` (`Name`, `Gender`, `Age`, `Donuts eaten`) VALUES (";
    $query .= mysql_real_escape_string($_POST['Name']) . ", ";
    $query .= mysql_real_escape_string($_POST['Gender']) . ", ";
    $query .= mysql_real_escape_string($_POST['Age']) . ", ";
    $query .= mysql_real_escape_string($_POST['Donuts_eaten']);
    $query .= ")";
    $result = mysql_query($query);
    if($result != false) {
        echo "success!";
    } else {
        echo "an error occured saving your data!";
    }

?>

Now, when I try to add data I just get this error: an error occurred saving your data!.

What is the problem here exactly? I try to find the error whole day...

dongzhang2150
dongzhang2150 现在一切都很好,因为我的ID名称错了。感谢帮助
6 年多之前 回复
dousuize3082
dousuize3082 什么是表结构,表结构或mysql连接一定有问题,重置一切都很好
6 年多之前 回复
dongzj2015
dongzj2015 您是否检查过服务器日志以查看是否有明确的错误消息?
6 年多之前 回复
dtr32221
dtr32221 演示:agrotime.roadvoyage.com/index1.html
6 年多之前 回复

2个回答



您没有引用字符串:</ p>

  $ query。= mysql_real_escape_string($  _POST ['姓名'])。  “,”; 
</ code> </ pre>

应为:</ p>

  $ query。=“'”。  mysql_real_escape_string($ _ POST ['Name'])。  “',”; 
</ code> </ pre>

(对于所有字符串值)</ p>

顺便说一句,它可能会让您的生活更轻松 如果您切换到PDO或mysqli和准备好的语句。 然后你不必转义并引用你的变量,并且不推荐使用 mysql _ * </ code>函数。</ p>
</ div>

展开原文

原文

You are not quoting your strings:

$query .= mysql_real_escape_string($_POST['Name']) . ", ";

should be:

$query .= "'" . mysql_real_escape_string($_POST['Name']) . "', ";

(for all string values)

By the way, it would probably make your life easier if you switched to PDO or mysqli and prepared statements. Then you would not have to escape and quote your variables and the mysql_* functions are deprecated anyway.

dongxiang3648
dongxiang3648 是的,非常感谢
6 年多之前 回复
dongxiaoshe0737
dongxiaoshe0737 我再次收到错误:保存数据时出错! ,,,例如:agrotime.roadvoyage.com/index1.html,尝试添加数据点击按钮添加新的
6 年多之前 回复



  $ query =“INSERT INTOstatNameGenderAgeDonuts eaten)VALUES(“; 
$ query。=”'“。mysql_real_escape_string($ _ POST ['Name'])。”',“;
$ query。=”'“。mysql_real_escape_string($ _ POST ['Gender' ])。“',”;
$ query。=“'”。mysql_real_escape_string($ _ POST ['Age'])。“',”;
$ query。=“'”。mysql_real_escape_string($ _ POST [' Donuts_eaten']);
$ query。=“')”;
</ code> </ pre>

将所有值放在单引号中。</ p>
</ div >

展开原文

原文

      $query = "INSERT INTO `stat` (`Name`, `Gender`, `Age`, `Donuts eaten`) VALUES (";
      $query .= "'".mysql_real_escape_string($_POST['Name']) . "', ";
      $query .= "'".mysql_real_escape_string($_POST['Gender']) . "', ";
      $query .= "'".mysql_real_escape_string($_POST['Age']) . "', ";
      $query .= "'".mysql_real_escape_string($_POST['Donuts_eaten']);
      $query .= "')";

Put all the values in single quotes.

dongyan0629
dongyan0629 谢谢,它真的很有用
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐