Page 1 of 1

Database help for a newbie

Posted: Sat Mar 22, 2003 4:53 am
by werlop
I am writing an administration page for a guestbook. What the admin page does is lets the administrator view the messages that are not authorised, then I want it to be able to change the value of authorised from No to Yes. The cdoe I have for doing that is below. My problem is that when i press add to database, instead of changing the value of authorised, an new record is created (with all the same data) except the id has been incrimented, and authorised is set to Yes. The origional record remains. Could somebody please explain what I am doing wrong - I don't want a new record to be created, I just want the origional record to be updated.


Thanks
:D

Code: Select all

<?php
  	if ($id) {
	
	

	$sql = "UPDATE `guestbook` SET name='$name',location='$location',email='$email',check='$check',homepage='$homepage',comment='$comment',ip='$ip',date_time='$date_time',authorised='Yes' WHERE `id`='$id'";
	print $sql;

  }

  // run SQL against the DB

  $result = mysql_query($sql);
?>

Re: Database help for a newbie

Posted: Sat Mar 22, 2003 7:02 am
by craigh
Try what is below. Also does the print $sql print what you would expect? Is $id being accurately included?

Code: Select all

<?php
  	if ($id) {
	$sql = "UPDATE guestbook SET name='$name',location='$location',email='$email',check='$check',homepage='$homepage',comment='$comment',ip='$ip',date_time='$date_time',authorised='Yes' WHERE `id`=".$id;
	print $sql;
       }

  // run SQL against the DB

  $result = mysql_query($sql);
?>

Posted: Sat Mar 22, 2003 7:38 am
by twigletmac
If all you want to change is the value of authorised try:

Code: Select all

<?php
if (!empty($id)) { 
    $sql = "UPDATE guestbook SET authorised='Yes' WHERE `id`=".$id; 
    // run SQL against the DB 
   @mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
}
?>
Mac

Posted: Sat Mar 22, 2003 2:41 pm
by werlop
Thanks, I eventually found the error myself, the person who origionally wrote this page had 2 tables in the database, and so did not require to pass $id via the form, i simply added a hidden field with <?php print $id; ?> and it worked great. Thanks a lot! :D