Results 1 to 2 of 2

Thread: Editing MySQL records with PHP

  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Editing MySQL records 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.php
    <form name="user" method="post" action="editUser.php" >
    			<table>
    				<h4>Users:</h4><br />
    					<table>
    						<tr>
    							<th>Name</th>
    							<th>Project</th>
    							<th>Contact</th>
    							<th><a href="addUser.php">Add</a></th>
    						</tr>
    						<?
    						$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 name="contact"><?=$userReport['contact']?></td>
    							<td><input type="submit" value="Edit"/></td>
    						</tr>
    						<?
    						}
    						?>
    					</table>
    			</table>
    		</form>
    and editUser.php
    Code:
    <?
    //Connect to database
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '1234';
    
    //making connection
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
    //echo "Connection to the server is successful!<br/>";
    
    //select database
    mysql_select_db("time_tracker") or die(mysql_error());
    //echo "Database is selected!<br/>";
    
    // Get the user id
    $id = $_POST[table row unique identifier];
    
    // Get data from user with the specified id
    $info = mysql_query("SELECT * FROM users WHERE uid = $id") or die ('Error Getting User Data! <br />' .mysql_error());
    $chk = mysql_num_rows($info);
    $u = mysql_fetch_array($info);
    
    ?>
    
    <form method="post" action="">
    	<table width="250">
    		<tr>
    			<td>netID:</td>
    			<td><input type="text" name="username" value="<?php echo $u['username'] ?>" /></td>
    		</tr>
    		<tr>
    			<td>First Name:</td>
    			<td><input type="text" name="firstName" value="<?php echo $u['first_name'] ?>" /></td>
    		</tr>
    		<tr>
    			<td>Last Name:</td>
    			<td><input type="text" name="lastname" value="<?php echo $u['last_name'] ?>" /></td>
    		</tr>
    		<tr>
    			<td>Contact:</td>
    			<td><input type="text" name="lastname" value="<?php echo $u['contact'] ?>" /></td>
    		</tr>
    		
    		<tr>
    			<td colspan="2"><input type="submit" name="edit" value="Edit" /></td>
    		</tr>
    	</table>
    </form>
    Once this is done, then I can insert/update the values back to the table.

  2. #2
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    As long as there is an IDX or id or something like that field in your mysql table, use this in your form, and then check for $_POST['rec_id'] in your php processing.

    Code:
    <input type="hidden" name="rec_id" value="<?php echo $u['IDX']; ?>" />

Bookmarks

Posting Permissions

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