Jump to content

Edit MYSQL row Problem!


HaZaRd420

Recommended Posts

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.

[code]
<?
//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.";
    }
}
?>
[/code]
Link to comment
Share on other sites

[!--quoteo(post=367111:date=Apr 20 2006, 10:46 PM:name=HaZaRd420)--][div class=\'quotetop\']QUOTE(HaZaRd420 @ Apr 20 2006, 10:46 PM) [snapback]367111[/snapback][/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.

[code]

<?
   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.";
    }
}
?>
[/code]
[/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:

[code]
if ($_POST["$submit"])
[/code]


$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.

[code]
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.";
    }
}
[/code]





Link to comment
Share on other sites

[!--quoteo(post=367114:date=Apr 20 2006, 11:20 PM:name=HaZaRd420)--][div class=\'quotetop\']QUOTE(HaZaRd420 @ Apr 20 2006, 11:20 PM) [snapback]367114[/snapback][/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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.