Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Jquery中使用Ajax的内联编辑_Php_Jquery_Ajax_Pdo - Fatal编程技术网

Php Jquery中使用Ajax的内联编辑

Php Jquery中使用Ajax的内联编辑,php,jquery,ajax,pdo,Php,Jquery,Ajax,Pdo,有人能告诉我下面的代码哪里出了问题吗?我试图使用Ajax和Jquery使html表上的一些字段可编辑。编辑在前端工作,但当我刷新页面时,编辑消失,因为它们没有写入数据库。到目前为止,我的代码是: index.php--我正在尝试使名称字段可编辑 echo "<table cellpadding='0' cellspacing='0' border='0' class='table table-striped' id='datatable'> <thead>

有人能告诉我下面的代码哪里出了问题吗?我试图使用Ajax和Jquery使html表上的一些字段可编辑。编辑在前端工作,但当我刷新页面时,编辑消失,因为它们没有写入数据库。到目前为止,我的代码是:

index.php--我正在尝试使名称字段可编辑

echo "<table cellpadding='0' cellspacing='0' border='0' class='table table-striped' id='datatable'>
        <thead>
        <tr>
            <th>Name</th>
            <th>Address</th>
            <th>City</th>
            <th>County</th>
            <th>Phone</th>
            <th>Mobile</th>
            <th>Admin</th>
        </tr>
        </thead><tbody>";
        while( $row = $sth->fetchObject() ){
            echo '<tr>
                <td class="edit name '.$row->customer_id.'">'.$row->name.'</td>
                <td>'.$row->address_line_1.',<br />'.$row->address_line_2.',<br />'.$row->address_line_3.'</td>
                <td>'.$row->city.'</td>
                <td>'.$row->county.'</td>
                <td>'.$row->phone.'</td>
                <td>'.$row->mobile.'</td>
                <td><div class="btn-group">
                <a class="btn btn-primary" href="#">Admin</a>
                <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span>
                </a><ul class="dropdown-menu">
                <li><a href="mailto:'.$row->email.'"><i class="icon-envelope"></i>Email</a></li><li><a href="#"><i class="icon-pencil"></i>Edit</a></li><li><a id="'.$row->customer_id.'" class="delete"/><i class="icon-trash"></i>Delete</a></li>
                </td></tr>';
                    }
            echo "</tbody></table>";
echo”
名称
地址
城市
县
电话
可移动的
管理
";
而($row=$sth->fetchObject()){
回声'
“.$row->name。”
“.$row->address_line_1.”,
“.$row->address_line_2.”,
“.$row->address_line_3.” “.$row->城市。” “.$行->县。” “.$row->电话。” “.$row->mobile。”
  • 删除
  • '; } 回声“;
Jquery

<script>
            $(document).ready(function(){                           
                $('td.edit').click(function(){
                    $('.ajax').html($('.ajax input').val());
                    $('.ajax').removeClass('ajax');

                    $(this).addClass('ajax');
                    $(this).html('<input id="editbox" size="'+$(this).text().length+'" type="text" value="' + $(this).text() + '">');

                    $('#editbox').focus();

                }
            );
                $('td.edit').keydown(function(event){
                    arr = $(this).attr('class').split( " " );
                        if(event.which == 13)
                            { 
                            $.ajax({    type: "POST",
                                        url:"includes/edit-customer.php",
                                        data: "value="+$('.ajax input').val()+"&rownum="+arr[2]+"&field="+arr[1],
                                        success: function(data){
                                             $('.ajax').html($('.ajax input').val());
                                             $('.ajax').removeClass('ajax');
                                        }});
                        }

                    }
                );
                    $('#editbox').live('blur',function(){
                    $('.ajax').html($('.ajax input').val());
                    $('.ajax').removeClass('ajax');
                });
            });
    </script>

$(文档).ready(函数(){
$('td.edit')。单击(函数(){
$('.ajax').html($('.ajax输入').val());
$('.ajax').removeClass('ajax');
$(this.addClass('ajax');
$(this.html(“”);
$(“#编辑框”).focus();
}
);
$('td.edit').keydown(函数(事件){
arr=$(this.attr('class').split(“”);
if(event.which==13)
{ 
$.ajax({type:“POST”,
url:“包含/编辑customer.php”,
数据:“value=“+$('.ajax input').val()+”&rownum=“+arr[2]+”&field=“+arr[1]”,
成功:功能(数据){
$('.ajax').html($('.ajax输入').val());
$('.ajax').removeClass('ajax');
}});
}
}
);
$('#editbox').live('blur',function(){
$('.ajax').html($('.ajax输入').val());
$('.ajax').removeClass('ajax');
});
});
编辑customer.php

<?php

    //MySQL Database Connect
    require 'config.php';

    if(isset($_POST['rownum']))  
    {  
        update_data($_POST['field'],$_POST['value'],$_POST['rownum']);  
    }  

    print_r($_POST);

    function get_data()  
    {  
        $query = $dbh->prepare("SELECT * FROM customer");
        $query->execute();
        return $query;
    }  

    function update_data($field, $data, $rownum)  
    {  
        $query = $dbh->prepare("UPDATE customer SET ".$field." = '".$data."' WHERE customer_id = ".$rownum;);
        $query->execute();      
    }   
?>

更新语句中有一个分号:

$query = $dbh->prepare("UPDATE customer SET ".$field." = '".$data."' WHERE customer_id = ".$rownum;);
应该是

$query = $dbh->prepare("UPDATE customer SET ".$field." = '".$data."' WHERE customer_id = ".$rownum);

另一个问题是,您的
行数
字段
arr[]
中很容易混淆,因为您只是拆分并假设其中一个先到。

我认为您需要简化问题。首先,数据似乎没有保存在服务器端。在执行查询期间,不检查是否有错误。按此方式修改以查找错误/消除错误向量:

$result = $query->execute();     
// execute() can fail for various reasons.
if ( false===$result ) {
  die('execute() failed: ' . htmlspecialchars($query->error));
}'
和修改成功以查看返回的任何错误消息:

success: function(data){

alert(data);
}});

如果这是空的,那么我们可以排除查询中的错误

嘿,谢谢你的评论,我似乎得到了以下错误-致命错误:对非对象调用成员函数prepare()。第20行。我把那行改成了上面贴的@Tim Withers,但它似乎出现了一个错误。感谢你对错误处理的提醒,我在你和Tim的帮助下解决了这个问题:)非常感谢Hi@Tim Withers谢谢你,我修正了这个语句,但我似乎遇到了一个致命的错误:调用成员函数prepare()在该行中,您需要将数据库处理程序(
$dbh
)声明为全局处理程序,或者将其传递到函数本身<代码>$dbh
未初始化,否则不是PDO对象。很好,修复了错误,在此过程中我学到了一些新东西,非常感谢!