dongpiansui8755 2015-01-10 18:39
浏览 50

回调可编辑PHP不起作用

I have created a table where I can edit inside td and it works very well. I edited the name of product and it changed successfully just on my webpage, BUT it is not updating into Database.

My database would be like this:

Here is the code for editing inside td.

$results = $mysqli->query("SELECT * FROM productList");
while($row = $results->fetch_assoc()) {
    <td> <span class="edit" id="'.$row['ID'].'"> '.$row['name'].' </span> </td>
}

For editable, I have to use AJAX to call the value back. Something like this:

jQuery(document).ready(function() {  
    $.fn.editable.defaults.mode = 'popup';
    $('.edit').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: "edit.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!!');
            }
        });
    });
});

After AJAX, I have to call url to edit.php. The code for edit.php is below:

$mysqli = new mysqli('localhost','root','root','database_name');
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

if($_GET['id'] and $_GET['name'])
{
    $id = $_GET['id'];
    $name = $_GET['name'];
    if ($mysqli->query("UPDATE productList SET name='$name' WHERE ID='$id'"));
    $mysqli->close();
    echo 'success';
}

I have tried to update manually in phpmyadmin, it works very well there. But not here. It should be working right now. Am I missing something?

  • 写回答

1条回答 默认 最新

  • duanfuchi7236 2015-01-10 18:53
    关注

    Need to update your code like below. And you can directly call the edit.php?id=..&name=.. to debug if the file is ok.

    jQuery:

    $.ajax({
                url: "edit.php?id="+x+"&name="+y,
                type: 'GET',
                success: function(s){
                    alert(s) ;
                    if(s == 'status'){
                       $(z).html(y);
                    }                    
                },
                error: function(e){
                    alert(e.status);
                }
            });
    

    PHP:

    if($_GET['id'] && $_GET['name']) // this is not SQL! so and will not work!
    {
        $id = $_GET['id'];
        $name = $_GET['name'];
        if ($result = $mysqli->query("UPDATE productList SET name='".$name."' WHERE ID='". $id."'")){
    
           $mysqli->close(); 
           echo 'success';
        }else{
            echo mysqli->error;
        }
    
    
    }
    

    READ Some MYSQLI DOC

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据