I wrote this code in php, it's purposes is to update the user record in mySQL database. There's already a script view.php which displays all records in the database, in the view file there's a edit link which sends the user id information to edit.php via get method. on the edit file there's a confirmation button to whether delete or not after i click on the edit button, the information is sent via post method to edit1.php to process the data.

When I click on the edit confirm button I am transferred to the edit form, when I click on the edit record submit button, nothing happens and no record is updated. Please help me with this.

Here's my edit.php file

<?php
include("header.html");
include("nav.html");
include("sidebars.html");
?>
<div id="content">
<?php
if(isset($_GET['id'])&&is_numeric($_GET['id']))
{
	$id=$_GET['id'];
}
  elseif(isset($_POST['id'])&&is_numeric($_POST['id']))
  {
	  $id=$_POST['id'];
  }
     else
	 {
		 echo "<p>This page has been accessed in error</p>";
		 echo "</div>";
		 include("footer.html");
		 exit();
	 }
require_once("connect.php");
$q="SELECT first_name, last_name, email FROM users WHERE user_id=$id";
$r=mysql_query($q,$dbc);
$num=mysql_num_rows($r);
echo mysql_error($dbc);
if($num==1)
{
	$row=mysql_fetch_array($r,MYSQL_NUM);
	echo '<h2>Edit Details</h2><hr /><br />'.
	'First Name on Record : '.$row[0]. '<br /><br />'.
	'Last Name on Record : '.$row[1].'<br /><br />'.
	'Email Address on Record : '.$row[2].'<br /><br />';
	echo '<form action="edit1.php" name="edconfirm" method="post">'.
	'<input type="submit" name="submit" value="Click if you want to edit this record" />'.
	'<input type="hidden" name="id" value="'.$id.'" />'.
	'</form>';
}
?>
</div>
<?php
include("footer.html");
?>

Here's my edit1.php file

<?php
include("header.html");
include("nav.html");
include("sidebars.html");
?>
<div id="content">
<?php
$errors=array();
if(isset($_POST['id'])&&is_numeric($_POST['id']))
{
	$id=$_POST['id'];
}
   else
   {
	   echo "<p>This page has been accessed in error</p></div>";
	   include("footer.html");
	   exit();
   }
if(isset($_POST['submitted']))
{
	if(empty($_POST['fname']))
	{
		$errors[]="You forgot to enter your first name";
	}
	   else
	   {
		   $fn=$_POST['fname'];
	   }
	if(empty($_POST['lname']))
	{
		$errors[]="You forgot to enter your last name";
	}
	   else
	   {
		   $ln=$_POST['lname'];
	   }
	if(empty($_POST['email']))
	{
		$errors[]="You forgot to enter your email";
	}
	   else
	   {
		   $e=$_POST['email'];
	   }
    if(empty($errors))
    {
	require("connect.php");
	$q='UPDATE users SET first_name="$fn", last_name="$ln", email="$e" WHERE user_id=$id LIMIT 1';
	$r=mysql_query($q,$dbc);
	$num=mysql_affected_rows($dbc);
	if($num==1)
	{
		echo "<p>You records have been edited</p>";
	}
	   else
	   {
		   echo "<p>You record could not be edited to due to server issue, sorry for the inconvenience</p><br />";
		   echo mysql_error($dbc)." Query : ".$q;
	   }
}
  else
  {
	  echo "Your records could not be updated because :- <br /> ";
	  foreach($errors as $msg)
	  {
		  echo ' - '.$msg.'<br />';
	  }
  }
}
   else
   {
	   echo '<form action="edit1.php" name="form1" method="post">'.
	   '<b>First Name : </b><input type="text" name="fname" /><br /><br />'.
	   '<b>Last Name : </b><input type="text" name="lname" /><br /><br />'.
	   '<b>Email : </b><input type="text" name="email" /><br /><br />'.
	   '<input type="hidden" name="id" value="'.$id.'" />'.
	   '<input type="submit" name="submit" value="Edit Record" /><br /><br />'.
	   '</form>';
   }

?>
</div>
<?php include("footer.html") ?>

change this line 19 from

if(isset($_POST['submitted']))

to

if(isset($_POST['submit']))

because you are giving name submit to the submit-button and not submitted.

It's now working, now when I fill in the last form, I get this error :-

You record could not be edited to due to server issue, sorry for the inconvenience

Unknown column '$id' in 'where clause' Query : UPDATE users SET first_name="$fn", last_name="$ln", email="$e" WHERE user_id=$id

use single quotes (') instead of double quotes(") at line no 48

$q="UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id='$id' LIMIT 1";

use single quotes (') instead of double quotes(") at line no 48

$q="UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id='$id' LIMIT 1";

It worked. Thanks a ton.

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.