Jump to content

Archived

This topic is now archived and is closed to further replies.

Fearpig

Recomend a tutorial

Recommended Posts

Hello,
Can anyone recomend a tutorial on editing MySQL data with PHP forms. I've tried a hundred ways and I just get stuck.  :'(

Cheers
Tom

Share this post


Link to post
Share on other sites
wat do you want to do, retrieve the data from a table in a form for editing.... is it one record at a time or multiple

Share this post


Link to post
Share on other sites
Hi,
Basically its a telephone book. I want to list all names as links. When you click on the link it takes you to a form where you can edit the details for that person (first_name, last_name, role, number, department... etc.) .

It seems simple enough and I can get most of it working but the last few steps seem to outwit everybody so I was going back to basics.

Share this post


Link to post
Share on other sites
Sorry... its on an IntrAnet server with no public access! The webserver doesn't have PHP / MySQL.

Share this post


Link to post
Share on other sites
;D

Went back to basics and did it already..... just incase anyone else was having the same issue.... or thinking about it can anyone spot a problem with doing it this way?

I did this in three pages:

1. list of clickable names that link to a form - Edit_ListPeople.php
2. A form to fill out and post results - Edit_Person.php
3. A page to process the form contents - Edit_ProcessForm.php

Edit_ListPeople.php
[code]
<html>
<head>
</head>
<body>
<?php

$db = mysql_connect("localhost", "root", "password");

mysql_select_db("Telephonelist",$db);

$result = mysql_query("SELECT * FROM tbl_telephonenumbers",$db);

echo "<table border=1>\n";

echo "<tr><td><b>Name</b></td><td><b>Position</b></tr>\n";
//colum names

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

printf("<tr><td><a href=\"%s?id=%s\">%s %s</a></td><td>%s</td></tr>\n",
//lay out results 2 in the first column and 1 in the second
"Edit_Person.php", $myrow["ID"], $myrow["First_Name"], $myrow["Last_Name"], $myrow["Role"]);



}
echo "</table>\n";

?>

</body>
</html>
[/code]

Edit_Person.php
[code]
<html>
<head>
</head>
<body>
<?php

$db = mysql_connect("localhost", "root", "password");
mysql_select_db("Telephonelist",$db);

$id = $_GET["id"];
$sql = "SELECT * FROM tbl_telephonenumbers WHERE ID=$id";
$result = mysql_query($sql);       
$myrow = mysql_fetch_array($result);
?>
   
        <form action="Edit_ProcessForm.php" method="post">
        <input type=hidden name="Frm_ID" value="<?php echo $myrow["ID"] ?>">
 
        First Name:<INPUT TYPE="TEXT" NAME="Frm_First" VALUE="<?php echo $myrow["First_Name"] ?>" SIZE=30><br>
        Last Name:<INPUT TYPE="TEXT" NAME="Frm_Last" VALUE="<?php echo $myrow["Last_Name"] ?>" SIZE=30><br>
        Role:<INPUT TYPE="TEXT" NAME="Frm_Role" VALUE="<?php echo $myrow["Role"] ?>" SIZE=30><br>
 
        <input type="hidden" name="cmd" value="edit">
        <input type="submit" name="submit" value="submit">
 
          </form>

</body>
</html>
[/code]

Edit_ProcessForm.php
[code]
<html>
<head>
</head>
<body>
<?php

$db = mysql_connect("localhost", "root", "password");
mysql_select_db("Telephonelist",$db);

$id = $_POST["Frm_ID"];
$First = $_POST["Frm_First"];
$Last = $_POST["Frm_Last"];
$Role = $_POST["Frm_Role"];
   
$sql = "UPDATE tbl_telephonenumbers SET First_Name='$First',Last_Name='$Last',Role='$Role' WHERE ID=$id";
//replace news with your table name above
$result = mysql_query($sql);
echo "Thank you! Information updated.";
echo "<br>";
echo "<a href='index.htm'>Click here to return to the phone index</a>";


?>

</body>
</html>
[/code]

Share this post


Link to post
Share on other sites
It looks that should work fine for you.

However, be sure to validate user input before updating the database.

Head to the link below for more information:

http://www.phpfreaks.com/phpmanual/page/security.database.sql-injection.html

Share this post


Link to post
Share on other sites

×

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.