Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.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将查询转换为单选按钮进行选择_Php_Mysql_Forms_Radio Button - Fatal编程技术网

使用PHP将查询转换为单选按钮进行选择

使用PHP将查询转换为单选按钮进行选择,php,mysql,forms,radio-button,Php,Mysql,Forms,Radio Button,我希望用数据作为单选按钮自动填充php/html表单,以允许用户从自己的记录中进行选择,并将其选择转换为另一个表单的可重用变量 我已经从数据库获得了登录名和密码,可以使用相应的用户id创建会话$variable 然后,我不得不使用正确的查询和html构造。我看了很多例子都没有用 我的桌子如下 tableid Title Author published colour user_id ref_id ====================================

我希望用数据作为单选按钮自动填充php/html表单,以允许用户从自己的记录中进行选择,并将其选择转换为另一个表单的可重用变量

我已经从数据库获得了登录名和密码,可以使用相应的
用户id创建会话
$variable

然后,我不得不使用正确的查询和html构造。我看了很多例子都没有用

我的桌子如下

tableid Title           Author  published   colour  user_id ref_id
==================================================================
1       how to ski      pete    2014        red 2   1
2       how to cook     jones   2015        white   4       2
3       how to drive    smith   2012        yellow  2       3
4       how to cook     jones   2015        white   4       2
5       how to drive    smith   2012        yellow  4       3
我已经创建了基本查询来提取数据,但我很难将其显示为单个选择的单选按钮

$queryOrders2="SELECT * FROM books WHERE user_id=$user_id";
$resultOrders2=mysql_query($queryOrders2);
$row = mysql_fetch_assoc($resultOrders2);
print_r(mysql_fetch_assoc($resultOrders2));
$Title = $row["Title"];
$Author  = $row["Author"];
$published = $row["published"];
$colour = $row["colour"];`
然后,我尝试转换为单选按钮,以允许单记录选择

<form action="display-selections.php" method="POST">
<input class="radio_style" id="Title" name="Title" type="radio" value="Title">
<input class="radio_style" id="Author" name="Author" type="radio" value="Author">
<input class="radio_style" id="published" name="published" type="radio" value="published">
<input class="radio_style" id="user_id" name="user_id" type="radio" value="user_id">
<input name="submitted" type="submit" value="Submit">
</form>

但是代码是无效的,因为我对PHP很陌生,还在学习。我被难倒了,有太多的例子似乎在某种程度上不符合要求

我已经尝试了很多不同的语法,我正在努力弄清楚如何允许用户以可选格式从mysql中选择数据


向正确的方向提供任何帮助或推动都是非常好的。

首先和最重要的一点:不要使用MYSQL函数,它们已被弃用。改用PDO或MySQLi。见:

现在,一个工作示例:

选择要显示的列的HTML表单:

<!DOCTYPE html>
<html>
<head>
    <title>Form example</title>
</head>
<body>
    <form action="process.php" method="post">
        Choose columns (no selection = all columns) :
        <input type="checkbox" name="title" value="1"> Title
        <input type="checkbox" name="author" value="1"> Author
        <input type="checkbox" name="published" value="1"> Published
        <input type="checkbox" name="user_id" value="1"> User ID
        <input type="submit" value="submit">
    </form>
</body>
</html>

范例
选择列(无选择=所有列):
标题
著者
出版
用户ID
处理复选框并相应生成请求的PHP文件开头:

<?php

$fields = array();

foreach($_POST as $label => $value) {
    array_push($fields, "`" . $label . "`");
}

if(count($fields) > 0) {
    $field_list = implode(",", $fields);
} else {
    $field_list = "*";
}

echo $query = "SELECT " . $field_list . " FROM `table` WHERE condition";

//send query, retrieve and show data :)

?>

您需要的是复选框,而不是无线电输入。我正在为您构建一个小示例。您应该做的第一件事是删除mysql\uAPI,因为它已被弃用。在你做任何事情之前。它不安全。。你应该研究一下用mysqli_uu或pdo参数化你的查询,我不清楚你想做什么。这张桌子很好,因为我可以看到我从什么开始。但我不知道该在哪里结束。你能提供一个你想在页面上显示什么的例子吗。