Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 23rd, 2009, 11:17 AM
Authorized User
 
Join Date: Nov 2007
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default Editing MySQL record with PHP

Situation:
There is a page that displays information about a user in a tabular form.
I want to create an edit link across each record for a user to update information.

Question: how do I associate that "edit" link with a particular record. The record being generated from the database?

eg: a user click on users information, that gives bunch of records. Now the user wants to edit a particular record. The user clicks on that record, using some edit link, and the edit page opens up, with fields filled out from the previous page.

This is what I have done: on the user information page I am displaying information by using getUsers() function that basically joins three tables and displays user id, name, and contact. My problem is how can I associate that edit button/link to a particular row, or in other words how can I uniquely identify that row? If I could identify that then I can use $_POST variable to generate a query by passing that as a parameter and then displaying result accordingly. Problem is how to get that row by some identifier? Any help would be appreciated. Thanks.
Code:
<?
                        $users = getUsers() ;
                        while($userReport = mysql_fetch_assoc($users)){
                        //var_dump($userReport) ;
                        ?>
                        <tr>
                            <td><?=$userReport["concat(u.first_name, ' ', u.last_name)"]?></td> 
                            <td><?=$userReport['project_name']?></td>
                            <td><?=$userReport['contact']?></td>
                            <td ><input type="submit" value="Edit"/></td>
                        </tr>
                        <?
                        }
                        ?>
 
Old March 24th, 2009, 12:34 AM
Authorized User
 
Join Date: Feb 2009
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can do this in many ways

In all, you need an Identifier to recognize each User(Record)
Hope you have an ID to do this in table

So your while loop will be

while($userReport = mysql_fetch_assoc($users)){
//var_dump($userReport) ;
?>
<tr>
<td><?=$userReport["concat(u.first_name, ' ', u.last_name)"]?></td>
<td><?=$userReport['project_name']?></td>
<td><?=$userReport['contact']?></td>
<td ><a href="edit_user.php?id=<?=$userReport['id']
?>">Edit</a></td>
</tr>
<?
}


So that when you click on Edit link of a user it calls the edit_user.php with a GET variable id contains its identifier.

In another method you can pass the Id as a POST Variable.
Here we need to do this using Javascript, with the help of a hidden variable


PHP Code:
<script language="javascript">
    function edit_user(user_id) {
        document.getElementById('user_id_for_edit').value = user_id;
        document.frm_details.submit();
    }
</script>
    <form name="frm_details" action="edit_user.php" method="post">
    <input type="hidden" name="user_id_for_edit" id="user_id_for_edit" value="" />
    <?
    $users 
getUsers() ;
    while(
$userReport mysql_fetch_assoc($users)){
    
//var_dump($userReport) ;
    
?>
    <tr>
        <td><?=$userReport["concat(u.first_name, ' ', u.last_name)"]?></td> 
        <td><?=$userReport['project_name']?></td>
        <td><?=$userReport['contact']?></td>
        <td><a href="javascript:edit_user('<?=$userReport['id']?>')">Edit</a></td>
    </tr>
    <?
    
}
    
?>
    </form>
(Code not Tested!)

Regards
Anees





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combo boxes in record editing form srcLakeJake Access 0 January 23rd, 2008 11:14 PM
how to record an Audio file using PHP & MYSQL pksingh PHP How-To 0 September 8th, 2007 01:21 PM
Editing SINGLE record in ASP.NET Koofmich ADO.NET 5 December 30th, 2004 05:42 AM
Creating or Editing a record from a form Brian263 Access 3 October 18th, 2004 11:42 PM
Editing MySQL data from a PHP form cmiller Beginning PHP 2 September 2nd, 2003 05:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.