Jump to content

Fill Text Boxes With Data From mySql Table HeLp pLeAsE...?


TheStalker

Recommended Posts

Hi,

 

I am trying to create a edit customer page. I have a drop down box with the customers IDs in sourced from the database table. When i select a customerID i would like the text boxes to fill with the data about that customer e.g. surname, first name etc so i can edit it and then update the database.

 

dose anyone have any surgestions on a simple way of doing this?

 

do i need a fetch like ;while($row = mysql_fetch_array($result))?

 

 

this is what ive come up with so far but it dosnt work:

 

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = 'randv';
mysql_select_db($dbname);
?> 

 

 

<center>
<table border='0' width='300'>
<form>
<tr>
<td><p>CustomerID:</td><td><?php

print "<SELECT NAME=results>";

// Search alternativeDishes	
$query = "SELECT CustomerID FROM customers"; 
$result = mysql_query($query);

while ($line = mysql_fetch_array($result))
	{
	foreach ($line as $value)
		{
		print "<OPTION value='$value'";
		}
	print ">$value</OPTION>";
	}
?></td>



<?php $result = mysql_query("SELECT * FROM customers WHERE CustomerID = '$value'");?>

<tr> 
<td><p>Surname:</td>
<td><input type="text" name="surname" value="<?php echo $row ['Surname']?>" /></td>
</tr>
<tr>
<td><p>Forename:</td>
<td><input type="text" name="forename" /></td>
</tr>

just this min got this to work but it dosnt change when i chnage the CustomerID in the drop down box

 

 

<td><p>Surname:</td>
<td><input type="text" name="surname" value="<?php $result = mysql_query("SELECT * FROM customers WHERE CustomerID = '$value'"); ;while($row = mysql_fetch_array($result)) echo $row ['Surname']?>" /></td>

just this min got this to work but it dosnt change when i chnage the CustomerID in the drop down box

 

 

<td><p>Surname:</td>
<td><input type="text" name="surname" value="<?php $result = mysql_query("SELECT * FROM customers WHERE CustomerID = '$value'"); ;while($row = mysql_fetch_array($result)) echo $row ['Surname']?>" /></td>

 

Dont know but it could be

WHERE CustomerID = '$value'"

should be

WHERE CustomerID = $value"

 

Otherwise try

 

result = mysql_query("SELECT surname FROM customers
WHERE id=$value");

while($row = mysql_fetch_array($result))
  {
?>
<form>
  <textarea name="aboutme" cols="40" rows="5">
  <?php echo stripslashes($row['surname']); ?>
  </textarea><br>
  
  </form>

you've got some crazy stuff going on in your html.  There is at least one unclosed <td> tag and putting a <form> tag inside a table block is bad juju.

 

<center>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" name="names">
<table border='0' width='300'>
<tr>
<td><p>CustomerID:</p></td>
<td>
<SELECT NAME='results' onchange="this.form.submit();">
<?php	
	// Search alternativeDishes	
	$query = "SELECT CustomerID FROM customers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
</tr>

<?php 

if($value=$_POST['results']){

$result = mysql_query("SELECT * FROM customers WHERE CustomerID = '$value'");

$surname = mysql_result($result,0,'surname');
$forename = mysql_result($result,0,'forename');
}
?>

<tr> 
<td>Surname:</td>
<td><input type="text" name="surname" value="<?php echo $surname ?>" /></td>
</tr>
<tr>
<td>Forename:</td>
<td><input type="text" name="forename" value="<?php echo $forename?>"/></td>
</tr>
</table>
</form>
</center>

 

There, I cleaned it up and fixed some stuff.

 

Besides the html and php being funky, the reason it wasn't working was because you weren't processing the form.  The flow of this type of thing is:

 

1. enter data into form

2. submit form

3. page calls itself and passes data from form

4.  page checks for form data and if it is there, uses it.

 

The original script wasn't passing the form data to anything and it wasn't listening for any form data

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.