开发者

select from dropdown list containing sql data

开发者 https://www.devze.com 2023-03-08 13:42 出处:网络
Pls help. I am working on a php project and this requires adding and altering data from the SQL database. There are two tables开发者_如何学Go involved with this: emp which contains employee details a

Pls help. I am working on a php project and this requires adding and altering data from the SQL database. There are two tables开发者_如何学Go involved with this: emp which contains employee details and pay containing payroll records for each employee.

I would like to have a dropdown list containing all employee names (empname) from the database. Below the list is a form where the user shall input the payroll details. These details will then be saved into the SQL pay table corresponding to the employee name selected.

I've tried searching over the web but it doesn't seem to similar to my problem. How should I do this? Thanks!


Wihtout knowing any more details about your datbase, table structure and other stuff, this is closes that I could get.

<?php
/* Connect to SQL and retreive data */
$sqlConf = array('u'=>'username','p'=>'password','h'=>'localhost','d'=>'myDatabase');
$sqlCon = mysql_connect($sqlConf['h'], $sqlConf['u'], $sqlConf['p']);
if ($sqlCon) {
    $emp = array();
    mysql_select_db($sqlConf['d'], $con);
    $result = mysql_query("SELECT * FROM emp");
    while($row = mysql_fetch_array($result)) { $emp[] = $row; }
    mysql_close($sqlCon);
}
/* Generate select box contents */
$html = '<select name="emp">';
$html .= '<option>Employee list</option>';
if (!empty($emp)) {
    foreach ($emp as $k => $v) {
        $html .= '<option value="'.$v['empid'].'">'.$v['empname'].'</option>';
    }
}
$html .= '</select>';
/* Output */
echo $html;
?> 


Sounds like you need to learn some basic php/SQL. The basic outlines of the problem are:

  • Query the dropdown list from SQL, and display in an option-select.
  • When they the form submit, send a corresponding query to update the database.

Is there some particular aspect of this problem that you can't get past?


This can be solved in any number of ways and the overall structure, design and layout of your application would dictate which route to go.

You could have classes that represent your database objects, classes that represent your forms, you could print your forms by mixing php and html like a crazy person and so on and so on.

Then it all depends on if you will be using ajax or not.

So give us a bit more information about how your application is designed and that will probably help.

If you have not started at all I would suggest you to look into some of the many frameworks. Zend Framework or Symphony for example. They have a good way of handling db abstractions and forms creation etc.

0

精彩评论

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

关注公众号