Editing database content via forms

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
luke4868
Forum Newbie
Posts: 6
Joined: Thu Jun 11, 2009 1:44 pm

Editing database content via forms

Post by luke4868 »

Hey, I'm fairly new to PHP (as well as using databases). Ive managed to create a simple 'Add Customer' form for an admin area i'm doing, but i'm curious. How do you load database information into a form? For example, Loading a customers data when you enter their ID (or postcode or whatever). And the onced it loaded into the form its obviously editable, then you press update which will update the records with whatever is in the form at the time.

I might have over explaining what im trying to do there, but there we go.

Thanks, Luke.
vtvstv
Forum Newbie
Posts: 10
Joined: Sun May 24, 2009 3:19 am

Re: Editing database content via forms

Post by vtvstv »

Create a file that is called for example preupdate.php that will call all the info from your db based on a specific id and then echo the content into a form and its text boxes. So for example you have a database with the columns

id, name, email, address

Code: Select all

<?php     
//Database connection
include("connectioninfo.php");
$con = mysql_connect(localhost,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("yourdb_", $con);
 
// If there is no id to work with
if ($_REQUEST['id']=='') {
    echo "<form action='' method=POST><input type='text' name='id' value='' />";
    echo "<input type='submit' value='submit' name='submit' /></form>";
}
 
//what to do if there is a value available for id
else {
 
// First we query the database
$customerid = $_REQUEST['id'];
$data = mysql_query("SELECT * FROM yourtable WHERE id='$customerid'") or die(mysql_error()); 
 
// Now we echo out the contents
$info = mysql_fetch_array($data);
{ 
echo "<form action='update.php' method='POST'><input type='text' name='name' value='".$info['name']."' />";
echo "<input type='hidden' name='id' value='".$customerid."'><input type='text' name='email' value='".$info['email']."' />";
echo "<input type='text' name='address' value='".$info['address']."' /><input type='submit' name='submit' value='submit' /></form>";
}
 
//Close connection to database
mysql_close($con);
?>
You will see from the above code that the form action=update.php - so now create the update.php file using the code below. This will do the magic with the new info and updates it back in the the database.

Code: Select all

 <?php
//Database connection
include("connectioninfo.php");
$con = mysql_connect(localhost,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("yourdb_", $con);
 
// Addslashes to characters that might affect processing
$name= addslashes($_REQUEST['name']);
$email= addslashes($_REQUEST['email']);
$address = addslashes($_REQUEST['address']);
 
// Update the Database
$sql="UPDATE yourtable SET name = '$name', email = '$email', address = '$address' WHERE id = '$_REQUEST[id]'";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
 
// If update carried out successfully
echo "<center><h1>Record Updated!!!</h1></center>";
 
//Close connection to database
mysql_close($con);
?>
Thats it I think that should do what you want. Just add more fields as needed any probs give me a shout.
luke4868
Forum Newbie
Posts: 6
Joined: Thu Jun 11, 2009 1:44 pm

Re: Editing database content via forms

Post by luke4868 »

Hey vtvstv

Thanks for the help, i'll have a go at that sometime over the weekend and see how it turns out.
Post Reply