Jump to content

passing values from a database into a text field


kikilahooch

Recommended Posts

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

you could do it in an array.

here is a sample
[code]<? // 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>';
}
?>[/code]

Ray


Now nothings wrong so far. But I dont why but when I see stuff like this:
[code] $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');[/code]It confuses the crap out of me, however It might not to you. But there is a simple to the above which is:
[code]//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'];[/code]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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.