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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Users updating their details (PHP form/script, MySQL database)

    Hi All,

    I've had a go at modifying a script so my users can update a field, submit it and their update replaces the information currently stored on their details held on my DB. The problem i have is i cannot get or set the users id as they login, so that it updates only their information.

    What i have so far, i have a page called dispatched.php, with the following code:

    Code:
    <?php
    
    //make connection to dbase
    $connection = @mysql_connect($server, $dbusername, $dbpassword)
    			or die(mysql_error());
    			
    $db = @mysql_select_db($db_name,$connection)
    			or die(mysql_error());
    			
    // get value of id that sent from address bar
    $id=$_GET['id'];
    
    //build and issue the query
    $sql ="SELECT * FROM $table_name";
    $result = @mysql_query($sql,$connection) or die(mysql_error());
    
    ?>
    
    
    <!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>Untitled Document</title>
    <style type="text/css">
    .header {	color: #D2A77B;
    }
    </style>
    </head>
    
    <body>
    <p>&nbsp;</p>
    <p><span class="header"><?php echo $_SESSION[user_name]; ?></span></p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <table width="667" border="1">
      <tr>
        <td width="72">Index</td>
        <td width="80">Flight Number</td>
        <td width="97">Departure ICAO</td>
        <td width="75">Arrival ICAO</td>
        <td width="50">Type</td>
        <td width="161">Comments</td>
        <td width="86">Complete?</td>
        <td width="86">Approved</td>
      </tr>
      <tr>
        <td>1.</td>
        <td><span class="header"><?php echo $_SESSION[flt_no1]; ?></span></td>
        <td><span class="header"><?php echo $_SESSION[dep_ICAO1]; ?></span></td>
        <td><span class="header"><?php echo $_SESSION[arr_ICAO1]; ?></span></td>
        <td><span class="header"><?php echo $_SESSION[type1]; ?></span></td>
        <td><span class="header"><?php echo $_SESSION[comments1]; ?></span></td>
        <td><label for="complete_1"></label>
        <input name="complete1" type="text" id="complete1" value="<? echo $_SESSION[complete1]; ?>" size="15" /></td>
        <td><span class="header"><?php echo $_SESSION[approved1]; ?></span></td>
      </tr>
      <tr>
        <td>2.</td>
        <td>&nbsp;</td>
        <td><span class="header"><?php echo $_SESSION[dep_ICAO2]; ?></span></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><label for="checkbox2"></label></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>3.</td>
        <td>&nbsp;</td>
        <td><span class="header"><?php echo $_SESSION[dep_ICAO3]; ?></span></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><label for="checkbox3"></label></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>4.</td>
        <td>&nbsp;</td>
        <td><span class="header"><?php echo $_SESSION[dep_ICAO4]; ?></span></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>5.</td>
        <td>&nbsp;</td>
        <td><span class="header"><?php echo $_SESSION[dep_ICAO5]; ?></span></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>6.</td>
        <td>&nbsp;</td>
        <td><span class="header"><?php echo $_SESSION[dep_ICAO6]; ?></span></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <form name="form1" method="post" action="dispatch_update.php">
      <input type="submit" name="Submit" id="Submit" value="Submit" />
    </form>
    <p>&nbsp;</p>
    </body>
    </html>
    So what's happening here is the pilot can see many fields that are populated from info currently held on the database. The field i wish them to be able to change is complete1, so they can type Yes/No if its been completed. Once they type this in the complete box and hit submit, the form is checked/processed by the next page dispatch_update.php.

    dispatch_update.php

    [code]

    <?php
    $host="localhost"; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name=""; // Database name
    $tbl_name=""; // Table name

    /* obtain the form data */

    $id = $_POST['id'];
    $user_name = $_POST['username'];
    $complete1 = $_POST['complete1'];

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    // update data in mysql database
    $sql="UPDATE $tbl_name SET complete1='$complete1' WHERE id = '$id'";
    $result=mysql_query($sql);

    // if successfully updated.
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='list_records.php'>View result</a>";
    }

    else {
    echo "ERROR";
    }

    ?>

    [code]

    So what is mean't to happen here is the database grabs the user id, from when they logged in and it posts the field altered by the pilot (yes or no), so when they go back to the dispatched.php page it will now show in the complete field yes/no. What actually happens is if i hit submit, it tell me the change has been successfull, but doesn't actually update the info held on the database for that particular user.

    I hope you can help me with this

    Kind regards

    Josh

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,575
    Thanks
    23
    Thanked 643 Times in 642 Posts
    from first code given:
    <td><span class="header"><?php echo $_SESSION[dep_ICAO6]; ?></span></td>

    You never start sessions and you never tell us if the calling code stored the session and your using a lot of them.

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply! Sorry could you clarify what you mean, i'm a little unusure.

    Regards

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,575
    Thanks
    23
    Thanked 643 Times in 642 Posts
    OK. I made two statements

    You never start sessions
    The first line in a php script should be
    <?php
    session_start();

    You need this to use any session variable

    and you do use them
    you never tell us if the calling code stored the session

  • #5
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Not sure what happened when i copied the code over, but this was missing from the first files code
    Code:
    ?php
    
    //prevents caching
    header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Cache-Control: post-check=0, pre-check=0",false);
    session_cache_limiter();
    session_start();
    Does the session start have to be on my second page (the form processing one).

    Thanks

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,575
    Thanks
    23
    Thanked 643 Times in 642 Posts
    session_start(); has to be the first line of code and on every page.


  •  

    Posting Permissions

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