dpd66100 2015-07-14 16:23
浏览 85

函数中的Last_Insert_ID()

I have a webform where users enter names and select a "program" name from a drop down. I have a javascript function that allows the user to add boxes when they want to enter multiple names into the form. When the program is selected, and inserted into the table, a Primary Key(LINE_ID) is generated. The names are inserted into another table with then name and LINE_ID in their respective columns.

    <div>
<label>Name:</label>
<tr>
<td><input type ="Text" name="yourname"/></td>
<td><input onclick="addBox(this);" type="button" value="+ Add Another Name"/></td>
</tr>
</div>

<script type="text/javascript">
        var BoxNum = 0;
        var NameCount = [];
    function addBox(input) {
    console.log(input);
        BoxNum ++;
            var row ='<input name = "BoxNum'+BoxNum+'">';
        $(input).before(row);
        NameCount.push(BoxNum);
        $('#nameCount').val(NameCount);
        };
    </script>

The first name that appears will be entered useing the pre-existing HTML input box (1), the additional boxes will be generated by the user clicking a button (2).

    <?php

    $name = $_POST['yourname'];
    $program = $_POST['PROGRAM_NAME'];
        mysql_query("INSERT INTO prod_activity (PROGRAM_ID) (SELECT PROGRAM_ID FROM tc_program WHERE PROGRAM_NAME='$program' AND ZONE_ID=4)");
        $lineid = mysql_insert_id();
        // THIS WORKS:
mysql_query("INSERT INTO tc_user (USER_NAME, LINE_ID) VALUES ('$name','$lineid')");
        $ElementNo = 0;
    function POSTALLNAMES(){
        //$ElementNo = 0;
        $ElementNo ++;
        $nameCount = $_POST['nameCount'];
        $nameelement = explode(",", $nameCount);
        $NameCountLgth = sizeof($nameelement);
        foreach ($nameelement as $value){
        $RealName = $_POST["BoxNum${value}"];
// THIS DOES NOT WORK:
        mysql_query("INSERT INTO tc_user (USER_NAME, LINE_ID) VALUES ('$RealName','$lineid')");}}
            POSTALLNAMES();

The first time I try and insert the $lineid variable with the HTML (1) box, everything is inserted into tc_user correctly. The second time I try and do it with the javascript generated input boxes (2) it does not. When I echo the variable $lineid after POSTALLNAMES(); runs, it gives the correct value for $lineid which tells me that the variable is not being reset at any point.

When I take out the LINE_ID/$lineid from the Insert statement in (2) and leave it in (1), everything inserts as you would expect (the first name with the ID and the additional names without).

  • 写回答

2条回答 默认 最新

  • douhe3313 2015-07-14 16:35
    关注

    Use $lineid as a global variable.

    Within the function call lineid as global $lineid

    function POSTALLNAMES(){
       global $lineid; //this should take the correct value of $lineid
       //rest of the code here ......
    }
    

    I think that should work correctly :)

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么