Jump to content

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

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.