Page 1 of 2

php - taking data from database and inputting into a form??

Posted: Fri May 06, 2005 7:40 am
by ant_sutton
Hi guys.

I have a database with person table with name and address fields. I have PHP code that takes these values from the database and prints them into a HTML table. Does anyone know how to create a button or hyperlink to select one row/record from the html table and copy the values into a HTMl form. This will enable the values to be edited and resubmitted into the database.

Thanks alot

Anthony

Posted: Fri May 06, 2005 7:55 am
by Skara
you can echo the data anywhere you want in the html.

e.g.

Code: Select all

print("<input type='text' name='foo' value='{$value_from_db}' />");

thanks

Posted: Fri May 06, 2005 3:17 pm
by ant_sutton
arr thats great thanks alot. How can I echo the values from only the record selected. By this I mean I have a html table that is printed from the database with two rows. How can I select to edit values from one row only. e.g.

table:

bill | bill@hotmail.com |UK
anne | anne@hotmail.com |USA

How can I take the values from bills row only and input them in a html form to be editted?

Thanks alot

Anthony

Posted: Fri May 06, 2005 6:48 pm
by Skara
SELECT * FROM table WHERE name = 'bill'
[parse into the variables]
<input type='text' name='email' value='{$bill_email}' />
[submit form]
UPDATE table SET email='{$_GET['email']}' WHERE name = 'bill' LIMIT 1

Posted: Fri May 06, 2005 9:45 pm
by Jim_Bo
Hi,

Some thing like:


link:

Code: Select all

<a href="../edit.php?id=<?php echo $row['id']; ?>">EDIT</a>
edit.php

Code: Select all

<?php

// connection details here

$id = $_GET['id'];

$sql = "SELECT * FROM table WHERE id='$id'";

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {

?>

<form name="People" method="post" action="../edit_function.php">
   <table width="302" border="1" align="center" cellpadding="4" cellspacing="1" bordercolor="#000000" bgcolor="#FFFFFF">
		<td width="23%"><div align="right">Name:</div></td>
    	<td width="77%"><input name="name" type="text" size="39" value="<?php echo $row['name']; ?>"></td>
  	</tr>
    	<td><div align="right">City:</div></td>
    	<td><input name="city" type="text" size="20" value="<?php echo $row['city']; ?>"></td>
   </tr>
     	<td><div align="right">Email:</div></td>
     	<td><input name="email" type="text" size="39" value="<?php echo $row['email']; ?>"></td>
    </tr>
    <tr> 
      	<td>&nbsp;</td>
      	<td><div align="right"><input name="id" type="hidden" value="<?php echo $row['id']; ?>"><input type="submit" name="Submit" value="Submit"></div></td>
     </tr>
	</table>
</form>

<?php
}
?>
edit_function.php

Code: Select all

<?php


// connection details here

$id = $_POST['id'];
$name = $_POST['name'];
$city = $_POST['city'];
$email = $_POST['email'];

if(!$name) {
	
	echo '<center><b>Error!</b> Name is needed to continue.</center><br><br>';
	echo '<center><a href="#" onClick="history.go(-1)">BACK</a></center>';
	
} else {

$sql = "UPDATE table SET name='$name', city='$city', email='$email' WHERE id='$id'";
$result = mysql_query($sql);

if (!$result) {

	echo '<center><b>Error!</b> Please contact the administrator.</center><br><br>';
	echo '<center><< <a href="../index.php">BACK</a></center>';

} else {

	echo '<center><b>Record successfully updated</b></center><br><br>';
	echo '<center><< <a href="../index.php">BACK</a></center><br>';

 }
}

?>
Should give you the idea ..

PHP

Posted: Sat May 07, 2005 6:17 am
by ant_sutton
Hi guys

Thanks so much for your help. I'm wel on the way to getting it working. This is a great forum

Thanks again

Anthony

please help again!

Posted: Mon May 09, 2005 8:21 am
by ant_sutton
Hi guys

I have tried your code jim_bo and when I click on 'edit' the edit.php page returns a blank page. I think it is because the this code - <td><a href='edit.php?id=<?php echo $row['id']; ?>'>EDIT</a></td> is not assigning an id, so the get function on the edit.php page is not obtaining the values to run in the array. How exactly is the link code used? how does it have an id associated with the 'edit' link?

Thanks alot

Anthony

please help!

Posted: Mon May 09, 2005 1:38 pm
by ant_sutton
jim_bo, if your around, or anyone else please could you take another look at this for me. I really need this to work

Thanks again

Ant

Posted: Mon May 09, 2005 1:50 pm
by infolock
well, that's probably because $row['id'] is refering to a field that does not exist in your table..

Could you post the table layout you have in which you are trying to grab the field ID from? Also, could you post the script in which this is getting passed from? (IOW, the script that you are issuing the ...id=<?php echo $row['id']; ?> statement.

Thanks.

code

Posted: Mon May 09, 2005 3:53 pm
by ant_sutton
Hi again. Thanks for the reply info.

This is the code with the $row line on:

Code: Select all

&lt;html&gt;
&lt;body&gt;
&lt;?php
echo &quote;&lt;h1&gt; Report for Department: $dep &lt;/h1&gt;&quote;;
$MySQL_hostname = &quote;co-project.lboro.ac.uk&quote;;
      $MySQL_username = &quote;user20&quote;;
      $MySQL_password = &quote;ogh21aty&quote;;
      $db_name = &quote;db20&quote;;
      $connection = @mysql_connect($MySQL_hostname,$MySQL_username,
      $MySQL_password) or die(&quote;Couldn't connect&quote;);
      $db = @mysql_select_db($db_name, $connection)
	       or die(&quote;Couldn't select database&quote;);

echo &quote;&lt;table border=1&gt;
        &lt;th&gt;id&lt;/th&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Email&lt;/th&gt;
        &lt;th&gt;Edit&lt;/th&gt;&quote;;

$result = mysql_query(&quote;select id, name, email from db20.person&quote;);

if ($result)
{
   if (mysql_num_rows($result) &gt; 0)
   {
      while ($row = mysql_fetch_assoc($result))
      {
         $id= $row&#1111;&quote;id&quote;];
         $m = $row&#1111;&quote;name&quote;];
         $x= $row&#1111;&quote;email&quote;];
         print &quote;&lt;br&gt;\n&quote;;
         print &quote;&lt;br&gt;\n&quote;;
      } //removed a ; here.  these are gonna cause you errors (infolock)
   } //removed another one here....(infolock)
} // and another one here....(infolock)

echo&quote;&lt;tr&gt;
      &lt;td&gt;$id&lt;/td&gt;
      &lt;td&gt;$m&lt;/td&gt;
      &lt;td&gt;$x&lt;/td&gt;
      &lt;td&gt;&lt;a href='edit.php?id=&lt;?php echo $row&#1111;'id']; ?&gt;'&gt;EDIT&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;&quote;;
echo &quote;&lt;/table&gt;&quote;;
?&gt;
&lt;/body&gt;
&lt;/html&gt;
The field names of person table are 'id' 'name' 'email'.

Thanks

Posted: Mon May 09, 2005 5:19 pm
by Skara
Gya!! wrap your code in

Code: Select all

tags, please!

What's this bit?:
};
};
}; 
No need for ; there... >.>

And you just might want to remove the user and pass... ^^;  (Unless you just made that bit up)

php

Posted: Mon May 09, 2005 6:59 pm
by ant_sutton
Hi guys. sorry about the php tags, I forgot and thnaks for your reply skara. It didnt seem to make a difference. ANy other ideas please

Thanks lot

Ant

please help

Posted: Tue May 10, 2005 6:24 am
by ant_sutton
Hi guys. Please can someone have another look at this for me?

I'd really appreciate it

Thanks

Anthony

Posted: Tue May 10, 2005 7:08 am
by infolock
#1, read the changes i made in your code above. just making sure you removed all of them...

#2, change this

Code: Select all

<td><a href='edit.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>
to this :

Code: Select all

<td><a href='edit.php?id=".$row['id']."'>EDIT</a></td>
last, turn Error Reporting on, and see what kind of errors you are generating. right now you are coding in the dark and it's not getting you anywhere...

hi

Posted: Wed May 11, 2005 7:45 am
by ant_sutton
hi guys. I made the changes, and the edit.php still returns a blank page. Any ideas?? Thanks alot

Ant