HELP WITH PHP FORM / MYSQL UPDATE SCRIPT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MEECLOUD
Forum Newbie
Posts: 2
Joined: Wed Mar 24, 2010 8:27 am

HELP WITH PHP FORM / MYSQL UPDATE SCRIPT

Post by MEECLOUD »

Hi,

I found the following script online at http://www.spoono.com/php/tutorials/tutorial.php?id=23 . The script runs ok with no errors, but does not update the db.

I've tried breaking it into three separate php files, and am then successful in getting this to work; however, I'd prefer to have this all contained into one file like the tutorial.
File 1. Get all "news" from db with link to edit.
File 2. Get row to edit in a form.
File 3. MYSQL UPDATE.

I am assuming the problem is that for whatever reason the last part of the script (the MYSQL UPDATE) is not running. Any suggestions?

Code: Select all

 
<?php 
//connect to mysql
include("db.php");
 
//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
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
     
     //make the title a link
      echo "<a href='edit-mm.php?cmd=edit&id=$id'>$title - Edit</a>";
      echo "<br>";
    }
}
?>
<?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-mm.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>
      Message:<TEXTAREA NAME="message" ROWS=10 COLS=30><?php echo $myrow["message"] ?></TEXTAREA><br>
      Who:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["who"] ?>" SIZE=30><br>
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
<?php } ?>
 
<?php
   if ($_POST["$submit"])
   {
      $title = $_POST["title"];
      $message = $_POST["message"];
      $who = $_POST["who"];
      
      $sql = "UPDATE news SET 
        title='$title',
        message='$message',
        who='$who' 
        WHERE id='$id' ";
        
      //replace news with your table name above
      $result = mysql_query($sql);
        
      echo "Thank you! Information updated.";
    }
 
}
?>
 
 
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: HELP WITH PHP FORM / MYSQL UPDATE SCRIPT

Post by ell0bo »

change $_POST['$submit'] to $_POST['submit']
MEECLOUD
Forum Newbie
Posts: 2
Joined: Wed Mar 24, 2010 8:27 am

Re: HELP WITH PHP FORM / MYSQL UPDATE SCRIPT

Post by MEECLOUD »

ell0bo wrote:change $_POST['$submit'] to $_POST['submit']
Ahah! Thanks, that did the trick.
Post Reply