weixin_33726943 2017-02-14 06:04 采纳率: 0%
浏览 23

在ajax中编辑多个值

I am trying to edit two columns using ajax and php.My code currently edits one values(name) in my table and saves it to my database.When i add the second variable (p) my ajax call it updates both columns p and y with the same value.How do i edit the third value and assign it a different value from y.I want the two different columns to have different values in my db(columns:name and capacity)

This code edits and updates two values:

 <script type="text/javascript">
jQuery(document).ready(function() {  
        $.fn.editable.defaults.mode = 'popup';
        $('.xedit').editable();     
        $(document).on('click','.editable-submit',function(){
            var x = $(this).closest('td').children('span').attr('id');
            var y = $('.input-sm').val();
            var z = $(this).closest('td').children('span');
            $.ajax({
                url: "process.php?id="+x+"&data="+y,
                type: 'GET',
                success: function(s){
                    if(s == 'status'){
                    $(z).html(y);}
                    if(s == 'error') {
                    alert('Error Processing your Request!');}
                },
                error: function(e){
                    alert('Error Processing your Request!!');
                }
            });
        });
});
</script>

And this is what i tried to edit three values:

     <script type="text/javascript">
jQuery(document).ready(function() {  
        $.fn.editable.defaults.mode = 'popup';
        $('.xedit').editable();     
        $(document).on('click','.editable-submit',function(){
            var x = $(this).closest('td').children('span').attr('id');
            var y = $('.input-sm').val();
            var p = $('.input-sm').val();
            var z = $(this).closest('td').children('span');
            $.ajax({
                url: "process.php?id="+x+"&data="+y+"&capacity="+y,
                type: 'GET',
                success: function(s){
                    if(s == 'status'){
                    $(z).html(y);
                    $(z).html(p);}
                    if(s == 'error') {
                    alert('Error Processing your Request!');}
                },
                error: function(e){
                    alert('Error Processing your Request!!');
                }
            });
        });
});
</script>

And heres my php file(process.php)

    <?php
include("connect.php");
if
    ($_GET['id'],$_GET['capacity'] and $_GET['data'])
{
    $id = $_GET['id'];
    $data = $_GET['data'];
    $capacity = $_GET['capacity'];
    if(mysqli_query($con,"update mytable set name='$data',capacity='$data' where id='$id'")){

        echo "success";
    }

else{
echo 'failed';
}

}
?>

And my table in index.php

 <tbody>
                    <?php
                    $query = mysqli_query($con,"select * from mytable");
                    $i=0;
                    while($fetch = mysqli_fetch_array($query))
                    {
                        if($i%2==0) $class = 'even'; else $class = 'odd';

                        echo'<tr class="'.$class.'">

                            <td><span class= "xedit external-event bg-brown" id="'.$fetch['id'].'">'.$fetch['name'].'</span></td>

                            <td><span class= "xedit external-event bg-brown" id="'.$fetch['id'].'">'.$fetch['capacity'].'</span></td>

                        </tr>';                         
                    }
                    ?>
                    </tbody>
  • 写回答

2条回答 默认 最新

  • weixin_33716557 2017-02-14 06:13
    关注

    You have error in your sql query. As you not passing correct parameters. Please see below code.

    $id = $_GET['id'];
    $data = $_GET['data'];
    $capacity = $_GET['capacity'];
    
    // Check Sql
    $query = "update mytable set name='$data',capacity='$capacity' where id='$id'";
    if(mysqli_query($con,$query)){
    
      echo "success";
    } else{
        echo 'failed';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘