Jump to content


Photo

passing values from a database into a text field


  • Please log in to reply
2 replies to this topic

#1 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 10 April 2006 - 03:37 PM

Hi, I'm new to php and having trouble with it. I am using a MySql database and am trying to display the values that I have stored on a customer into text fields so that they can be edited. I have passed the userId in from my login.php page and want to be able to update the details relevent to the userId that was passed in.

Here is the code I have so far, it could be completly wrong:

<?php

include("db.php");

$userId= $_GET['id'];


$sql = "select password, name, surname, address, address2, county, country, telNo, paypalEmail from customer where userName = '$userId';";
//db
$result = mysql_query($sql,$conn) or die(mysql_error());

//get the number of rows in the result set; should be 1 if a match
if (mysql_num_rows($result) == 1) {
//if authorized, get the values of name, surname...

$password = mysql_result($result, 0, 'password');
$name = mysql_result($result, 0, 'name');
$surname = mysql_result($result, 0, 'surname');
$address = mysql_result($result, 0, 'address');
$address2 = mysql_result($result, 0, 'address2');
$county = mysql_result($result, 0, 'county');
$country = mysql_result($result, 0, 'country');
$telNo = mysql_result($result, 0, 'telNo');
$paypalEmail = mysql_result($result, 0, 'paypalEmail');





}


Any help would be appreciated



#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 10 April 2006 - 03:44 PM

you could do it in an array.

here is a sample
<? // all previous stuff here

if (mysql_num_rows($result) == 1) {
//if authorized, get the values of name, surname...
// Fetch results into an array
$r = mysql_fetch_array($result);
// print out form
print '<form name=somename method=POST action=somepage.php>
<table>
<tr>
<td width=40%>Name</td>
<td width=60%><input type=text name=uname value="'.$r['name'].'"></td>
// rest of rows here...

<tr>
<td colspan=2 align=center><input type=submit value=Edit></td>
</tr>
</table>
</form>';
}
?>

Ray




#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 10 April 2006 - 03:48 PM

Now nothings wrong so far. But I dont why but when I see stuff like this:
$password = mysql_result($result, 0, 'password');
$name = mysql_result($result, 0, 'name');
$surname = mysql_result($result, 0, 'surname');
$address = mysql_result($result, 0, 'address');
$address2 = mysql_result($result, 0, 'address2');
$county = mysql_result($result, 0, 'county');
$country = mysql_result($result, 0, 'country');
$telNo = mysql_result($result, 0, 'telNo');
$paypalEmail = mysql_result($result, 0, 'paypalEmail');
It confuses the crap out of me, however It might not to you. But there is a simple to the above which is:
//put our results into an array ($row);
$row = mysql_fect_array($result);

$name = $row['name'];
$surname = $row['surname'];
$address = $row['address'];
$address2 = $row['address2'];
$county = $row['county'];
$country = $row['country'];
$telNo = $row['telNo'];
Much clearner and easier to follow! However this is my personal opinion. But you dont have to change your code if you dont want to.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users