内联更新mysql使用jquery/php

inline update in mysql using jquery/php

本文关键字:jquery php 使用 mysql 更新      更新时间:2023-09-26

我正在使用JQuery/Ajax and php/MySQL执行CRUD操作

我能够insert/selectdelete数据,但我不得不卡在编辑/更新。我拉数据到文本框,当我点击编辑按钮,但编辑后,当我点击保存按钮无法在mysql数据库更新!!

任何帮助都是感激的

<span class="noedit name" idl='<?php echo $row->id;?>'>
    <?php echo $row->url;?>
</span>
<input id="url1" name="url1" class="form-control edit name url1" value="<?php echo $row->id;?>"/>
<a ide='<?php echo $row->id;?>' id="edit" class='editOrder' href="#" style="display:block-inline;">EDIT</a>
<a idu='<?php echo $row->id;?>' id="update" class='update saveEdit' href='#' style='display:none;'>SAVE</a>
<a idc='<?php echo $row->id;?>' id="cancel" class='cancelEdit edit' href='#' style='display:none;'>CANCEL</a>

Jquery代码

$('body').delegate('.edit','click',function(){
    var IdEdit = $(this).attr('ide');
    alert(IdEdit);
    $.ajax({
        url:"pages/feeds.php",
        type:"post",
        data:{
            editvalue:1,
            id:IdEdit
        },
        success:function(show)
        {
            $('#id').val(show.id);
            $('#url1').val(show.url);
        }
    });
});
$('.update').click(function(){
var id = $('#id').val()-0;
var urls = $('#url1').val();
$.ajax({
    url:"pages/feeds.php",
    type:"post",
    async:false,
    data:{
        update:1,
        id:id,
        upurls:urls
    },
    success:function(up)
    {
        $('input[type=text]').val('');
        showdata();
    },
    error:function(){
        alert('error in updating');
    }
});
});
PHP代码

if(isset($_POST['editvalue']))
{
    $sql = "select * from test where id='{$_POST['id']}'";
    $row = mysql_query($sql);
    $rows = mysql_fetch_object($row);
    header("Content-type:text/x-json");
    echo json_encode($rows);
    exit();
}
if(isset($_POST['update']))
{
    $sql = "
        update test
        set 
            url='{$_POST['upurls']}'
        where id='{$_POST['id']}'
    ";
    $result = mysql_query($sql);
    if($result)
    {
        //alert('success');
        echo 'updated successfully';
    }
    else
    {
        //alert('failed');
        echo 'failed to update';
    }
}

我在代码中没有看到#id输入。它在那里吗?
我认为问题就在这里。

如果存在此输入,则使用以下提示:
检查是否所有值(id, url)都被发送到PHP脚本。
您可以在Javascript中使用console.log或在PHP中使用print_r, var_dump函数。

变化

$('.update').click(function(){

$('.saveEdit').click(function(){