Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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 使用函数从mysql数据中选中一个单选框_Php_Mysql_Function_Radio Button - Fatal编程技术网

Php 使用函数从mysql数据中选中一个单选框

Php 使用函数从mysql数据中选中一个单选框,php,mysql,function,radio-button,Php,Mysql,Function,Radio Button,我正试图建立一个编辑页面来编辑我以前的文章 因此,当我单击帖子前面的edit链接时,它会将帖子id发送到edit.php 现在我的问题是,我想在编辑页面中选中一个显示我当前文章类别的单选框 我有这样一个函数: function category($name,$postid){ if( $name == $postid) { return "checked"; } } 在edit.php中 $postid=$_GET['postid'] $query=mys

我正试图建立一个编辑页面来编辑我以前的文章 因此,当我单击帖子前面的edit链接时,它会将帖子id发送到edit.php 现在我的问题是,我想在编辑页面中选中一个显示我当前文章类别的单选框 我有这样一个函数:

function category($name,$postid){
if( $name == $postid)
    {
        return "checked";   
    }
}
在edit.php中

$postid=$_GET['postid']
$query=mysqli_query($con,"SELECT *FROM category");
    while($s=mysqli_fetch_array($query)){
    echo 
'<table width="200" border="1">
    <tbody>
    <tr>
    <td>'.$s['category_name'].'</td>
    <td><input type="radio" name="post_category" value="'.$s['category_name'].'" '.category($s['id'],$postid).'></td>
    </tr>
    </tbody>
</table>';
}

但是它没有选中单选框

您应该告诉PHP编写函数返回:

编辑1

将函数更改为始终返回某些内容

function category($id, $postid) {
    if( $id== $postid)
    {
        return "checked";   
    }

    return null;
}
返回将使用checked或null填充var$checked或

while($s = mysqli_fetch_array($query)) {

    $checked = category($s['id'], $postid);

    echo 
        '<table width="200" border="1">
            <tbody>
                <tr>
                    <td>'.$s['category_name'].'</td>
                    <td>
                        <input... ' . $checked . '>
                    </td>
                </tr>
             </tbody>
         </table>';
}

如果没有看到不匹配的内容,很难判断,但是从您的命名来看,您似乎正在将Id与名称匹配。如果您匹配的是Id,那么应该使用parseInt来确保您处理的是整数,并比较相同格式的数据。

您能告诉我如何将其正确地放入代码中吗?您调试了$postid的值吗?你确定会返回你的任何类别的检查结果吗?你是对的,我必须将这个$s['category_name']更改为$s['id'],但仍然不起作用