duanjiuhong5843 2012-08-21 22:03
浏览 43

从动态jquery表单字段向mysql添加行

please excuse me as I am not proficient in php/mysql. I have an html form for inventory. I have a set of fields to update my sql database, they are item_name, item_cost, item_quantity. I have a jquery button that allows the user to dynamically add additional items/rows.

Question: how can I have php loop whatever number of items the user adds and put them in the mysql database?

thank you for your time.

  • 写回答

2条回答 默认 最新

  • doujianwan7570 2012-08-21 22:07
    关注

    If you have multiple form inputs with the same name, and that name ends in double square brackets [], their values will be turned into an array when PHP populates $_POST from the form.

    So your jQuery button should insert a row with fields named like this:

    <input type="text" name="item_name[]" value="" />
    <input type="text" name="item_cost[]" value="" />
    <input type="text" name="item_quantity[]" value="" />
    

    In your PHP code that takes the form submission, you can process all the rows that exist like this:

    //I used `item_name` as the loop termination condition, 
    //but any of the 3 keys would have worked
    for ($i = 0; $i < count($_POST['item_name']); $i++) {
        $item_name = $_POST['item_name'][$i];
        $item_cost = $_POST['item_cost'][$i];
        $item_quantity = $_POST['item_quantity'][$i];
    
        //here, inside the loop, run your database query using the 3 values above    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?