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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错