zgkhoo Posted August 19, 2007 Share Posted August 19, 2007 <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. Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/ Share on other sites More sharing options...
phpQuestioner Posted August 19, 2007 Share Posted August 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-327920 Share on other sites More sharing options...
zgkhoo Posted August 19, 2007 Author Share Posted August 19, 2007 <?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"]; Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-327936 Share on other sites More sharing options...
zgkhoo Posted August 19, 2007 Author Share Posted August 19, 2007 problem solved,solution =select * from person; but how to write the update function ? using if(isset($_REQUEST['button'])){ } or? thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-327966 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-327969 Share on other sites More sharing options...
zgkhoo Posted August 19, 2007 Author Share Posted August 19, 2007 u use if(isset($_POST['button'])){ ?? wat different with if(isset($_REQUEST['button'])){ ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-327972 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-327973 Share on other sites More sharing options...
zgkhoo Posted August 19, 2007 Author Share Posted August 19, 2007 <?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.. Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-327998 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-328001 Share on other sites More sharing options...
zgkhoo Posted August 19, 2007 Author Share Posted August 19, 2007 problem solved, thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/65653-solved-how-to-appear-mysql-data-into-a-form-text-and-element/#findComment-328016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.