doushan1863 2013-12-27 01:50
浏览 73
已采纳

从动态创建的输入字段插入适当的数据

I need to insert data from below inputs to database. Whole <div class="clone"> can be cloned with jquery and it also adds +1 to names, for example next created div will have inputs named name="npm[]1", name="apm[]1" etc.
<div class="list" id="sub"> part list subcategories of shops that can be checked (shop can have multiple subcategories) and they also get +1 on names. Is it ok like that?

<div class="clone">
    <label>Shop name</label><input type="text" name="npm[]0" id="npm" required>
    <label>Address</label><input type="text" name="apm[]0" id="apm" required>
    <label>City</label><input type="text" name="mipm[]0" id="mipm" required>
    <label>Working hours</label><input type="text" name="rv[]0" id="rv" required>
    <label>Tel</label><input type="text" name="ktf[]0" id="ktf" required>
    <label>Mob</label><input type="text" name="ktm[]0" id="ktm" required>
    <label>E-mail</label><input type="text" name="kea[]0" id="kea" required>
    <label>Contact person</label><input type="text" name="ipko[]0" id="ipko" required>

    <a id="select" class="ctpct">Select shop subcategory</a>
      <div class="list" id="sub">
        <?php
         $a="select a.id,a.cat,b.x,b.subcat,b.cat_id from cat a
            inner join
                subcat b
                    on a.id = b.cat_id
                        where b.cat_id !=1
                            order by a.id";

         $res = mysql_query($a) or die(mysql_error());

         $cat = '';

         while ($re = mysql_fetch_array($res))
         {
          if( $cat === '' )
           {
             $cat = $re["cat"];
             echo "<div class='catdis'>";
             echo "<h2>$cat</h2>";
           }
          elseif( $cat !== $re["cat"] )
           {
             $cat = $re["cat"];
             echo "</div>";
             echo "<div class='catdis'>";
             echo "<h2>$cat</h2>";
           }

           $subcat= $re["subcat"];
           $id= $re["x"];

           echo "<input type='checkbox' name='subcat[]0' value='$id'> $subcat<br />";

         }

       ?>
     </div>
</div>

PHP part:

foreach ($_POST as $key => $value) {
$values = mysql_real_escape_string($value);

$query2 = "INSERT INTO shops(n, a, c, wh, tel, mob, mail, con) VALUES ('$values')";
$rs2=mysql_query($query2) or die(mysql_error());
}

Now, php part that i have prints this "Column count doesn't match value count at row 1", it may be because under that clone div i have few more inputs, normal ones.
I'am php newbie so can you please help me with this. I need it to insert data for every shop from input 'Shop name' to 'Contact person' in table 'shops' and selected subcategories in table 'shop_subcat' with subcategory_id and shop_id
Thank you!

  • 写回答

1条回答 默认 最新

  • duanchi6397 2013-12-27 01:57
    关注

    You should give the input fields names like name="apm[]". PHP treats names that end with [] specially, it turns all the inputs into an array. So you can do:

    foreach ($_POST['apm'] as $apm) {
        ...
    }
    

    to process all those inputs.

    Your name="apm[]0" doesn't follow this pattern, so it doesn't work.

    Also, remove the id attributes from your inputs. IDs are required to be unique, and when you clone the DIV you're creating duplicates.

    To solve the SQL error, you need to fix your query. You list 7 columns to insert into, so you have to provide 7 values.

    $input_count = count($_POST['npm']);
    for ($i = 0; $i < $input_count; $i++) {
        $n = mysql_real_escape_string($_POST['npm'][$i]);
        $a = mysql_real_escape_string($_POST['apm'][$i]);
        // Repeat above pattern for all the other inputs
        $query2 = "INSERT INTO shops(n, a, c, wh, tel, mob, mail, con) 
                   VALUES ('$n', '$a', '$c', '$wh', '$mob', '$mail', '$con')";
        $rs2=mysql_query($query2) or die(mysql_error());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了