Jump to content

Autofill PHP form from mySQL database

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
Hi guys as usual I have a little "easy" (I hope) question for you.

I have a list of results as below that an user can see in a display.php file

Quote

id--------------Category--------------Name--------------Nation--------------Author--------------Date
1---------------Sport-----------------Soccer--------------Spain---------------Messi--------------15 Jan 2010
2---------------Sport-----------------Soccer-------------France-------------Benzema------------02 Mar 2010
3---------------Sport---------------Volleyball--------------Italy----------------Rossi---------------22 Jan 2009
4---------------Sport-----------------Soccer------------Germany--------------Lahm--------------11 Nov 2009

I'd like to be able to modify one selected result using a select box and a button (eg: Modify as label).
So something like this:

Quote

id--------------Category--------------Name--------------Nation--------------Author--------------Date
1
[]---------------Sport-----------------Soccer--------------Spain---------------Messi--------------15 Jan 2010
Modify

2
[]---------------Sport-----------------Soccer-------------France-------------Benzema------------02 Mar 2010
Modify

3
[]---------------Sport---------------Volleyball--------------Italy----------------Rossi---------------22 Jan 2009
Modify

4
[]---------------Sport-----------------Soccer------------Germany--------------Lahm--------------11 Nov 2009
Modify

I'd like that when an user select the select box of the row he wants to modify and press the button, he will be redirect to a page where he can see the standard form (he already used to insert the entry) but with all data already filled in and he has only to modify or change the entries he wants).
Is it possible to do this?

I thought that in the URL must be the id of the field I want to modify, so I wrote this:

<?php
$query=" SELECT * FROM MyTable ORDER BY id_name ASC limit $eu, $limit ";

$result=mysql_query($query);
	echo mysql_error();

while($row = mysql_fetch_array($result))
{

  $modify_page_name="admin.php?page=modifyentry&id=$row[id]";

  echo "$row[id]<br><form name='f2' method='post' action='$modify_page_name'><input
  type='checkbox' value='' name='$row[id]'><br><input type='submit' name='submit' id='submit'
  value='Modify'></form>";
}
?>

And if I test the URL I have this:

Quote


By now it seems that everything works good, but when I try the autofill way to populate the PHP form I have a little problem, I wrote:

<?php
$modify_entry_query=" SELECT * FROM MyTable WHERE id='$id' ";

	$modify_entry_result=mysql_query($modify_entry_query);
		echo mysql_error();
	
	$row_old = mysql_fetch_array($modify_entry_result);
?>
    <div class="wrap">
    <h2>Modify Entry</h2>
    <form name="f3" method="post" enctype="multipart/form-data" action="<?php echo $action_url ?>">
    Name: <br>
    <input type="text" name="id_name" value="<?php echo $row['id_name'] ?>">
    <p />
<input type="submit" name="submit" id="submit" value="Submit">
    </form>
    </div>


<?php
if (isset($_POST['submit']) && $_POST['submit']==TRUE)
	{
        	$id_name = ($_POST['id_name']);

$sql_run = "UPDATE MyTable SET id_name='$id_name' WHERE id=$id ";
	$sql_result = mysql_query($sql_run);
	
}
?>

It doesn't work because in the "Name" field I have another name and not the right one.
Anyone can help me and tell me where could be the problem please?

#2
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
SOLVED!