php - taking data from database and inputting into a form??
Moderator: General Moderators
-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
php - taking data from database and inputting into a form??
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
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
you can echo the data anywhere you want in the html.
e.g.
e.g.
Code: Select all
print("<input type='text' name='foo' value='{$value_from_db}' />");-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
thanks
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
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
Hi,
Some thing like:
link:
edit.php
edit_function.php
Should give you the idea ..
Some thing like:
link:
Code: Select all
<a href="../edit.php?id=<?php echo $row['id']; ?>">EDIT</a>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> </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
}
?>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>';
}
}
?>-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
PHP
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
Thanks so much for your help. I'm wel on the way to getting it working. This is a great forum
Thanks again
Anthony
-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
please help again!
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
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
-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
please help!
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
Thanks again
Ant
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.
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.
-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
code
Hi again. Thanks for the reply info.
This is the code with the $row line on:
The field names of person table are 'id' 'name' 'email'.
Thanks
This is the code with the $row line on:
Code: Select all
<html>
<body>
<?php
echo "e;<h1> Report for Department: $dep </h1>"e;;
$MySQL_hostname = "e;co-project.lboro.ac.uk"e;;
$MySQL_username = "e;user20"e;;
$MySQL_password = "e;ogh21aty"e;;
$db_name = "e;db20"e;;
$connection = @mysql_connect($MySQL_hostname,$MySQL_username,
$MySQL_password) or die("e;Couldn't connect"e;);
$db = @mysql_select_db($db_name, $connection)
or die("e;Couldn't select database"e;);
echo "e;<table border=1>
<th>id</th>
<th>Name</th>
<th>Email</th>
<th>Edit</th>"e;;
$result = mysql_query("e;select id, name, email from db20.person"e;);
if ($result)
{
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
$id= $rowї"e;id"e;];
$m = $rowї"e;name"e;];
$x= $rowї"e;email"e;];
print "e;<br>\n"e;;
print "e;<br>\n"e;;
} //removed a ; here. these are gonna cause you errors (infolock)
} //removed another one here....(infolock)
} // and another one here....(infolock)
echo"e;<tr>
<td>$id</td>
<td>$m</td>
<td>$x</td>
<td><a href='edit.php?id=<?php echo $rowї'id']; ?>'>EDIT</a></td>
</tr>"e;;
echo "e;</table>"e;;
?>
</body>
</html>Thanks
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)-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
php
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
Thanks lot
Ant
-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
please help
Hi guys. Please can someone have another look at this for me?
I'd really appreciate it
Thanks
Anthony
I'd really appreciate it
Thanks
Anthony
#1, read the changes i made in your code above. just making sure you removed all of them...
#2, change this
to this :
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...
#2, change this
Code: Select all
<td><a href='edit.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>Code: Select all
<td><a href='edit.php?id=".$row['id']."'>EDIT</a></td>-
ant_sutton
- Forum Commoner
- Posts: 32
- Joined: Thu May 05, 2005 5:27 am
hi
hi guys. I made the changes, and the edit.php still returns a blank page. Any ideas?? Thanks alot
Ant
Ant