Database help for a newbie

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
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Database help for a newbie

Post 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);
?>
craigh
Forum Newbie
Posts: 19
Joined: Sun May 19, 2002 2:50 pm

Re: Database help for a newbie

Post 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);
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post 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
Post Reply