Insert Delete Update in one page

Hi all,
How would i create a page which can do an insert delete and update in the same page itself.What i mean is
Initially when the user comes to the page there will be no records
So the page will show no records and a buttton to add records…Once the user enters something it gets validated and then submits it to itself and now the records is displayed with an edit/delete option so that the user can now edit/delete the record.
As of now i was doing it wiht sepearte pages for insert/delete/update and wanted to knwo
what is the best way to achive this.
Thanks
Abbie

Something like this should do you (modify for your own needs) - you basically just tell the one script to expect specific variables from the form if submitted and to display certain variables and do certain tasks (like DB queries & displaying messages) dependant upon these variables:

<?
   // this file might be called index.php:
   // user is adding stuff:
   if(isset($_POST['add']))
   {
   	// 1: do some error checking here
   	// 2: throw an error or do DB INSERT here
   	// 3: set variable that displays "edit" button:
   	$display_button_valu = "edit";
   	$display_button_name = "edit";
   	// 4: set a confirmation message:
   	$msg = "Your stuff has been ADDED";
   }
   // user is editing stuff:
   elseif(isset($_POST['edit']))
   {
   	// 1: do some error checking here
   	// 2: throw an error or do DB UPDATE here
   	// 3: set variable that displays "delete" button:
   	$display_button_valu = "delete";
   	$display_button_name = "delete";
   	// 4: set a confirmation message:
   	$msg = "Your stuff has been EDITED";
   }
   // user is deleting stuff:
   elseif(isset($_POST['delete']))
   {
   	// 1: do some error checking here
   	// 2: throw an error or do DB DELETE here
   	// 3: set variable that displays "add" button:
   	$display_button_valu = "add";
   	$display_button_name = "add";
   	// 4: set a confirmation message:
   	$msg = "Your stuff has been DELETED";
   }
   // user is doing nothing, it's his/her first time (ooer)
   else
   {
   	// 1: set variable that displays "add" button:
   	$display_button_valu = "add";
   	$display_button_name = "add";
   	// 2: set a welcome message:
   	$msg = "Welcome. Please add some stuff in the form below";
   }
   
   ?>
   <!-- display your message here -->
   <p><?=$msg?></p>
   
   <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
   <!-- create your form here -->
   
   <!-- display your button here -->
   <input type="submit" name="<?=$display_button_name?>" value="<?=$display_button_valu?>" />
   </form>

Hope that helps :slight_smile:

thanks that looks good, can u set it up with an example of connecting with a mysql db and then displaying,adding,deleting,editing records.
am quite new to PHP and would like some kind of template/code to get started.
Thanks for your time

That example would be a good “template” php script to do things as you asked, IE: inserting/updating/deleting in a single file.

However, if you need to ask about connecting to a DB in the first place, your best bet would be to do a search and here at Sitepoint or get a book which shows you the many methods of querying a database and which one would best suit you.

I hear Kevin Yank’s book is pretty darned good, though I’ve not read it myself.

Good luck,
Russ

Here’s how I connect to my database:


mysql_connect ("localhost" , "USERNAME" , "PASSWORD") or die(mysql_error());
mysql_select_db ("DATABASE") or die(mysql_error());

Of course, you’d replace USERNAME, PASSWORD and DATABASE with your username, password and database name.

As for adding and deleting files, I gave up on that long ago. I spent days and days working on it, and I never could get it to work. What a pain in the butt.

You might try Googling for some pre-made scripts. I discovered a script made by an Italian which was pretty cool. The only problem is it made copies of all my tables, which seemed pretty wasteful. But I’ll but someone has made some good scripts out there.