【问题标题】:Input variable number of values from html-form in sql-table从 sql-table 中的 html-form 输入可变数量的值
【发布时间】:2017-01-23 10:16:51
【问题描述】:

我有一个带有五个输入字段的 html 表单,可以在其中输入一个或多个产品特征。输入的值应发送到 SQL 表。当所有 5 个字段都包含值时,这适用于 for 循环。

    <input name="productmaingroupsinput[1]" size="80"/><br>
    <input name="productmaingroupsinput[2]" size="80"/><br>
    <input name="productmaingroupsinput[3]" size="80"/>
    <input name="productmaingroupsinput[4]" size="80"/>
    <input name="productmaingroupsinput[5]" size="80"/>

     $productmaingroupsinput  = $_POST["productmaingroupsinput "];

     for ($i=1; $i<6; $i++) {
     $sql="INSERT INTO productmaingroup (productmaingroup_id, product_id, 
     productmaingroups) VALUES ('', (SELECT master_id from producttable 
     WHERE productname='$ productname '), '".productmaingroupsinput 
     [$i]."')";

      mysqli_query($db, $sql);
          }

但是,当较少的字段填充条目时,我的 SQL 表中会出现空条目。现在,我想避免 SQL 表中的空字段。我试过了

     if (isset($_POST["productmaingroupsinput[$k]"])) {
                       $j = $k + 1;
             }

    $j = count($productmaingroupsinput);



     for ($i=1; $i<$j; $i++) {
     $sql="INSERT INTO productmaingroup (productmaingroup_id, product_id, 
     productmaingroups) VALUES ('', (SELECT master_id from producttable 
     WHERE productname='$ productname '), '".productmaingroupsinput 
     [$i]."')";

但它没有用。

如何使 for 循环动态化,以便仅将输入的值发送到 SQL 表?

谢谢!

【问题讨论】:

    标签: php html sql for-loop


    【解决方案1】:

    你必须循环 productmaingroupsinput 帖子值并检查是否为空。做这样的事情

        <?php
    
        foreach($_POST['productmaingroupsinput'] as $row)
        {
    
            if($row!='')
            {
                $sql="INSERT INTO productmaingroup ( product_id, 
             productmaingroups) VALUES ( (SELECT master_id from producttable 
             WHERE productname='$productname'), '".$row."')";
    
                mysqli_query($db, $sql);
    
            }
            else
            {
    
                echo "value empty";
            }
    
    
        }
    
    
        ?>
    

    【讨论】:

    • 您的情况if($row!='' &amp;&amp; $row!=NULL &amp;&amp; !empty($row)) 太重且错误。来自 html-forms 的所有值都作为文本传输。这就是为什么条件$row!='' 就足够了。但是如果我在表单中提交数字0,条件!empty($row) 将失败。
    • 非常感谢您的建议@KostyaZhevlakov
    • 难道没有一个简单的解决方案,在 for 循环中找到 $j 的动态值 ($i=1; $i
    【解决方案2】:

    这是我的解决方案:

    $master = mysqli_query($db, "SELECT master_id from producttable WHERE productname='{$productname}'");
    $master_id = $master ? mysqli_fetch_field($master) : 'NULL';
    foreach($_POST['productmaingroupsinput'] as $group) {
        if($group=='') {
            continue;
        }
        $groups[] = "('', {$master_id}, '".mysqli_real_escape_string($group)."')";
    }
    $result = mysqli_query($db, "INSERT INTO productmaingroup (productmaingroup_id, product_id, productmaingroups) VALUES ".implode(', ', $groups));
    

    【讨论】:

      【解决方案3】:

      你应该检查值是否为空。像 if($_POST["productmaingroupsinput"][$i]!=NULL) 然后只执行那个查询。我希望这对你有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 2015-06-04
        • 2022-12-05
        相关资源
        最近更新 更多