Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

通过列表页列表的编辑id获取对应信息,取到了id,但输出的内容是错的

PHP
<?PHP
    header("Content-Type: text/html; charset=utf-8");

    include('conn.php');//链接数据库

    $sql = 'select id,article_name,check_box,radio_box,content,input_name,input_number,input_title,input_describe from article';
    $res = mysqli_query($conn,$sql);
    $data = array();
    $arr['count']=mysqli_num_rows($res);
    while ($row = mysqli_fetch_array($res,MYSQLI_ASSOC)){
        $arr['data'][] = $row;
    }
    echo json_encode($arr);
?>
Ajax
//编辑的id获取数据库数据
$(document).ready(function(){
    function getUrl(name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
    var id = getUrl('edit_id');
    console.log('id:' + id);
    $.ajax({
        url : "./php/edit.php",//后台请求的数据,用的是PHP
        type : "post",//请求方式
        data : {edit_id:id},
        async : false,//是否异步请求
        success : function(data) {
            var res = JSON.parse(data);
            for(let i=0;i<res.count;i++){
                var art_name = res.data[i].article_name;
                console.log(art_name);
                $("#art_name").val(art_name);
            }
        },
    })
})

前端页面编辑选项测试:
image
编辑选项详情部分页面:
image
前端console.log输出:
id:8
测试1
测试2
—————————————————————————————————————————————
对两个div获取数据库内容进行测试,想通过编辑对应id获取相对应的内容,我用for循环获取到的两个article_name,分别是测试1和测试2,并不是id:8对应的单独测试1,而且val出来的内容是测试2,怎么改能准确的获取对应的内容?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

哎,,你又重新开一个问题.... 你在 for 循环里面加一个 if (parseInt(res.data[i].id) == id) 把里面其它的语句写到这个 if 里面很难理解吗??

另外,你都把 id 传到 后端 php 去了,为什么后端 php 就不能在取 edit_id 不为0的情况下,sql 就只查 id =8 的文章呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...