Jump to content

Editing a row in a table


at0mic

Recommended Posts

I'm having trouble editing a row. The existing data echoed into the form ok but if I try to change anything and click submit, the error message at the end of the script is echoed. Can anybody help?

 

<?

include ('inc/config.php');

?>


<?
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"] ?>">

      name:<INPUT TYPE="TEXT" NAME="name" VALUE="<?php echo $myrow["name"] ?>" SIZE=30><br>
      start_date:<TEXTAREA NAME="start_date" ROWS=10 COLS=30><? echo $myrow["start_date"] ?></TEXTAREA><br>
      headline:<INPUT TYPE="TEXT" NAME="headline" VALUE="<?php echo $myrow["headline"] ?>" SIZE=30><br>
      article:<INPUT TYPE="TEXT" NAME="article" VALUE="<?php echo $myrow["article"] ?>" SIZE=30><br>
      <input type="hidden" name="cmd" value="edit">

      <input type="submit" name="submit" value="submit">

      </form>

   <? }?>


<?
   if ($_POST["$submit"])
   {
      $name = $_POST["name"];
      $start_date = $_POST["start_date"];
      $headline = $_POST["headline"];
      $article = $_POST["article"];
      
      $sql = "UPDATE news SET name='$name',start_date='$start_date',headline='$headline',article='$article' WHERE id=$id";

      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
   }
   else
   {echo 'error';}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/48439-editing-a-row-in-a-table/
Share on other sites

  • 1 month later...

Hi please can anyone help me? if I click submit I don't get any error, however, the row isn't updated.

 

If I turn on error reporting, I get:

 

Notice: Undefined index: submit in H:\xampplite\htdocs\pages\news\editNews.php on line 48

 

When I make my changes and click submit, I get:

 

Notice: Undefined index: cmd in H:\xampplite\htdocs\pages\news\editNews.php on line 21

Notice: Undefined index: id in H:\xampplite\htdocs\pages\news\editNews.php on line 62

 

I assume its to do with register_globals being turned off by default in later versions of PHP but I dont know how to solve it. Here's the code I used:

 

<?
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="editNews.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">

      Name:<INPUT TYPE="TEXT" NAME="name"
      VALUE="<?php echo $myrow["name"] ?>" SIZE=30><br>
      Start_date:<INPUT TYPE="TEXT" NAME="start_date"
      VALUE="<?php echo $myrow["start_date"] ?>" SIZE=30><br>
      Headline:<INPUT TYPE="TEXT" NAME="headline"
      VALUE="<?php echo $myrow["headline"] ?>" SIZE=30><br>
      Article:<TEXTAREA NAME="article" ROWS=10 COLS=30>
      <? echo $myrow["article"] ?></TEXTAREA><br>
      <input type="hidden" name="cmd" value="edit">

      <input type="submit" name="submit" value="submit">

      </form>

   <? }?>

<?
   if ($_POST["submit"])
   {
      $name = $_POST["name"];
      $start_date = $_POST["start_date"];
      $headline = $_POST["headline"];
      $article = $_POST["article"];

$sql = "UPDATE news
            SET
                name = '".$_POST['name']."',
                start_date = '".$start_date."',
                headline = '".$headline."',
                article = '".$article."'
            WHERE
                id = '".$_GET['id']."'
        ";

$result = mysql_query($sql)
or die('Could not edit row '.mysql_error());

   }
}
?> 

 

I hope you can help

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.