views:

561

answers:

3

I want to use a hyperlink to load details into an updatable form page.

I have 2 php pages. One returning the id of the last 10 records of a MYSQL query and another returning all field values for a specific record into a form, giving the end user the opportunity to update the field values. Can anyone help me link the two so that when I click on say row 3 (id = 3) of the table in the first page it takes me to the second page using the id 3 in the MYSQL query utilised by the second page, to prepopulate the form fields.

i.e. MYSQL table 'members' with 'id', 'firstname', 'surname', 'dob', and 'address'

Page 1 returns last 10 results of 'select id from members' & the id values are hyperlinks Page 2 returns results of 'select id, firstname, surname, dob, address from members where id = 3 when user selects the id 3 hyperlink on page 1, and promotes the respective values to form fields 'id_ff', 'firstname_ff', 'surname_ff', 'dob_ff', and 'address_ff'

Don't know how to promote the id '3' values to the page 2 form fields?

+1  A: 
you can do:

    // list.php

    $query = sprintf("SELECT * FROM my_table");

    $result = mysql_query($query);

    while($c = mysql_fetch_array($result)) { 
        echo '<a href="/edit.php?id=' . $c['id'] . '">Edit ' . $c['id'] . '</a><br />';
    }



<?
                // edit.php
    if(isset($_POST['apply'])) {

        $query = sprintf("UPDATE my_table SET somefield = '%s', somefield2 = '%s', somefield3 = '%s' WHERE id = %s", 
        mysql_real_escape($_POST['somefield']),
        mysql_real_escape($_POST['somefield2']),
        mysql_real_escape($_POST['somefield3']),
        mysql_real_escape($_POST['id'])

        );

        $r = mysql_query($query);

        if (!$r) die(mysql_error());

        header("Location: /list.php");

    }


        $id = $_GET['id']; // just an example.. you should prevent injections here
        $query = sprintf("SELECT * FROM my_table WHERE id = %s", $id);

        $result = mysql_query($query);

        $details = mysql_fetch_array($result);


    ?>
    <form method="post" action="">

    <input type="text" name="somefiled" value="<?= $details['somefiled']"/>
    <input type="text" name="somefiled2" value="<?= $details['somefiled2']"/>
    <input type="text" name="somefiled3" value="<?= $details['somefiled3']"/>


    <input type="hidden" name="id" value="<?= $details['id']"/>
    <input type="hidden" name= "apply" value="yes"/>


    <input type="submit" value="Submit"/>
    </form>
Gabriel Sosa
why the downvote? This seems to be OK to me...
Martijn
you've got malformed html (quotes in href attribute)
Alexander Gyoshev
the downvote was before the edit of the answer, I removed it
Alexander Gyoshev
come on, that is just an example :P.. anyway I've just addded some html to made the changes
Gabriel Sosa
A: 

Sure.

# Do sql query and drop it into $members
for ($members AS $member)
{
echo '<a href="/page2.php?id='.$member['id'].'">Member '.$member['id'].'</a>';
}

and have on your 2nd page:

$_GET['id'] = whatever_you_use_to_sanitise($_GET['id']);
#do sql query with new id

Remember, don't just copy and paste. Think for yourself and LEARN what we did. Look at http://www.w3schools.com/php/default.asp and go through the basics.

Good luck!

Dorjan
Thanks for that. Will do.
If the answer helped you Alex, don't forget a + vote :) (i'm new here and need all the rep I can get :D )
Dorjan
A: