Edit Form

Discussion in 'PHP' started by johneva, Feb 12, 2010.

  1. #1
    Hi

    Could someone please take a look over this for me and let me know where I am going wrong please.

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <link href="cp_css/loginmodule.css" rel="stylesheet" type="text/css" />
    </head>
      
      <body>
       <h1>Edit Form</h1>
       <p><a href="http://localhost/dual_control_cars/control_panel">Control Panel</a></p>
    
    <ul>
    <?php 
    //connect to mysql
    //change user and password to your mySQL name and password
    mysql_connect("localhost", "johnmev_user1", "Pass1");
    	
    //select which database you want to edit
    mysql_select_db("cp"); 
    
    //If cmd has not been initialized
    if(!isset($cmd)) 
    {
       //display all the cars
       $result = mysql_query("select * from data order by id"); 
       
       //run the while loop that grabs all the cars
       while($r=mysql_fetch_array($result)) 
       { 
          //grab the model and the ID
          $make=$r["make"];//take out the model     
          $model=$r["model"];//take out the model
          $id=$r["id"];//take out the id
         
    	 //show the make and model a link in a list
          echo "<li>";
          echo "<a href='edit.php?cmd=edit&id=$id'>Edit - $make $model</a>";
          echo "</li>";
        }
    }
    ?>
       </ul>
    
    <?php
    if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
    {
       if (!isset($_POST["submit"]))
       {
          $id = $_GET["id"];
          $sql = "SELECT * FROM data 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"] ?>">
       
          Make:<input type="text" name="make" value="<?php echo $myrow["make"] ?>" size=30 /><br />
          Model:<input type="text" name="model" value="<?php echo $myrow["model"] ?>" size=30 /><br />
          Spec:<input type="text" NAME="spec" value="<?php echo $myrow["spec"] ?>" size=30 /><br />
       
          <input type="hidden" name="cmd" value="edit" />
       
          <input type="submit" name="submit" value="submit" />
       
          </form>
    
         <?php } ?>
       
       <?php
       if ($_POST["$submit"])
       {
          $make = $_POST["make"];
          $model = $_POST["model"];
          $spec = $_POST["spec"];
    
          $sql = "UPDATE data SET make='$make',model='$model',spec='$spec' WHERE id=$id";
    
          $result = mysql_query($sql);
          echo "Thank you! Information updated.";
       }
    }
    ?>
    
    </body>
    </html>
    PHP:

     
    johneva, Feb 12, 2010 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    if ($_POST["$submit"])

    change to if($_POST['submit'])
     
    php-lover, Feb 12, 2010 IP
    johneva likes this.
  3. bugcoder

    bugcoder Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    whats the error?
     
    bugcoder, Feb 12, 2010 IP
  4. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #4
    Cheers thats works now :)
     
    johneva, Feb 12, 2010 IP
  5. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #5
    Bonus points for why its only updating the last vehicle/id in the list of vehicles even when I go in to edit the first or second in the list.
     
    johneva, Feb 12, 2010 IP
  6. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    $make = $_POST["make"];
    $model = $_POST["model"];
    $spec = $_POST["spec"];
     $id = $_POST['id'];       //<------ add this line
    
    
     $sql = "UPDATE data SET make='$make',model='$model',spec='$spec' WHERE id=$id";
    
    PHP:
     
    php-lover, Feb 12, 2010 IP
  7. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #7
    Ah top man yeah of course.

    Cheers mate
     
    johneva, Feb 12, 2010 IP