简体   繁体   中英

How to make PHP Update MySQL Table Form display all Rows of Data

I am trying to make this PHP Update Form display all of my row data from the mysql table. While it does work and update my table, I would like it to display each employee rather than having to search up an employee number in order to edit. Basically I would like it to show an array of employees with edit inputs for each, instead of having to search for each one. I am asking this because I have searched vastly across the web and have found no solutions to my appeal.

Any help will be greatly appreciated.

Thank you

PHP Code:

<html>
<head>
<title>Update a Record in MySQL Database</title>
</head>
<body>

<?php
if(isset($_POST['update']))
{
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$emp_id = $_POST['emp_id'];
$emp_salary = $_POST['emp_salary'];

$sql = "UPDATE employee ".
       "SET emp_salary = $emp_salary ".
       "WHERE emp_id = $emp_id" ;

mysql_select_db('');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Employee ID</td>
<td><input name="emp_id" type="text" id="emp_id"></td>
</tr>
<tr>
<td width="100">Employee Salary</td>
<td><input name="emp_salary" type="text" id="emp_salary"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

Try to add your sql under form to udpate table. make var_dump to see if you receive values of employer id ad salary.

Do you want a drop-down menu displaying each employee and then allow the user to enter a new salary for that employee? For that, you could do something like this:

echo "<select name='employee'>";
$query = mysql_query("SELECT * FROM employees");
while($row = mysql_fetch_assoc($query)) {
 $id = $row['emp_id'];
 echo "<option value='$id'>$id</option>";
}
echo "<input type='text' name='salary'>
echo "</select>";

Forgive me if I misunderstood.

So you want to display the employee's current salary along with the form asking for the user to input a new one? If so, try something like this:

$employee = $_POST['employee']; // Or however you're retrieving the current employee the user is editing
$query = mysql_query("SELECT * FROM employees WHERE username='$employee'");

echo "<form action='FILENAME.PHP' method='post'>";
while($row = mysql_fetch_assoc($query)) {
    $salary = $row['emp_salary'];
    echo "Current salary: $salary <br />";
}

echo "<input type='text' name='salary'>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM