douyan1972 2015-08-09 02:37
浏览 113
已采纳

如何将动态生成的表单存储不同的字段值返回数据库php mysql?

I am beginner in php mysql and I have a problem below picture type of form how to save values of all rows or only selected row fields values:- name of fields are as :-

  foreach($data as $row){
 <tr>
  <td><input type="checkbox" value="$row['ProductID']" name="productID[]" /></td>
  <td>     <input type="text" value="$row['Quantity']" name="quantity[]" /></td>
 <td><input type="text" value="$row['Price']" name="price[]" /></td>
 </tr>
}

enter image description here

1. Question is How to get only selected row fields value in php or jquery I have no Idea how to get only selected row fields value in php

  • 写回答

1条回答 默认 最新

  • du8794 2015-08-09 05:01
    关注

    If you are trying to get a listing of all your items with a note whether selected or not, just change your form names a bit (If I understand what you are looking for...):

    <?php 
        $data[] =   array('ProductID'=>123,"Quantity"=>1,"Price"=>"2.00");
        $data[] =   array('ProductID'=>234,"Quantity"=>2,"Price"=>"1.50");
        $data[] =   array('ProductID'=>345,"Quantity"=>1,"Price"=>"4.59");
        $data[] =   array('ProductID'=>456,"Quantity"=>4,"Price"=>"1.99");
    
        foreach($data as $row){ ?>
        <tr>
            <td><input type="checkbox" name="product[<?php echo $row['ProductID'];?>][select]" /></td>
            <td><input type="text" value="<?php echo $row['Quantity']; ?>" name="product[<?php echo $row['ProductID'];?>][qty]" /></td>
            <td><input type="text" value="<?php echo $row['Price']; ?>" name="product[<?php echo $row['ProductID'];?>][price]" /></td>
        </tr>
    <?php }
    

    Gives you:

    // Just loop through the [product] array looking for the 'select' = 'on' 
    Array
    (
        [product] => Array
            (
                [123] => Array
                    (
                        [qty] => 1
                        [price] => 2.00
                    )
    
                [234] => Array
                    (
                        [select] => on
                        [qty] => 2
                        [price] => 1.50
                    )
    
                [345] => Array
                    (
                        [qty] => 1
                        [price] => 4.59
                    )
    
                [456] => Array
                    (
                        [qty] => 4
                        [price] => 1.99
                    )
            )
    )
    

    To get the selected items:

    if(!empty($_POST['product'])) {
        foreach($_POST['product'] as $row) {
                if(!empty($row['select'])) {
                        print_r($row);
                    }
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化