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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.