0

Hi

I'm new in php.I have created a Form to insert data into the database its worrking fine.The other is to show all records from the database in table form with Edit/Delete link in the end of each record its fine and I can delete a record but i have a problem with Edit link i don't know where to start, Want i want is to edit one record at a time when i click edit I want the form to appear with Name,Surname and Email filled with the data of that record and be able to edit it and save.

4
Contributors
11
Replies
12
Views
6 Years
Discussion Span
Last Post by cossay
0

Okay, the correct way to do this will depend on what your form action and method are set to do. If you could post your form code I should be able to help.

0

Below is my form code

<form action="Mainpage.php" method="post" >
Name:<br />
<input type="text" name="Name" size="30" /><br />
Email:<br />
<input type="text" name="Email" size="30" /><br />
Comment:<br />
<input type="text" name="Comment" rows="80" cols="80" /><br />

<input type='submit' name="save" value="save" />

</form>

0

So if this is your edit page: I have changed the form action to post the form to the same page as it is clearer to explain this way:

<form action="<?php $SERVER['PHP_SELF'] ?>" method="post" >
Name:<br />
<input type="text" name="Name" size="30" /><br />
Email:<br />
<input type="text" name="Email" size="30" /><br />
Comment:<br />
<input type="text" name="Comment" rows="80" cols="80" /><br />
<input type='submit' name="save" value="save" />
</form>

<?php
if(isset($_POST['save']))    //determine whether the button is clicked
{
    $name = $_POST['Name'];       //gather the form values
    $email = $_POST['Email'];
    $comment = $_POST['Comment'];
    
    //update query
    $update = mysql_query("UPDATE table SET name='$name', email='$email', comment='$comment' WHERE userID='12'") or (mysql_error());      
}
?>

You could also pre-populate these fields with the users existing details using the value attribute, but you'd have to pass them from the previous page or get their details from the database.

<input type="text" name="Name" size="30" value="$name"/>
0

Look at this:
database : susu
table = user_details

fable fields
id, first, last, gender, region, mobile, email, password

let assume you fetched an id, say SU/MAU/34/0002 from from the user_details table in the susu database which you stored in $user_id variable.

For the link to your update page you will write

echo "<a href = 'update_page.php?id=".urlencode($user_id)."'>Update</a>";

On the update page you would write something like this:

<?php
$con = mysql_connect("localhost", "root", "password") or die("Unable to connect".mysql_error());
mysql_select_db("susu");

if(!isset($_POST['update']))
{
	//Fetch a data associated with the value stored in $_GET['id'] form the db
	$id = mysql_real_escape_string($_GET['id']);
	$query = "SELECT * FROM user_details WHERE id = '".$id."'";
	$result = mysql_query($query) or die(mysql_error());
	$customer = mysql_fetch_object($result);
	
?>
<table widht = "550 align = "left">
<tr><td>
<fieldset id = 'update_set'>
<legend><i>Current account details</i></legend>
<form method = "post" action = "<?php echo $_SERVER[PHP_SELF];?>">
<table width = "550" align = "center" cellspacing = "2" cellpadding = "2" border = "0">
<tr>
	<td width = "200" align = "right">First name &nbsp;</td>
	<td align = "left"><input type = "text" name = "first" size = "30" value = "<?php echo $customer->first;?>"></td>
	
</tr>
<tr>
	<td align = "right">Last name &nbsp;</td>
	<td align = "left"><input type = "text" name = "last" size = "30" value = "<?php echo $customer->last;?>"></td>
	 
</tr>
<tr>
	<td align = "right">Gender &nbsp;</td>
	<td align = "left">
		<select name = "gender">
			<option value = "<?php echo $customer->gender;?>">
			<?php echo $customer->gender;?>
			</option>
			<option value = "Female">Female</option>
			<option value = "Male">Male </option>
		</select>
	</td>
	 
</tr>
<tr>
	<td align = "right">Region &nbsp;</td>
	<td align = "left">
		<select name = "region">
			<option value = "<?php echo $customer->region;?>">
			<?php echo $customer->region;?>
			
			</option>
			<option value = "Ashanti">Ashanti</option>
			<option value = "Brong Ahafo ">Brong Ahafo</option>
			<option value = "Central ">Central</option>
			<option value = "Greater Accra ">Greater Accra</option>
			<option value = "Eastern ">Eastern</option>
			<option value = "Western ">Western</option>
			<option value = "Northern ">Northern</option>
			<option value = "Volta ">Volta</option>
			<option value = "Upper West ">Upper West</option>
			<option value = "Upper East ">Upper East</option>
		</select>
	</td>
	 
</tr>
<tr>
	<td align = "right">Mobile &nbsp;</td>
	<td align = "left"><input type = "text" name = "mobile" size = "30" value = "<?php echo $customer->mobile;?>"></td>
	 
</tr>
<tr>
	<td align = "right">Email &nbsp;</td>
	<td align = "left"><input type = "text" name = "email" size = "30" value = "<?php echo $customer->email;?>"></td>
	 
</tr>
<tr>
	<td align = "right">Password &nbsp;</td>
	<td>
	<input type = "password" name = "pass" size = "30" value = "<?php echo $customer->password;?>">
	</td>
</tr>

<tr>
	<td align = "right">Password (Confirm) &nbsp;</td>
	<td>
	<input type = "password" name = "pass_verify" size = "30" value = "<?php echo $customer->password;?>">
	</td>
</tr>

<tr>
	<td></td>
	<td align = "left">
		<input type = "submit" name = "update" value = "UPDATE ACCOUNT">
	</td>
</tr>

</table>
</form>
</fieldset></td></tr>
</table>

<?php
}
else
{
	
	//Your update query here
	
}
?>

Edited by cossay: n/a

0

Can you post all your code so far and put your separate pages in separate code tags with their file name at the top. This will make it easier to help you.

0

@cossay:

You might want to check your code for typos. For example:

<table widht = "550 align = "left">

Edited by n_e: n/a

0

That's still wrong Cossay. Tables are block-level elements, thus you can't be aligned. The syntax doesn't look right either

Edited by nonshatter: n/a

0

Well I know a table can be aligned to the left, right and center, and the default alignment is left. My next correct is

<table width = "550">
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.