提问者:小点点

使用ajax用php更新mysql表


在这里,我创建了一个表http://jsbin.com/ojanaji/13/edit和demo:http://jsbin.com/ojanaji/13

因此,当用户单击表上的某些行时,可以自动将表中的值填充到模式窗口中。当点击按钮“编辑行”时,用户打开模式窗口。现在我需要知道如何用列更新mysql表:姓名、性别、年龄、吃过的甜甜圈。

我创建js Ajax:

$("#edit").click(function() {
    //in here we can do the ajax after validating the field isn't empty.
    if($("#name").val()!="") {
        $.ajax({
            url: "update.php",
            type: "POST",
            async: true, 
            data: { Name:$("#name").val(), Gender:$("#gender").val(), Age:$("#age").val(), Donuts_eaten:$("#donuts_eaten").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
    }
});

HTML模式窗口和按钮:

<!-- Button trigger modal -->
<button id="edit" class="btn btn-success disabled" type="button" data-toggle="modal" data-target="#myModal">
Edit selected row</button>

<!-- 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">Name</span>
  <input type="text" value="" id="name" class="form-control" placeholder="Type name">
        </div></br>
        <div class="input-group">
  <span class="input-group-addon">Gender</span>
  <input type="text" id="gender" class="form-control" placeholder="Gender?">
</div></br>
        <div class="input-group">
  <span class="input-group-addon">Age</span>
  <input type="text" id="age" class="form-control" placeholder="Number of age">
</div></br>
        <div class="input-group">
  <span class="input-group-addon">Donuts eaten</span>
  <input type="text" id="donuts_eaten" class="form-control" placeholder="Number of donuts eaten">
</div></br>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

那么我现在如何用PHP更新MySql数据库:

所以file update.php how必须是这样的:

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

mysql_select_db('gmaestro_agro', $con); 

//HOW I CAN UPDATE MYSQL DATABASE, WHAT I NEED TO ADD HERE?

?>

共1个答案

匿名用户

表中应该有一列是自动递增列,比如“id”,或者下面的示例使用“index_id”。这应该在创建表单时使用,并与$_POST数组一起发送,以引用要更新的行。这是一个简单的例子,你可以用它来开始。

 $_POST = stripslashes_deep($_POST); # you will want to better filtering for security.
    if(isset($_POST['Name']) && $_POST('Name') !=''){

    $query = "UPDATE stat
              SET Name   ='". $_POST['Name'] . "',
                  Gender ='". $_POST['Gender'] . "',
                  Age    ='". $_POST['Age'] . "',
                  Donuts_eaten  ='" .$_POST['Donuts_eaten'] . "'
             WHERE
                 index_id = '". $_POST['index_id'] . "'";

    $result = mysql_query($query) or die(mysql_error()); 
    exit(json_encode($_POST)); 
    }


    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
            array_map('stripslashes_deep', $value) :
            stripslashes($value);

        return $value;
    }

对于您的MYSQL表,您可以在您的MYSQL phpMyAdmin中运行以下内容:

ALTER TABLE  `stats` ADD  `index_id` INT( 3 ) NOT NULL AUTO_INCREMENT FIRST ,
ADD PRIMARY KEY (  `index_id` )