开发者

fill a select html with mysql table rows

I'm writting a web app, I'm doing a html form with javascript to validate the data, I'm using a servlet to insert the data in a DB, but I need, when the form load, fill a select in the html form with the rows of a table in a MySql DB, I think I have to make a query in a ResultSet and then fill the select in the html form with this info, and then use the HttpServletResponse, but I have no idea how to make 开发者_如何学编程this


Use PHP to access the database and then put it into the form. Something like this:

 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM table_name WHERE id='$id'");

 $info = mysql_fetch_array($result))
 $name = $info['name'];
 $email = $info['email'];
 $anotherField = $info['another_field'];

In this, the info array has all of the database data in it of the id. But of course you would change the values and stuff. Change table_name to the table name, and if you are going to do something in the URL like ?id=373384 or something, then this will work. (I would assume you are doing it this way.) But, if it isn't 'id', just change WHERE id='$id' to what is in the database to identify a line.

$info['name']; would be the "name" column in your table. But, you can change 'name' and stuff to what you have in your database.

Here is the form code to pre-fill it with data:

<form>
<input type="text" name="name" value="<?php echo $name ?>" />
<input type="text" name="name" value="<?php echo $email ?>" />
<!--etc...-->
<input type="submit" value="Submit" />
</form>

You would need to change a lot of this, but I'm sure you get the point of it. :)

Note: This is not a simple copy+paste script. You will have to do a lot of charing because I don't know what your table values are and stuff.

If you have any questions, please do ask.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜