Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    updating (changing) mysql db with html table

    I wish to use one html-page to view and change entries in a db. Here is the code I got so far, but when clicking "save", it will just display the original data without updating.

    PHP Code:
    <?php 
    include('Connections/db.php');
    $id '1';
    mysql_select_db($db);
    // Update the database if form posted
    if ($save) {
        echo 
    "updated <p>" ;
        
    $update mysql_query("UPDATE distributoren Set Firma = $txtFirma, Land = $txtLand WHERE id = $id") or die("SQL Update failed");
    }
    $query_distributoren "SELECT * FROM distributoren WHERE id = $id";

    $distributoren mysql_query($query_distributoren) or die(mysql_error());
    $row_distributoren mysql_fetch_assoc($distributoren);

    ?>

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Details</title>
    </head>

    <body>
    <form method="post" action="<?php echo $PHP_SELF ?>">
        <input type="hidden" name="txtId" value="<? echo $id?>">
      
      Firma
      <input  type="text" name="txtFirma"value="<?php echo $row_distributoren['Firma']; ?>"/>
      <br />
        Land
        <input type="text" name="txtLand" value="<?php echo $row_distributoren['Land']; ?>" />
        <br />
      
        <input type="submit" name="save" value="Save">
    </form>

    </body>
    </html>
    <?php
    mysql_free_result
    ($distributoren);
    ?>
    What's wrong? Thanks.

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    You can redirect the page to itself once you have updated the database.

    for example

    <?php
    if(isset($_POST['save'])){
    // start saving data into data base
    // and if successful, redirect page to itself to refresh page.
    header("location: " . $_SERVER['PHP_SELF']);
    exit();
    }
    ?>

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    PHP Code:
    echo "updated <p>" // after this line add:
    $txtFirma $_POST['txtFirma'];
    $txtLand $_POST['txtLand']; 
    You may want to echo() the query before you execute it too to make sure that it's properly formatted and possibly run it from phpMyAdmin.
    Quote Originally Posted by ess View Post
    You can redirect the page to itself once you have updated the database.
    There's no reason to do that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •