Page 1 of 1

edit database using form...plz help

Posted: Wed Aug 06, 2008 3:15 pm
by odie2828
here is my code to recall information from the database and the re enter it into the data base.

I am not getting any errors but it isnt wrking.

Code: Select all

 
 
<?php
 
[color=#008000]include[/color]([color=#FF0000]"config.php");[/color]
 
     if([color=#0000FF]isset[/color]([color=#0000FF]$_POST[/color][[color=#FF0000]'submit'[/color]]))
 
  {
 
      $title = mysql_escape_string($_POST['title']);
 
      $text1 = mysql_escape_string($_POST['text1']);
 
      
         $result = mysql_query("UPDATE 'news' SET 'title'='$title' AND 'text1'='$text1' WHERE 'newsid'='$newsid'",$connect);
 
 
 
          echo "<b>Thank you! News UPDATED Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
 
          echo "<meta http-equiv=Refresh content=4;url=index.php>";
 
}
 
elseif(isset($_GET['newsid']))
 
{
 
 
 
        $result = mysql_query("SELECT * FROM news WHERE newsid='$_GET[newsid]' ",$connect);
 
        while($myrow = mysql_fetch_assoc($result))
 
             {
 
                $title = $myrow["title"];
 
                $text1 = $myrow["text1"];
 
               
 
?>
 
<br>
 
<h3>::Edit News</h3>
 
 
 
<form method="post" action="<?php echo $PHP_SELF ?>">
 
<input type="hidden" name="newsid" value="<? echo $myrow['newsid']?>">
 
 
 
Title: <input name="title" size="40" maxlength="255" value="<? echo $title; ?>">
 
<br>
 
Text1: <textarea name="text1"  rows="7" cols="30"><? echo $text1; ?></textarea>
 
<br>
 
 
<input type="submit" name="submit" value="Update News">
 
</form>
 
<?
 
              }//end of while loop
 
 
 
  }//end else
 
?>
 
 
Any Suggestions?

Re: edit database using form...plz help

Posted: Wed Aug 06, 2008 7:35 pm
by dajawu
First you need to grab the newsid from $_POST after you submit the form. I also changed your SQL code a little. Use these two lines below in place of your SQL line and it will work:

Code: Select all

 
$newsid = $_POST['newsid'];
 $result = mysql_query("UPDATE `news` SET `title` = '$title', `text1` = '$text1' WHERE `newsid` = $newsid LIMIT 1",$connect);
 
Also it seems like you have Global Variables turned on. YOu should call variables with their full name. For example you have <form action="<?php echo $PHP_SELF ?>" method="post">, but you shoudl really have it <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

Re: edit database using form...plz help

Posted: Wed Aug 06, 2008 8:06 pm
by califdon
dajawu didn't mention this, but he also corrected your use of single quotes to back-ticks. They are different characters. The names of tables and fields of tables may be enclosed in back-ticks ( ` ), but not single quotes ( ' ).

Re: edit database using form...plz help

Posted: Thu Aug 07, 2008 2:45 pm
by odie2828
thank you soooo much! that was starting to get frustrating!!

i see the difference between single quotes and back-ticks.

does this forum use points? can i give you guys some?

Re: edit database using form...plz help

Posted: Thu Aug 07, 2008 7:38 pm
by califdon
Nah, but just knowing that you solved your issue and learned something in the process is good enough for us. Drop back here again.