Add/Edit/Delete question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bedcor
Forum Newbie
Posts: 24
Joined: Fri Jul 26, 2002 11:01 am
Location: Barrie, Ontario

Add/Edit/Delete question

Post by bedcor »

I know this going to sound really dumb! but i'm still new at this.

I have a HTML form to enter information and it has 3 buttons (ADD,Edit and Delete). This form calls the php script to do the work. I have the Add button working fine but i don't know how to incorporate the other two into the script?

can some one help?

thanx :?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

well, you didn't give me much to work with, but how about something like:

Code: Select all

$query = "UPDATE table1 SET text='{$_POSTї'text']}' WHERE text_id=0";
for editing and

Code: Select all

$query="DELETE FROM table1 WHERE text='{$_POSTї'text']}'";
for deleting.

Where is your problem? the queries? errors from PHP?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I think he has three submit buttons so (if I'm completely wide of the mark in answering this question just let me say the same as llimllib in that more info would've been nice)...

Give each button a name:

Code: Select all

<input type="submit" value="add" name="button" />
<input type="submit" value="edit" name="button" />
<input type="submit" value="delete" name="button" />
then in your code you can test to see which button was pressed and act accordingly:

Code: Select all

if ($_POST&#1111;'button'] == 'add') &#123;
    // if add clicked
&#125; elseif ($_POST&#1111;'button'] == 'edit') &#123;
   //  if edit clicked
&#125; elseif ($_POST&#1111;'button'] == 'delete') &#123;
    // if delete clicked
&#125;
You could use a switch for the above instead of the if...elseif statement. That's one way of having three submit buttons on a form.

Mac
bedcor
Forum Newbie
Posts: 24
Joined: Fri Jul 26, 2002 11:01 am
Location: Barrie, Ontario

Post by bedcor »

sorry, the problem is that the buttons on the HTML form page are created like this:

<input type=submit name="save" value="Save"> <input type=submit name="update" value="Update"> <input type=submit value="Next">

so then when i press them they go to the php script and execute a certain fuction from a query. This is the code i have for ADD:

$query = "INSERT INTO clientinfo (client_id, contact_name, position, department, street, city, prov_state, country, zip_postal, phNumber, faxNumber, pagerNumber, email) Values ('0', '$contact_name', '$position', '$department', '$street', '$city', '$prov_state', '$country', '$zip_postal', '$phNumber', '$faxNumber', '$pagerNumber', '$email');";

$result = mysql_query($query);

if ($result==$save)
{
mysql_close($db);
echo "<b>Saving Client Information was a Success!</b> ";
}
if(!$result)
{
echo "<b>Client Information not added!</b> ", mysql_error();
exit;
}
?>

so if $result = $query and that query is doing the adding to the database, then how do i do the edit and delete queries with the $result?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Mac, you want to be careful with that. IIRC, browsers aren't required to send the name of the submit button with a form; and I'm pretty sure that some browsers don't send it.

As for your form, the easiest way for you to do it is to have a separate form for each action, or to have the user check which action he wants to complete, and then only one submit button.

Finally, you don't use the $result to edit or delete; at the top of your form you check what action the user selected, and perform a different query for each one based on that.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I suppose I should've added an 'only tested on IE 6 'cos that's what I'm working on right now disclaimer' :) . I prefer to have three separate forms for adding, editing and deleting (obviously built dynamically to reduce effort) as there's less chance of doing the wrong action (and causing lots of damage) that way.

Mac
Post Reply