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!!
Auto Populate HTML Form Value with SQL Data
Moderator: General Moderators
Re: Auto Populate HTML Form Value with SQL Data
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.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: Auto Populate HTML Form Value with SQL Data
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:
Once you get the movie ID, query your database and get the movie's details:
Now you need to check whether a movie with that ID exists. If so, get movie data.
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'];
}
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'");
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
}