Jump to content

Editing a row in mysql using php


mikew2

Recommended Posts

Hi guys,

Im trying to edit a row in mysql using php. This is my code:

[quote]<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","admin");

//select which database you want to edit
mysql_select_db("ict");

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the news
  $result = mysql_query("select * from contact 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
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
   
//make the title a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$title - Edit</a>";
      echo "<br>";
    }
}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["id"];
      $sql = "SELECT * FROM contact 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"] ?>">
 
      Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br>
      Content:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["content"] ?></TEXTAREA><br>
      Date:<INPUT TYPE="TEXT" NAME="who" 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"])
  {
      $title = $_POST["title"];
      $content = $_POST["content"];
      $date = $_POST["date"];

      $sql = "UPDATE contact SET title='$title',content='$content',date='$date' WHERE id=$id";

      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
  }
}
?>
[/quote]

It works fine until i hit submit and then it just goes back to the edit.php page. And doesnt update the row. It also doesnt display the "Thank you! Information updated." However there are no mysql errors.

Any help would be much appreciated
Thanks
Mike
Link to comment
https://forums.phpfreaks.com/topic/16187-editing-a-row-in-mysql-using-php/
Share on other sites

What is your primary key? You need to update it as well. I am assuming it is "id", if it is, this should work:

[code]
<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","admin");
 
//select which database you want to edit
mysql_select_db("ict");

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the news
  $result = mysql_query("select * from contact 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
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
   
    //make the title a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$title - Edit[/url]";
      echo "
";
    }
}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["id"];
      $sql = "SELECT * FROM contact 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"] ?>">
 
      Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30>

      Content:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["content"] ?></TEXTAREA>

      Date:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["date"] ?>" SIZE=30>

 
      <input type="hidden" name="cmd" value="edit">
      <input type="hidden" name="id" value="<?php echo $id; ?>">
 
      <input type="submit" name="submit" value="submit">
 
      </form>
 
  <? } ?>

<?
  if ($_POST["$submit"])
  {
      $title = $_POST["title"];
      $content = $_POST["content"];
      $date = $_POST["date"];
      $id = $_POST["id"];

      $sql = "UPDATE contact SET title='$title',content='$content',date='$date',id='$id' WHERE id=$id";

      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
  }
}
?>
[/code]
lol i just realised the answer rofl. Im kicking myself. The name of my date input element was called "who" and not "date" OMFOGMGOMFOI MFOM OM

ROFL OROFL LOL :p

thanks for the help guys
mike

[b]EDITED BY WILDTEEN88: REMOVED LANGUAGE. PLEASE DO NOT SWEAR IN POSTS. THERE ARE MINORS THAT BROWSE AROUND THIS FORUM[/B]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.