PHP - Editing Row, Little Problem

Associate
Joined
29 May 2005
Posts
144
hello.

im trying to create a page which will allow me to edit my already existing news. The code appears to work as It should, but it only editing the last ID (last news post) Its not working on the others. I have done the following code: can anyone spot where i have gone wrong?

website example: try editing the 1st post:here

thanks again, mofish

Code:
<? 
//connect to mysql
//done!
	
//select database
mysql_select_db("mo_mofish"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from comtbl order by postID"); 
   
   //run the while loop that grabs all the new
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the news
      $posttitle=$r["postTITLE"]; //take out the title
      $id=$r["postID"]; //take out the id
     
	 //make the title a link
      echo "<a href='edit.php?cmd=edit&postID=$id'>$posttitle - Edit</a>";
      echo "<br>";
    }
}
?>
<?
	//if you click the link above, it will set cmd to edit, so check if its been initialized
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
	//if submit is set, and edit has been pressed then
   if (!isset($_POST["submit"]))
   {
      $id = $_GET["postID"];
      $sql = "SELECT * FROM comtbl WHERE postID=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
	  
      <form action="edit.php" method="post">
      <input type=hidden name="postID" value="<?php echo $myrow["postID"] ?>"><br>
      Title:<br>
	  <INPUT TYPE="TEXT" NAME="postTITLE" VALUE="<?php echo $myrow["postTITLE"] ?>" SIZE=30><br>
      postTXT:<br>
	  <TEXTAREA NAME="postTXT"><? echo $myrow["postTXT"] ?></TEXTAREA><br>
      posterNAME:<br>
	  <INPUT TYPE="TEXT" NAME="posterNAME" VALUE="<?php echo $myrow["posterNAME"] ?>" SIZE=30><br>
      <input type="hidden" name="cmd" value="edit">
      <input type="submit" name="submit" value="submit">
      </form>
   
<? } ?>
<?
   if ($_POST["submit"])
    //if (isset($_POST["submit"]))
   {
      $posttitle = $_POST["postTITLE"];
	  $posttxt = $_POST["postTXT"];
	  $poster = $_POST["posterNAME"];
	  
	  $sql = "UPDATE comtbl SET postTITLE='$posttitle',postTXT='$posttxt',posterNAME='$poster' WHERE postID=$id";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
	}
}
?>
 
Back
Top Bottom