Log in

View Full Version : Populate a form using a database.



griffinwebnet
07-20-2011, 04:23 PM
Ello All,

I am trying to write an app that acts as a booking system for my dj company, i have a form for new events that inserts the data into a mySQL database. then i have a table that lists all the events on a different page. in that table i have an 'Edit Event' button next to each entry. what i need to do is when i click that button, it will load a form identical to the 'new event' form but populated with the values for that event.

to populate the form i figured id use a script like this:


<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM field");

while($row = mysql_fetch_array($result))
{
echo '<form action="">';
echo '<input type="text" name="client_name" value=".$row['cli-name']">';
///Etc, Etc, Etc.
echo '</form>';
}

mysql_close($con);
?>


what i dont know is 2 things, how to select the row using that button, and how to set up a select as i have many values to choose from in the select, but how do i make the one in the database the selected one?:confused:

Thanks in Advance,
Your help is much appreciated.

-JL Griffin

griffinwebnet
07-20-2011, 07:01 PM
I should also add that i am open to a differant way of doing this. If you know a better or more efficient way, please let me know.

Thanks JL Griffin

liamallan
07-21-2011, 09:59 PM
are u using a form where the button is to edit?

if u are, u may want to add a hidden field in ur form, which will hold the id of each event eg.

<input type="hidden" name="id" value="'.$row['id'].'"/>

im guessing that once u press the submit button, the form is submitted to the code u posed, so u will need to catch the id from the posted form like so:

<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM field where id=".$_POST['id']."");

while($row = mysql_fetch_array($result))
{
echo '<form action="">';
echo '<input type="text" name="client_name" value="'.$row['cli-name'].'">';
///Etc, Etc, Etc.
echo '</form>';
}

mysql_close($con);
?>

and hopefully that will display all the info on the selected event