nasirmunir
03-23-2009, 05:23 PM
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.
//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
<?
//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.
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.
//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
<?
//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.