dqdtgiw4736 2015-07-22 08:41
浏览 48
已采纳

基于HTML select选项在PHP中分配变量,并将结果插入数据库表

I'm running the following queries to retrieve info from the timesheetlogin table.

$clientlist = db_select("SELECT * FROM timesheetlogin WHERE type='client' ORDER BY company DESC");
$candlist = db_select("SELECT * FROM timesheetlogin WHERE type='cand' ORDER BY name DESC");

This info is displayed in a form on the front-end.

<select name="client" id="client">
<?php 
    foreach($clientlist as $key => $value) {
            $company = $clientlist[$key]["company"];
            $name = " (" . $clientlist[$key]["name"] . ")";
            echo '<option value="'. $company .'">'. $company . $name .' </option>';
            $clientid = $clientlist[$key]["id"];
                    }
                ?>  
</select><br />

So the form has lots of blank fields that I haven't shown, with just the client and candidate name being chosen from a dropdown.

Finally I'd like to insert the entry to the database:

if (isset($_POST['submit'])) {
    $clientid = db_quote($_POST['client']);
    $candid = db_quote($_POST['cand']);

Here is the SQL

db_query("INSERT INTO timesheets (clientid, candid, weekending, department, orderno, basicpay, basiccharge, otpay, otcharge, ot2pay, ot2charge, status, hue, huc) VALUES ('$client', '$cand', '$week_ending', '$department', '$order_no', '$basic_pay', '$basic_charge', '$ot_pay', '$ot_charge', '$ot2_pay', '$ot2_charge', '$status', '$hue', '$huc')");

The problem I have is I need to pass the id's associated with $_POST['client'] and $_POST['cand'] NOT the strings that are currently in the option fields.

My initial thoughts are to do something like this to check the 'client' field and query the database to get the value of the id:

if (isset($_POST['client'])) {
    //code
}

That seems messy though. I have already queried the db and looped through the $clientlist array, so the ids are available in that loop. Is there a way to assign the $clientid variable based on what the user chooses from the select box?

Thanks

  • 写回答

1条回答 默认 最新

  • doulu3399 2015-07-22 09:07
    关注

    You can put ids like this :

    $clientid = $clientlist[$key]["id"];
    echo '<option value="'.$clientid.'">'. $company . $name .' </option>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题