Edit problem

HI ,how are you Iam using this script to edit data , the script can retrieve the data( just the name field, the area field I don’t know how to retrieve data from text area) in the fields but can not update them and insert them again the data base under the same id so Any help would be appreciated.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Joke CMS: Edit Author</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
 error_reporting(E_ALL ^ E_NOTICE);
  
    //1-connecting to localhost
    if(!$connect=mysql_connect('localhost','root','')){
    exit('unable to connect to the data base');}
    
   // select database
    if (!mysql_select_db('topic')){
    exit ('unable to locate database');}
    
if (isset($_POST['name'])):
// The author's details have been updated.
$name = $_POST['name'];
$email = $_POST['area'];

$sql = "UPDATE omar SET
name='$name',
area='$area'
WHERE id='$id'";
if (@mysql_query($sql)) {
echo '<p>Author details updated.</p>';
} else {
echo '<p>Error updating author details: ' .
mysql_error() . '</p>';
}
?>
<p><a href="duty1.php">Return to the home pagelist</a></p>
<?php
else: // Allow the user to edit the author
$id = $_GET['id'];
$author = @mysql_query(
"SELECT name, area FROM omar WHERE id='$id'");
if (!$author) {
exit('<p>Error fetching author details: ' .
mysql_error() . '</p>');
}
$author = mysql_fetch_array($author);
$name = $author['name'];
$area = $author['area'];
// Convert special characters for safe use
// as HTML attributes.
$name = htmlspecialchars($name);
$area = htmlspecialchars($area);
?>

 <form action="editduty2.php"  method="post">
ID:<br/><input type="hidden" name="id" value="<?php echo $id;?>"/> <br/><br/>
name:<br/><input type="text" name="name" value="<?php echo $name;?>"/> <br/><br/>
Area:<textarea name="area" cols="60" rows="10" >
</textarea><br/>
<input type="submit" value="GO" />
  </form>
<?php endif; ?>
</body>
</html>










You haven’t set $id before putting it into the update query, so that’s bound to be the problem.

As for putting text in the textarea, simply put it between the textarea open tag and close tag.

sorry but I didn’t get it ,thank you for the textarea , I 've tried with the first part to set the id but I didnt get any thing.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Joke CMS: Edit Author</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
 error_reporting(E_ALL ^ E_NOTICE);
  
    //1-connecting to localhost
    if(!$connect=mysql_connect('localhost','root','')){
    exit('unable to connect to the data base');}
    
   // select database
    if (!mysql_select_db('topic')){
    exit ('unable to locate database');}
    
  
if (isset($_POST['name'])):
// The author's details have been updated.

 $id=isset($_POST["id"]) ? $_POST['id'] : "";
     $name=isset($_POST["name"]) ? $_POST['name'] : "";
     $area=isset($_POST["area"]) ? $_POST['area'] : "";

$sql = "UPDATE omar SET
name='$name',
area='$area'
WHERE id='$id'";
if (@mysql_query($sql)) {
echo '<p>Author details updated.</p>';
} else {
echo '<p>Error updating author details: ' .
mysql_error() . '</p>';
}
?>
<p><a href="duty1.php">Return to the home pagelist</a></p>
<?php
else: // Allow the user to edit the author
$id = $_GET['id'];
$author = @mysql_query(
"SELECT name, area FROM omar WHERE id='$id'");
if (!$author) {
exit('<p>Error fetching author details: ' .
mysql_error() . '</p>');
}
$author = mysql_fetch_array($author);
$id= $author['id'];
$name = $author['name'];
$area = $author['area'];
// Convert special characters for safe use
// as HTML attributes.
$name = htmlspecialchars($name);
$area = htmlspecialchars($area);
?>

 <form action="editduty2.php"  method="post">
ID:<br/><input type="hidden" name="id" value="<?php echo $id;?>"/> <br/><br/>
name:<br/><input type="text" name="name" value="<?php echo $name;?>"/> <br/><br/>
Area:<textarea name="area" cols="60" rows="10" ><?php echo $name;?>
</textarea><br/>
<input type="submit" value="GO" />
  </form>
<?php endif; ?>
</body>
</html>


















Well, first of all you need to echo $area in the textarea not $name, but that’s not relevant to your issue.

First of all, is this file which you’re posting called editduty2.php? So the form is posting to the same file which output it?

Secondly, you need to do certain tests to make sure that everything is going as planned. Because I’m feeling generous, here’s what you need to do in code:


<?php
define('DISPLAY_EVENTS', 1); //set to 0 if you don't want to list the event array
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Joke CMS: Edit Author</title>
		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	</head>
	<body>
	<?php
		error_reporting(E_ALL ^ E_NOTICE);
		$connect = mysql_connect('localhost','root','');
		if(!$connect) exit('unable to connect to the database');
		if (!mysql_select_db('topic')) exit('unable to locate database');
		$events = array(); //add details of every important stage - the result of ifs etc.
		if (isset($_POST['name'], $_POST['id'], $_POST['area'])){ //this makes sure everything was passed to the handler. You can't just give the default values as "" because this has no real meaning.
			// The author's details have been updated.
			$events[] = 'The fields "name", "id" and "area" were inside $_POST';
			$id = $_POST['id'];
			$name = $_POST['name'];
			$area = $_POST['area'];
			$events[] = "The fields contain the values: <b>'{$id}'</b>, <b>'{$name}'</b> and <b>'{$area}'</b> respectively";
			$id = (int)$id; //cast ID to an integer
			$events[] = 'After casting $id to an integer, it has the value <b>' . $id . '</b>';
			$name = mysql_real_escape_string($name); //escape it for database usage Oh and make sure Magic_Quotes is set as OFF in PHP.ini, it can give you false security about your application.
			$events[] = 'After escaping $name it contains the value: <b>' . $name . '</b>';
			$area = mysql_real_escape_string($area);
			$events[] = 'After escaping $area it contains the value: <b>' . $area . '</b>';
			$sql = "
					UPDATE omar
						SET
							name = '{$name}',
							area = '{$area}'
						WHERE
							id = {$id}
					";
			$events[] = "The generated SQL code is: <b>{$sql}</b>";
			$query = mysql_query($sql);
			if($query === false){
				$events[] = 'Error in the updating query: <b>' . mysql_error() . '</b>';
			}else{
				$events[] = 'Query has run successfully. We want 1 row to be affected - There were <b>' . mysql_affected_rows() . '</b> rows affected.';
			}
			echo '<p><a href="duty1.php">Return to the home pagelist</a></p>';
		}else{
			$events[] = "The fields 'id', 'name' and 'area' aren't inside $_POST. We want them to be in $_POST if the form has been submitted.";
			$events[] = "The script will therefore begin the process of displaying the current information.";
			$id = (int)$_GET['id'];
			$events[] = "The field ID in the $_GET array was <b>'{$_GET['id']}'</b> and was cast to an integer to become <b>'{$id}'</b>";
			$sql = "SELECT name, area FROM omar WHERE id={$id}";
			$events[] = "The SQL generated to get the information for the form was: <b>{$sql}</b>";
			$query = mysql_query($sql);
			if ($query === false) {
				$events[] = "The query produced an error: <b>" . mysql_error() . "</b>";
			}else if(mysql_num_rows($query) == 0){
				$events[] = "The query produced no rows - there is no row with id <b>{$id}</b>";
			}else{
				$row = mysql_fetch_array($query);
				$id = $row['id'];
				$name = $row['name'];
				$area = $row['area'];
				$name = htmlspecialchars($name);
				$area = htmlspecialchars($area);
				$events[] = "The row returned contained the following IDs for id, name and area: <b>{$id}</b>, <b>{$name}</b>, <b>{$area}</b>";
				$events[] = "The form is now going to be displayed";
				// Convert special characters for safe use in HTML
				?>
					<form action="editduty2.php"  method="post">
						<input type="hidden" name="id" value="<?php echo $id;?>" />
						<label for="edit_name" style="display: block">name</label>
						<input type="text" id="edit_name" name="name" value="<?php echo $name;?>"/>
						<label for="edit_area" style="margin-top: 2em; display: block">area</label>
						<textarea name="area" cols="60" rows="10" style="display: block"><?php echo $area;?></textarea>
						<input type="submit" value="GO" />
					</form>
				<?php
				$events[] = "The form has been displayed";
			}
		}
		$events[] = "End of processing. Displaying bottom HTML";
		if(DISPLAY_EVENTS == 1){
			echo '<h2>Events:</h2>';
			echo '<ul>';
			foreach($events as $event){
				echo '<li>', $event, '</li>';
			}
			echo '</ul>';
		}
		?>
	</body>
</html>

Give it a go :slight_smile: