Auto Populate HTML Form Value with SQL Data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
limo08
Forum Newbie
Posts: 1
Joined: Sun Oct 19, 2008 4:10 pm

Auto Populate HTML Form Value with SQL Data

Post by limo08 »

Hello,
I have a movie database that I am working on slowly. I created a search function, and another function that allows me to add data into the database. All works great. I would like to now create a form where I can update or delete the information as needed. For example, when I search for "Lord of the Rings", the movie title comes up in my search. When I click on it, I would like it to take me to form2.php that I created that has all the fields (Movie Title, Media Type etc...) and I would like those field auto populated with the information from the link i clicked. ID=2

When I am in the form and do <?php echo "$ID" ?> for the value, that works fine and displays the correct value, but the other values do not seem to be passed $TITLE, $MEDIA etc..

Essentially, I would like to be able to click update, and update the data, or delete to delete the data. Any help is appreciated. Thank you!!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Auto Populate HTML Form Value with SQL Data

Post by califdon »

There's no such thing as auto populating in PHP. You have to do it all yourself. It's not that hard, really, but you do have to plan how you will do so and then write the code. Or use somebody else's script. You may need to consider either storing data in session variables, to be used by other scripts, or since you have the ID value, do another database query to retrieve the data.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Auto Populate HTML Form Value with SQL Data

Post by aceconcepts »

What you want to do is POST the movie ID via the form to form2.php (the page where you're going to display the movie details.

Get the movie Id by doing the following:

Code: Select all

 
if(isset($_POST['submit']))
{
   //this will retrieve the movie ID from the form's POST method (alternatively, you could use the GET method).
   $movie_id=$_POST['movie_id'];
}
 
Once you get the movie ID, query your database and get the movie's details:

Code: Select all

 
//this will query your database for the movie with matching ID
$query=mysql_query("SELECT * FROM movie_table WHERE intMovieId='$movie_id'");
 
Now you need to check whether a movie with that ID exists. If so, get movie data.

Code: Select all

 
if(mysql_num_rows($query)>0)
{
   $row=mysql_fetch_array($query);
   
   //display movie data in form so it can be updated - let me know when you've got this far and i'll help you sort out you update form.
}
else
{
   //movie ID not found
}
 
Post Reply