0

Is there an easy way to have a user view their information in form fields? I have set up a query for a user's information in a table to come up, but I want the user to be able to edit their information...
Does this require other programming language knowledge other than php???

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by fobos
0

You read in the database record and then define values for each of the fields in the form as the variables that came from the database. This will work for text and textarea's. You have to do a bit more work if you are trying to put values in check boxes, radio buttons or drop-down lists.

0

No it doesnt require additional languages. To do this first you must initiate the connection and access the database and table.
In this we will user and name as fields in your table

<?php
// use variables to identify the user. session would be the best
$user = $_SESSION['username']; // whatever you want
$con = mysql_connect("host","user","pass");
if(!con) { die("Couldnt Connect" . mysql_error()); }
mysql_select_db("db",$con);
$sql = "SELECT * FROM table WHERE user = $user // this is whatever you want to define the user
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>

Now that you have connected to the database and the info is pulled, time to display it.

<form action="<?php $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<input type="text" name="myname" value="<?php echo $row['name']?>">
<input type="submit" name="submit" value="Update">
</form>

The form is submitting itself to same page its one. Now let update..

<?php
// i like to seperate this from the pulling data. this way its only called when submit is pressed
if(isset($_POST['submit'])) {
    $con = mysql_connect("host","user","pass");
    if(!con) { die("Couldnt Connect" . mysql_error()); }
    mysql_select_db("db", $con);
    mysql_query("UPDATE table SET name = '$_POST[myname]' WHERE user = $user");
    // redirect to another page if you want.
    header("location:somepage.php");
}
?>

Let me know if this helped

0

Hi, thank you for taking the time with all the code! I will try later tonight and let you know how it goes...

0

I tried creating the php page like this as instructed below as a test to see whether the user's name would show up in the form.
When the page is displayed, I am getting a blank form (the user name is not showing up in the text field).

The name of the column in the database table with the user's name is fname. How would I need to change the php code here for fname to show up in the text field?

<input type="text" name="myname" value="<?php echo $row['name']?>">

Also, where would I place this php code (below) for the update to occur (i.e. a new php page or the current page)?

<?php
// i like to seperate this from the pulling data. this way its only called when submit is pressed
if(isset($_POST['submit'])) {
    $con = mysql_connect("host","user","pass");
    if(!con) { die("Couldnt Connect" . mysql_error()); }
    mysql_select_db("db", $con);
    mysql_query("UPDATE table SET name = '$_POST[myname]' WHERE user = $user");
    // redirect to another page if you want.
    header("location:somepage.php");
}
?>

___________________________________________________________________________________________________
Here is the full code of my php page below:

<?php
session_start();  
 
if (!isset($_SESSION['memberusername'])){  
	header("Location: contractorlogin.php");  
	exit();  
}
 
mysql_connect('localhost','xxx','xxxxxx') or die( mysql_error() );
mysql_select_db('xxxxx') or die( mysql_error() );
$user = mysql_real_escape_string($_SESSION['memberusername']);
$sql = "SELECT Username FROM contractors WHERE Username='" . $user . "'";
$result = mysql_query($sql) or die( 'Unable to execute<br>'.$sql.'<br>'.mysql_error());
if( !mysql_num_rows($result) )
{
	echo '<p>No Records found!</p>';
}
else
{
	$row=mysql_fetch_assoc($result);
	echo '<table><tr><th>' .implode('</th><th>', array_keys($row) ).'</th></tr>';
	do
	{
		echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
	}while($row=mysql_fetch_assoc($result));
	echo '</table>';
}
 
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="<?php $_SERVER['PHP_SELF']?> " method="post" enctype="multipart/form-data">
<input type="text" name="myname" value="<?php echo $row['name']?>">
<input type="submit" name="submit" value="Update">
</form>
</body>
</html>
0

Well thats where you need to start reading my friend. i gave you the basics, now its time for you to learn. By the way the "name" in the input was just something that i written in there, change it to your table row name. Also, all of that would go onto 1 page.

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.