Hello there, I'm having a problem on this one. I want to update my records, but when I click save, the specific fields/record is null. instead of updating/edit.

These is my PHP code for admin_view.php (This is the form where you can choose a specific record/field that you are going to update.)

<form id="form1" name="form1" method="post" action="">
      <?php
			include("connect-db.php");
			
				
			$result=mysql_query("SELECT * FROM emprofile");
		
			echo "<table border='5' cellpadding='10' align='center'>";
        	echo "<tr> 
			<th>Month</th> 
			<th>Employee No</th> 
			<th> Employee Name</th>
			</tr>";
			
			while($test = mysql_fetch_array($result))
			{
				$id = $test['number'];	
				echo "<tr align='center'>";	
				echo"<td><font color='black'>" .$test['Month']."</font></td>";
				echo"<td><font color='black'>" .$test['Employeeno']."</font></td>";
				echo"<td><font color='black'>". $test['Employeename']. "</font></td>";
				echo"<td> <a href ='admin_edit.php?number=$id'>Edit</a>";
				echo "</tr>";
			}
			mysql_close($connection);
			?>
    </form>

This is my PHP code in admin_edit.php (This is the form where the admin can update/edit a record)

<?php
require("connect-db.php");
$id =$_REQUEST['number'];

$result = mysql_query("SELECT * FROM emprofile WHERE number = '$id'");
$test = mysql_fetch_array($result);
if (!$result) 
		{
		die("Error: Data not found..");
		}
				$month=$test['Month'] ;
				$empno= $test['Employeeno'] ;					
				$empname=$test['Employeename'] ;
				$salary=$test['Salary'] ;
				$allottee=$test['Allottee'] ;
				$relation= $test['Relation'] ;					
				$sss1=$test['sss'] ;
				$ph1=$test['ph'] ;
				$pagibig1=$test['pagibig'] ;
				$tax1= $test['tax'] ;					
				$total1=$test['total'] ;
				$sss2=$test['sss2'] ;
				$ph2=$test['ph2'] ;
				$pagibig2=$test['pagibig2'] ;
				$tax2= $test['tax2'] ;					
				$total2=$test['total2'] ;
				$tded=$test['totaldeduction'] ;
				$tsal= $test['totalsalary'] ;					
				$allotment=$test['allotment'] ;
				

if(isset($_POST['save']))
{	
	$month_save = $_POST['Month'];
	$empno_save = $_POST['Employeeno'];
	$empname_save = $_POST['Employeename'];
	$salary_save = $_POST['Salary'];
	$allottee_save = $_POST['Allottee'];
	$relation_save = $_POST['Relation'];
	$sss1_save = $_POST['sss'];
	$ph1_save = $_POST['ph'];
	$pagibig1_save = $_POST['pagibig'];
	$tax1_save = $_POST['tax'];
	$total1_save = $_POST['total'];
	$sss2_save = $_POST['sss2'];
	$ph2_save = $_POST['ph2'];
	$pagibig2_save = $_POST['pagibig2'];
	$tax2_save = $_POST['tax2'];
	$total2_save = $_POST['total2'];
	$tded_save = $_POST['totaldeduction'];
	$tsal_save = $_POST['totaldeduction'];
	$allotment_save = $_POST['totaldeduction'];

	mysql_query("UPDATE emprofile SET Month='$month_save', Employeename='$empname_save', Salary='$salary_save', Allottee='$allottee_save', Relation='$relation_save', sss='$sss1_save', ph='$ph1_save', pagibig='$pagibig1_save', tax='$tax1_save', total='$total1_save', sss2='$sss2_save', ph2='$ph2_save', pagibig2='$pagibig2_save', tax2='$tax2_save', total2='$total2_save', totaldeduction='$tded', totalsalary='$tsal', allotment='$allotment' WHERE number = '$id'")or die(mysql_error()); 
	echo "Saved!";
	
	header("Location: admin_view.php");			
}
mysql_close($connection);
?>

Anyone can help me? Thank you

Dani AI

Generated

Likely causes and quick diagnosis
The problem usually isn’t MySQL magic — it’s the edit page not actually receiving or using posted values. As observed, admin_edit.php must present a real HTML form (method="post") with inputs named exactly as the code expects (and a submit input named save). In this thread several _save variables are assigned from the wrong POST keys and the UPDATE uses the original fetched variables instead of the _save versions, so nothing new gets written. Also note that column names like Month can collide with SQL functions; use backticks or rename the column.

Concrete fixes (ordered)

  • Add an edit form with inputs (and a hidden number/id) so values are actually posted. Include <input type="submit" name="save">.
  • Match each input name to the exact $_POST key used. Confirm with var_dump($_POST) or browser “view source.”
  • Fix assignment mistakes (e.g., totalsalary/allotment must come from their own $_POST keys, not copied from totaldeduction) and use the _save variables in the UPDATE.
  • Avoid echoing before a header("Location: ...") redirect (or call exit immediately after).
  • Wrap column names in backticks if they are keywords and check for NULL/empty inputs before updating.

Minimal, safer pattern (illustrative)

// show form (values prefilled from DB)
<form method="post" action="">
  <input type="hidden" name="id" value="<?php echo htmlspecialchars($id); ?>">
  <input name="Month" value="<?php echo htmlspecialchars($month); ?>">
  <input name="Employeename" value="<?php echo htmlspecialchars($empname); ?>">
  <input name="Salary" value="<?php echo htmlspecialchars($salary); ?>">
  <input type="submit" name="save" value="Save">
</form>

// on POST, use prepared statements (mysqli/PDO) to update
$stmt = $mysqli->prepare("UPDATE `emprofile` SET `Month`=?, `Employeename`=?, `Salary`=? WHERE `number`=?");
$stmt->bind_param('ssdi', $_POST['Month'], $_POST['Employeename'], $_POST['Salary'], $_POST['id']);
$stmt->execute();

Additional notes
Enable full error reporting during debugging (error_reporting(E_ALL); ini_set('display_errors',1);) and check SQL errors ($mysqli->error or mysql_error()). Migrate off deprecated mysql_* functions to mysqli or PDO and use prepared statements to prevent SQL injection. Mentioned points from and about quoting numeric IDs are fine: numbers can be unquoted, but parameterized queries remove the ambiguity and are future-proof.

Recommended Answers

All 3 Replies

your admin_edit.php is confusing. How can you edit the values, if you dont have them posted in a form values? You just have, click edit, then variables to straight to the database with no option of editing? Second you have

if(isset($_POST['save'])) {

again, no in a form, so how can you post using the save button?

$result = mysql_query("SELECT * FROM emprofile WHERE number = '$id'");

to

$result = mysql_query("SELECT * FROM emprofile WHERE number = '" . $id . "'");

Also is number an int or string, if int remove single quotes.

Also is number an int or string, if int remove single quotes.

Works with and without for an int, and there is no need for splitting the string like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.