Jump to content

selecting a record


phppup

Recommended Posts

I am able to view record numbers 22, 84, and 203 by exectuing my PHP script with the correct record ID.

 

However, what I really want to do is have a simple form whereby I can input the desired record number and have it upload the data for me.

 

I imagine it would look something like this:

"SELECT * FROM mytable WHERE id = specified_record"

 

but need a bit of guidance to get the HTML input fields 'name' into the PHP request.

Link to comment
https://forums.phpfreaks.com/topic/257548-selecting-a-record/
Share on other sites

you can call the input field anything you like in the html, just use the same name to retrieve it in the php when submited.

 

<input type="text" name="record">

 

 

<?

$record = $_POST['record'];

"SELECT * FROM mytable WHERE id = " . $record

?>

 

of course you need to add security to this to prevent sql injection, but hopefully that gives you the idea

 

 

Link to comment
https://forums.phpfreaks.com/topic/257548-selecting-a-record/#findComment-1320093
Share on other sites

First, I would specify a query that would produce all of the ID numbers to display to the user and have it populate a drop down list.

 

<form action='' method='post'>
<select name='record_id'>
<?php
$sql = "select ID from table";
$query = mysql_query($sql) or die($sql . "<br />" . mysql_error());
if(mysql_num_rows($query) > 0)
{
    while($row = mysql_fetch_assoc($query))
    {
        $record_id = $row['ID'];
        echo '<option value="' . $record_id . '">' . $record_id . '</option>';
    }
}
?>
</select>
</form>

 

then, wherever you have the form action pointing to, you would add this code to grab the value of the select tag.

 

$record_id = (isset($_POST['record_id'])) ? $_POST['record_id'] : '';
//check for $record_id emptiness
if(!empty($record_id))
{
//execute code block
}

Link to comment
https://forums.phpfreaks.com/topic/257548-selecting-a-record/#findComment-1320094
Share on other sites

So I would have:

 

$record_id = (isset($_POST['record_id'])) ? $_POST['record_id'] : '';

//check for $record_id emptiness

if(!empty($record_id))

{

"SELECT * FROM mytable WHERE id = $record_id "

}

 

Is that correct??  Am I missing any brackets or braces??

 

if this code is on the page where you form action points to, it will hold the select value correctly, but the code your posted itself will not actually do anything. You also forgot to store the SQL string inside of a variable.

Link to comment
https://forums.phpfreaks.com/topic/257548-selecting-a-record/#findComment-1320110
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.