duanchen9594 2015-03-10 02:53
浏览 51
已采纳

PHP函数将随机值存储到数据库中

I have been looking the solution for long time that why does my added fields are not stored in database properly and concluded that jquery is not adding html field properly that is why it is not submitted to render php program to add the fields data in database.

for proper demonstration please have a look at the code

My HTML form code is written as

<table id="additional_item_numbers">
<thead>
<tr>
<th>Item</th>
<th>&nbsp;Delete</th>
</tr>
</thead>

<tbody>
<tr><td><input type="text" class="form-control form-inps" size="50" name="additional_item_numbers[]" value="123" /></td><td>
<a class="delete_item_number" href="javascript:void(0);">Delete</a>
</td><tr>
</tbody>
</table>

and jquery function is defined to add a new field in form on ENTER key press

<script type="text/javascript" language="javascript">
$("#additional_item_numbers").keyup(function(event){
    if(event.keyCode == 13){
        $("#additional_item_numbers tbody").append('<tr><td><input type="text" class="form-control form-inps" size="40" name="additional_item_numbers[]" value="" /></td><td>&nbsp;</td></tr>');
            //document.getElementById("form-inps").focus();
            $("#additional_item_numbers").find("input[type='text']").last().focus();
    }
});
</script>

This function perfectly add a new field in the html form but upon submission of this form the fields are added into database randomly. sometimes not a single value is added to database and some time random values are added and some time all values are added upon submission. If I check the source code of the page after adding new fields with jquery function. it does not show those newly added fields in source code. I think it is due to client side functionality but it should add all the values to database after submission. but it does not. and if I go to edit an item and add some further additional item numbers then the same save() function of php is called and it does not miss a single value of the submitted form from previous item numbers but the additional added item numbers behave same as described above.

After searching a lot and diving deep in php code, I think that jquery is not setting the name of newly added fields properly, so those fields data whose name is set up properly is submitted to php function and it stores that data into database but those whose name was not set properly does not go to php program so they don't appear in database.

For better understanding, I am including the php function code too.

My PHP function to add data into database.

function save($item_id, $additional_item_numbers)
    {
        $this->db->trans_start();

        $this->db->delete('additional_item_numbers', array('item_id' => $item_id));

        foreach($additional_item_numbers as $item_number)
        {
            if ($item_number!='')
            {
                $this->db->insert('additional_item_numbers', array('item_id' => $item_id, 'item_number' => $item_number));
            }
        }

        $this->db->trans_complete();

        return TRUE;
    }

Any help would be much appreciated.

  • 写回答

5条回答 默认 最新

  • dongzi1959 2015-03-11 07:31
    关注

    After spending a lot of time I got the root point of the problem, It was a database error because database table was defined not to store a duplicate value and each time, I was placing a duplicate value, it was not stored in the database, this was the cause of random behavior of the program. when I providing unique values, It was updating the database properly and whenever there was a duplicate in the values, then after duplication value, it was not storing further values into database. To get it work, just change the structure of your database or

    in my case: My requirement was, no duplication but all the values besides the duplicate values should be stored in the database and I achieved this by changing

    $this->db->insert('additional_item_numbers', array('item_id' => $item_id, 'item_number' => $item_number));
    

    to

    $this->db->query("INSERT IGNORE INTO additional_item_numbers (item_id, item_number) VALUES ('".$item_id."', '".$item_number."')");
    

    Thanks all for your answers. Thumbs up :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。