Jump to content

[SOLVED] how to appear mysql data into a form text and element?


Recommended Posts

<form name='detail' action='detail.php' method=post>
<table>
<tr>
<td>age</td>
<td><input type="text" name='age' ></td>
</tr>
<tr>
<td>username</td>
<td><input type="text" name='username' ></td>
</tr>
<tr>
<td>address</td>
<td><input type="text" name='address' ><td>
</tr>
<tr>
<td></td>
<td><input type="submit" name='button' value='update'><td>
<tr>
</form>

i wanna retrieve data from mysql such as username,address,age and appear those value in those form elements and user can modify and update from it..

thanks.

try this:

 

 

<?php

mysql_connect("localhost","username","password");
mysql_select_db("MyDatabase");
$results = mysql_query("select * from MyDataTable where id='$id'");
while ($r=mysql_fetch_array($results)) {

$age=$r["age"];
$username=$r["username"];
$address=$r["address"];

echo "<form name='detail' action='detail.php' method=post>
<table>
<tr>
<td>age</td>
<td><input type='text' name='age' value='$age' ></td>
</tr>
<tr>
<td>username</td>
<td><input type='text' name='username'  value='$username'></td>
</tr>
<tr>
<td>address</td>
<td><input type='text' name='address'  value='$address'><td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='button' value='update'><td>
<tr>
</form>";

}

?>

 

 

You will need a field in your database named "id" (I suggest setting it to auto increment). Too see results for each row in you database you would send a query string in you address bar like so: http://www.mydomain.com/detail.php?id=123.

<?php
include 'config.php';
include 'opendb.php';


session_start();


echo "loginname=".$_SESSION['loginname'];
$login=$_SESSION['loginname'];
echo "<br/>";
echo "loginvariable=".$login;

$result = mysql_query("SELECT username,password FROM person WHERE 
username='$login'") or die('Query failed: ' . mysql_error());


while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
   echo "row username=".$row["username"];
   echo "row password=".$row["password"];
   echo "row country=".$row["country"];
   echo "row ic=".$row["ic"];
   

echo "<table>";
echo "<tr>";
echo "<td>";
echo "username=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='username' value='".$row["username"]."' />";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>";
echo "Country=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='country' value='".$row["country"]."' />";
echo "</td>";
echo "</tr>";


echo "<tr>";
echo "<td>";
echo "IC=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ic' value='".$row["IC"]."' />";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>";
echo "fullname=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='fullname' value='".$row["Fullname"]."' />";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>";
echo "gender=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='gender' value='".$row["Gender"]."' />";
echo "</td>";
echo "</tr>";


echo "<tr>";
echo "<td>";
echo "DOB=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='dob' value='".$row["dob"]."' />";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>";
echo "address=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='address' value='".$row["address"]."' />";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>";
echo "postcode=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='postcode' value='".$row["postcode"]."' />";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>";
echo "city=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='city' value='".$row["city"]."' />";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>";
echo "state=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='state' value='".$row["state"]."' />";
echo "</td>";
echo "</tr>";


echo "<tr>";
echo "<td>";
echo "phonenumber=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='phonenumber' value='".$row["phonenumber"]."' />";
echo "</td>";
echo "</tr>";


echo "<tr>";
echo "<td>";
echo "hpnumber=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='hpnumber' value='".$row["hpnumber"]."' />";
echo "</td>";
echo "</tr>";


echo "<tr>";
echo "<td>";
echo "accname=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='accname' value='".$row["accname"]."' />";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>";
echo "accnumber=";
echo "</td>";
echo "<td>";
echo "<input type='text' name='accnumber' value='".$row["accnumber"]."' />";
echo "</td>";
echo "</tr>";



}//end while



?>


 

just this bellow showing data correctly with record, but if i change "username" to "Username" then just showing empty record.werid.

 

   echo "row username=".$row["username"];
   echo "row password=".$row["password"];

 

this bellow line cant function..there is empty display for $row["country"] and $row["ic"]; weird, which i using phpmyadmin and there is record in there, not empty..in fact.

   echo "row country=".$row["country"];
   echo "row ic=".$row["ic"];

 

 

MySQL Manual link

 

if(isset($_REQUEST['button'])){ } will be fine (personally i'll use $_POST)

 

$TheField = "ten";
$field2 = 1;

//SQL statement
UPDATE table SET field = '$TheField' WHERE field2 = $field2;

If you don't care where the request data comes from, you can use $_REQUEST as it contains a mix of GET, POST and COOKIE data

 

Personally i like to know where the data is from $_REQUEST is too much like register_globals which i hate..

<?php
include 'config.php';
include 'opendb.php';


session_start();


echo "loginname=".$_SESSION['loginname'];
$login=$_SESSION['loginname'];
echo "<br/>";
echo "loginvariable=".$login;

$result = mysql_query("SELECT * FROM person where username='$login'") or die('Query failed: ' . mysql_error());


while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
  /* echo "username=".$row["username"];
   echo "password=".$row["password"];
    echo "ic=".$row["ic"];
   
    
   echo "row username=".$row["username"];
   echo "row password=".$row["password"];
   echo "row country=".$row["country"];
   echo "row ic=".$row["ic"];
*/

echo
"<form name='detail' action='record.php' method=post>
<table>



<tr>
<td>ic</td>
<td><input type='text' name='ic'  value=$row[iC]></td>
</tr>


<tr>
<td>fullname</td>
<td><input type='text' name='fullname'  value=$row[FullName]><td>
</tr>


<tr>
<td>gender</td>
<td><input type='text' name='gender'  value=$row[Gender]><td>
</tr>


<tr>
<td>dob</td>
<td><input type='text' name='dob'  value=$row[DOB]><td>
</tr>


<tr>
<td>address</td>
<td><input type='text' name='address'  value=$row[Address]><td>
</tr>

<tr>
<td>postcode</td>
<td><input type='text' name='postcode'  value=$row[Postcode]><td>
</tr>

<tr>
<td>address</td>
<td><input type='text' name='city'  value=$row[City]><td>
</tr>

<tr>
<td>state</td>
<td><input type='text' name='state'  value=$row[state]><td>
</tr>


<tr>
<td>phonenumber</td>
<td><input type='text' name='phonenumber'  value=$row[PhoneNumber]><td>
</tr>

<tr>
<td>hpnumber</td>
<td><input type='text' name='hpnumber'  value=$row[HpNumber]><td>
</tr>

<tr>
<td>banktype</td>
<td><input type='text' name='banktype'  value=$row[bankType]><td>
</tr>

<tr>
<td>accname</td>
<td><input type='text' name='accname'  value=$row[AccName]><td>
</tr>

<tr>
<td>accnumber</td>
<td><input type='text' name='accnumber'  value=$row[AccNumber]><td>
</tr>


<tr>
<td></td>
<td><input type='submit' name='button' value='update'><td>
<tr>
</form>
";//end of echo





}//end while


if(isset($_REQUEST['button'])){
				echo $_POST[ic]; 
                    mysql_query("UPDATE Person SET ic = '$_POST[ic]' 
                    where username='$login'");
                    
}



?>

 

The form text element just keep showing the last data..not the data i update? eg

initially there is 888 , then when i type 88 and click update, then it appear back to 888, but the mysql data already updated to 88, just this form text element delay..or we need to set the form element equal to mysql data inside if(isset($_REQUEST['button'])){??

how to write?

echo "<input type='text' name='ic'  value=$row[iC]>"

or need use refresh?

thanks..

move

 

if(isset($_REQUEST['button'])){
				echo $_POST[ic]; 
                    mysql_query("UPDATE Person SET ic = '$_POST[ic]' 
                    where username='$login'");
                    
}

 

just under

 

echo "loginname=".$_SESSION['loginname'];
$login=$_SESSION['loginname'];
echo "<br/>";
echo "loginvariable=".$login;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.