Display and Update record in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bakarre
    New Member
    • Jun 2007
    • 8

    Display and Update record in php

    I have problem in retrieving record from mysql using php form, I was able to retrieve the record, if I edit, the changes is not updated in mysql.
    find below my code
    [code=php]
    <html>
    <body>
    <?php
    $db = mysql_connect(" localhost", "root");
    mysql_select_db ("k1",$db);
    $id = $_POST["id"];
    echo $id;
    if ($submit) {
    $first = $_POST['first'];
    $last = $_POST['last'];
    $address = $_POST['address'];
    $position = $_POST['position'];
    $sql = "UPDATE 'employees' SET 'first'='$first ','last'='$last ','address'='$a ddress','positi on'='$position' WHERE 'id'='$id'";
    $result = mysql_query($sq l);
    echo $first;
    echo "Thank you! Information updated.\n";
    } else {
    if ($id) {
    $sql = "SELECT * FROM employees WHERE id=$id";
    $result = mysql_query($sq l);
    $myrow = mysql_fetch_arr ay($result);
    $first = $myrow['1'];
    $last = $myrow['2'];
    $address = $myrow['3'];
    $position = $myrow['4'];
    $id = $myrow['id'];
    $submit = 'submit';
    }
    }
    ?>
    <form method="post" action="<?php echo $PHP_SELF?>">
    <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
    First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>
    Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br>
    Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>
    Position:<input type="Text" name="position" value="<?php echo $position ?>"><br>
    <input type="Submit" name="submit" value="Enter information">
    </form>
    </body>
    </html>[/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    #2
    Hi Bakarre and welcome to TSDN !!

    Can you take a look at

    this excellent post by Motoma where he works through options to get debug output from PHP. I suspect from looking at your code you have not got error messages turned on.

    Purple

    Comment

    • Purple
      Recognized Expert Contributor
      • May 2007
      • 404

      #3
      And try this

      [PHP]<html>
      <body>
      <?php
      $db = mysql_connect(" localhost", "root");
      mysql_select_db ("k1",$db);
      if(isset($_POST["id"])) $id = $_POST["id"];
      //echo $id;
      if (isset($_POST['submit']))
      {
      $first = $_POST['first'];
      $last = $_POST['last'];
      $address = $_POST['address'];
      $position = $_POST['position'];
      $sql = "UPDATE 'employees' SET 'first'='$first ','last'='$last ','address'='$a ddres s','position'=' $position' WHERE 'id'='$id'";
      $result = mysql_query($sq l);
      echo $first;
      echo "Thank you! Information updated.\n";
      }
      elseif (isset($id))
      {
      $sql = "SELECT * FROM employees WHERE id=$id";
      $result = mysql_query($sq l);
      $myrow = mysql_fetch_arr ay($result);
      $first = $myrow['1'];
      $last = $myrow['2'];
      $address = $myrow['3'];
      $position = $myrow['4'];
      $id = $myrow['id'];
      $submit = 'submit';
      }
      ?>
      <form method="post" action="<?php echo $PHP_SELF?>">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
      First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>
      Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br>
      Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>
      Position:<input type="Text" name="position" value="<?php echo $position ?>"><br>
      <input type="Submit" name="submit" value="Enter information">
      </form>
      </body>
      </html>[/PHP]

      Let me know how you get on

      Purple

      Comment

      • Bakarre
        New Member
        • Jun 2007
        • 8

        #4
        Hi,
        is perfectly now working.
        You can buy experience.
        I gained alot.

        Bakare

        Comment

        • Bakarre
          New Member
          • Jun 2007
          • 8

          #5
          Hi,
          is perfectly now working.
          You cann't buy experience.
          I gained alot.

          Bakarre

          Comment

          Working...