Jump to content


Photo

Edit MYSQL row Problem!


  • Please log in to reply
4 replies to this topic

#1 HaZaRd420

HaZaRd420
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 21 April 2006 - 05:46 AM

Ok Heres the problem, I got to edit it, click the link, I edit the textarea, i click submit, and nothing happends. Its suppose to use the echo "Thank you! Information updated" When i click the submit button but it doesnt, it just goes back to the link to edit again. Please Help me.

<? 
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","DB","password"); 
    
//select which database you want to edit
mysql_select_db("DB"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from news order by id"); 
   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the news
      $subject=$r["subject"];//take out the title
      $id=$r["id"];//take out the id
      $author=$r["author"];
            $date=$r["date"];
            $content=$r["content"];
            
     //make the title a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$subject - Edit</a>";
      echo "<br>";
    }
}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM news WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
      
      <form action="edit.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
   
      Subject:<INPUT TYPE="subject" NAME="suject" VALUE="<?php echo $myrow["subject"] ?>" SIZE=30><br>
      Content:<TEXTAREA NAME="content" ROWS=10 COLS=30><? echo $myrow["content"] ?></TEXTAREA><br>
      Author:<INPUT TYPE="author" NAME="author" VALUE="<?php echo $myrow["author"] ?>" SIZE=30><br>
            Date:<INPUT TYPE="date" NAME="date" VALUE="<?php echo $myrow["date"] ?>" SIZE=30><br>
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
<? } ?>

<?
   if ($_POST["$submit"])
   {
      $subject = $_POST["subject"];
        $content = $_POST["content"];
        $author = $_POST["author"];
            $date = $_POST["date"];
      
      $sql = "UPDATE news SET subject='$subject',content='$content',author='$author', date='$date' WHERE id=$id";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
    }
}
?>


#2 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,664 posts
  • LocationLos Angeles, CA USA

Posted 21 April 2006 - 06:12 AM

[!--quoteo(post=367111:date=Apr 20 2006, 10:46 PM:name=HaZaRd420)--][div class=\'quotetop\']QUOTE(HaZaRd420 @ Apr 20 2006, 10:46 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Ok Heres the problem, I got to edit it, click the link, I edit the textarea, i click submit, and nothing happends. Its suppose to use the echo "Thank you! Information updated" When i click the submit button but it doesnt, it just goes back to the link to edit again. Please Help me.


<?
   if ($_POST["$submit"])
   {
      $subject = $_POST["subject"];
        $content = $_POST["content"];
        $author = $_POST["author"];
            $date = $_POST["date"];
      
      $sql = "UPDATE news SET subject='$subject',content='$content',author='$author', date='$date' WHERE id=$id";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
    }
}
?>
[/quote]

My first question is... does this work at all? And if so, is that because you have register globals turned on? The reason I ask is because you look to see if $cmd isset, but you don't assign that variable. If you have register globals on, well you shouldn't, and the use ofthe $_GET and $_POST superglobs is very confusing.

Pick an approach and stay with it. Probably this line explains part of your problem:

 if ($_POST["$submit"])


$submit isn't what you want to index $_POST by. probably you meant 'submit' as you used it previously. However, it's obvious from your code, that what you want is an if - else situation based on the value of submit, which you check at the top of the block, so you probably should just rewrite your code to go with the implicit if- else condition.

if (!isset($_POST["submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM news WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
      
      <form action="edit.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
  
      Subject:<INPUT TYPE="subject" NAME="suject" VALUE="<?php echo $myrow["subject"] ?>" SIZE=30><br>
      Content:<TEXTAREA NAME="content" ROWS=10 COLS=30><? echo $myrow["content"] ?></TEXTAREA><br>
      Author:<INPUT TYPE="author" NAME="author" VALUE="<?php echo $myrow["author"] ?>" SIZE=30><br>
            Date:<INPUT TYPE="date" NAME="date" VALUE="<?php echo $myrow["date"] ?>" SIZE=30><br>
  
      <input type="hidden" name="cmd" value="edit">
  
      <input type="submit" name="submit" value="submit">
  
      </form>
  
<? } else  {
      $subject = $_POST["subject"];
        $content = $_POST["content"];
        $author = $_POST["author"];
            $date = $_POST["date"];
      
      $sql = "UPDATE news SET subject='$subject',content='$content',author='$author', date='$date' WHERE id=$id";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
    }
}







#3 HaZaRd420

HaZaRd420
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 21 April 2006 - 06:20 AM

Hey man, that worked, but now when i go to edit it, the subject just dissapears and doesnt insert it. :(

#4 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,664 posts
  • LocationLos Angeles, CA USA

Posted 21 April 2006 - 06:26 AM

[!--quoteo(post=367114:date=Apr 20 2006, 11:20 PM:name=HaZaRd420)--][div class=\'quotetop\']QUOTE(HaZaRd420 @ Apr 20 2006, 11:20 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hey man, that worked, but now when i go to edit it, the subject just dissapears and doesnt insert it. :(
[/quote]

Yes, notice the typo you made in the name of your form element for subject. Anytime something "sorta" works, you need to check for those types of errors.

#5 HaZaRd420

HaZaRd420
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 21 April 2006 - 06:28 AM

O so sorry man. Thanks so much dude. I added your AIM if thats cool with you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users