edit database using form...plz help

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
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

edit database using form...plz help

Post 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?
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: edit database using form...plz help

Post 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">
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: edit database using form...plz help

Post 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 ( ' ).
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: edit database using form...plz help

Post 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?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: edit database using form...plz help

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