Current location: Hot Scripts Forums » Programming Languages » PHP » Php Mysql Update and Delete Form Problem


Php Mysql Update and Delete Form Problem

Reply
  #1 (permalink)  
Old 07-18-07, 09:25 AM
mlmorg mlmorg is offline
New Member
 
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Php Mysql Update and Delete Form Problem

Hi all,
I have created an edit page for my site http://www.katherinewagnerdesigns.co...lford/edit.php

The php is this:
PHP Code:

$query="SELECT * FROM home";

$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();
$i=0;
while (
$i $num) {
$date=mysql_result($result,$i,"date");
$subtitle=mysql_result($result,$i,"subtitle");
$text=mysql_result($result,$i,"text"); 
and then the code:
Code:
<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="$id">
Title: <br /><input type="text" name="ud_subtitle" value="<? echo $subtitle; ?>"><br />
Date: <br /><input type="text" name="ud_date" value="<? echo $date; ?>"><br />
Content: <br /><textarea name="ud_text" rows=10 cols=80><? echo $text; ?></textarea>
<input type="Submit" value="Update">
</form>
<form action="deleted.php" method="post">
<input type="hidden" name="id" value="$id">
<input type="Submit" value="Delete">
</form>
ending with this so that it copies each post out with each form:
PHP Code:

<? $i++;

}
?>
What happens when I click Update, however, is it only updates the first post...so anything I change in the posts below and hit on that Update button, it will still only changes the first post and the others stay the same. Somehow it looks like it is not getting the id number of each successive post. The updated.php code is:
PHP Code:

$query="UPDATE home SET date='$ud_date', subtitle='$ud_subtitle', text='$ud_text' WHERE id='$ud_id'";

@
mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo 
"Record Updated";
mysql_close(); 
And also, the Delete button doesn't do anything at all...deleted.php is:
PHP Code:

$query="DELETE FROM contacts WHERE id='$id'";

mysql_query($query);
echo 
"Record Deleted";
mysql_close(); 
Why is this stuff happening...I can't seem to find anything wrong in the code...but again, I am just learning this so I must be doing something wrong. Thanks for any help, Matt

Maybe there is a way to make each forms Update button update every post at once from each form...eliminating the need to detect which id Update button the user clicks on...?
Reply With Quote
  #2 (permalink)  
Old 07-25-07, 06:00 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I see two problems.
1. There is nothing that changes the value of $id in the forms. So they all have the same value.
2. You are using two different values for id in your queries ($ud_id and $id).

I don't see anywhere in your code that is collecting the information from
your forms. Like $_POST['ud_id'], $_POST['id'], $_POST['ud_date'],
$_POST['ud_subtitle'] and $_POST['ud_text'].

First you must fix problem #1. Then to fix problem #2 change $ud_id to $_POST['ud_id']
and $id to $_POST['id'] and $ud_date to $_POST['ud_date'] and $ud_subtitle to
$_POST['ud_subtitle'] and $ud_text to $_POST['ud_text'] in your queries.

And then again, maybe you aren't posting all your code so we can get a better picture
of what's going on.
__________________
Jerry Broughton

Last edited by job0107; 07-25-07 at 06:13 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete from database - problem HakonHoy Database 5 06-30-06 07:15 AM
MySQL, UPDATE vs DELETE and INSERT nassau PHP 6 01-16-06 12:59 PM
UPDATE: MySQL Auto Backup & Export v1.1 Beyonder General Advertisements 2 03-21-05 02:05 PM
PHP - MySql - CMS integration Styleemp Job Offers & Assistance 3 02-10-05 12:14 AM
php mysql updating problem kevinb PHP 2 11-06-04 03:15 PM


All times are GMT -5. The time now is 06:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.