1

I am looking to auto-populate a php / html form with data as radio buttons to allow a user to choose from his own records and convert their selection into a reusable variable for another form.

I have got the login and password from database working fine and can create a session $variable with the appropriate user_id

Then I get stuck with the right query and html construct. I have look at various examples to no avail.

My table is as follows

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

I have created the basic query to pull the data but am struggling to get this displayed as radio buttons for a single selection.

$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"];`

Then I try to convert to radio buttons to allow for single record selection

<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>

But the code is invalid and as I am quite new to PHP and still learning. I am stumped and there are just too many examples that seem to fall short one way or another.

I have tried so many different syntax and I am struggling to get my head around how to allow user selection of data from mysql in selectable format.

Any help or nudge in the right direction would be great.

sjagr
  • 15,983
  • 5
  • 40
  • 67
ToeDipped
  • 11
  • 1
  • You need checkboxes, not radio inputs. I'm building a small example for you. – sodawillow Jan 15 '15 at 17:54
  • well the first thing you should do is drop they **mysql_*** api as it is deprecated. before you do anything else. it is not secure.. you should look into parameterizing your queries with **mysqli_*** or **PDO** – John Ruddell Jan 15 '15 at 17:56
  • It's not clear to me what you want to do. The table is nice because I can see what I'm starting with. But I don't know where to end up. Can you provide an example of what you want to appear on the page. – bloodyKnuckles Jan 15 '15 at 20:26

1 Answers1

0

First and foremost : DO NOT USE MYSQL_* functions, they are deprecated. Use PDO or MySQLi instead. See : Why shouldn't I use mysql_* functions in PHP?

Now, a working example :

HTML form to choose the columns you want to display :

<!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>

Beginning of PHP file to process the checkboxes and build the request accordingly :

<?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 :)

?>
Community
  • 1
  • 1
sodawillow
  • 12,497
  • 4
  • 34
  • 44