如何使用php将多个输入文本字段值添加到db中

时间:2014-03-17 08:55:23

标签: php mysql

如何将多个textfields值插入mysqldb?

<?php
      if(isset($_POST['save'])){

    foreach($_POST['color'] as $key => $value)
    {
    if($color!="")
    {
        $color = $value['code'];
        echo $color;
        $rgb = hex2rgba($color);
        $rgba = hex2rgba($color, 0.7);
        /*echo $rgba;
        echo $value['key'];
        echo $value['code'];
        echo $value['order_color'];*/
        $sql = "INSERT INTO tbl_colors values('','$rgba','".$value['key']."','".$value['code']."','".$value['order_color']."')";
        $rs = mysql_query($sql) or die("Bad Query==><br><br>$sql<br><br>".mysql_error());
     }
}
    ?>


     <form action="" method="post" name="recommend">
    <tr>
    <td><input type="text" value="" name="color[][key]" class="email" style="width:450px;font-   size:15px;font-weight:bold;"></td>
    td><input type="text"  value="" name="color[][code]" class="email"></td>
    <td><input type="text"  value="" name="color[][order_color]" class="email"     style="width:50px;"></td>
    </tr>
    <tr>
    <td colspan="5">&nbsp;</td>
   </tr>
   <tr>
    <td><input type="text" value="" name="color[][key]" class="email" style="width:450px;font-   size:15px;font-weight:bold;"></td>
    td><input type="text"  value="" name="color[][code]" class="email"></td>
    <td><input type="text"  value="" name="color[][order_color]" class="email"     style="width:50px;"></td>
    </tr>
    <tr>
    <td colspan="5">&nbsp;</td>
   </tr>

     <tr>
     <td colspan="5" align="center"><input type="submit" name="save" value="Add Color" /></td>
    </tr>
     </form>

2 个答案:

答案 0 :(得分:0)

Row1,Row2,Row3是您需要将它们命名为表格行的表格行。

<?php

      if(isset($_POST['colorkey'])){
            $colorkey = $_POST['colorkey'];
                    $colorcode = $_POST['colorkcode'];
                    $colorcode = $_POST['colororder'];          



        $sql = ("INSERT INTO tbl_colors (Row1, Row2, Row3) VALUES ('$colorkey', '$colorcode', '$colorcode')");

}
    ?>


     <form action="" method="post" name="recommend">
    <tr>
    <td><input type="text" value="" name="colorkey" class="email" style="width:450px;font-   size:15px;font-weight:bold;"></td>
    td><input type="text"  value="" name="colorcode" class="email"></td>
    <td><input type="text"  value="" name="colororder" class="email"     style="width:50px;"></td>
    </tr>
    <tr>
    <td colspan="5">&nbsp;</td>
   </tr>
   <tr>
    <td><input type="text" value="" name="colorkey" class="email" style="width:450px;font-   size:15px;font-weight:bold;"></td>
    td><input type="text"  value="" name="colorcode" class="email"></td>
    <td><input type="text"  value="" name="colororder" class="email"     style="width:50px;"></td>
    </tr>
    <tr>
    <td colspan="5">&nbsp;</td>
   </tr>

     <tr>
     <td colspan="5" align="center"><input type="submit" name="save" value="Add Color" /></td>
    </tr>
     </form>

答案 1 :(得分:0)

{
  <?php
      if(isset($_POST['save'])){

    foreach($_POST['color'] as $key => $value)
    {

        $color = $value['code'];
        echo $color;
        $rgb = hex2rgba($color);
        $rgba = hex2rgba($color, 0.7);
        /*echo $rgba;
        echo $value['key'];
        echo $value['code'];
        echo $value['order_color'];*/
        $sql = "INSERT INTO tbl_colors values('','$rgba','".$value['key']."','".$value['code']."','".$value['order_color']."')";
        $rs = mysql_query($sql) or die("Bad Query==><br><br>$sql<br><br>".mysql_error());
     }
}
    ?>


     <form action="" method="post" name="recommend">
    <tr>
    <td><input type="text" value="" name="color[1][key]" class="email" style="width:450px;font-   size:15px;font-weight:bold;"></td>
    td><input type="text"  value="" name="color[1][code]" class="email"></td>
    <td><input type="text"  value="" name="color[1][order_color]" class="email"     style="width:50px;"></td>
    </tr>
    <tr>
    <td colspan="5">&nbsp;</td>
   </tr>
   <tr>
    <td><input type="text" value="" name="color[2][key]" class="email" style="width:450px;font-   size:15px;font-weight:bold;"></td>
    td><input type="text"  value="" name="color[2][code]" class="email"></td>
    <td><input type="text"  value="" name="color[2][order_color]" class="email"     style="width:50px;"></td>
    </tr>
    <tr>
    <td colspan="5">&nbsp;</td>
   </tr>

     <tr>
     <td colspan="5" align="center"><input type="submit" name="save" value="Add Color" /></td>
    </tr>
     </form>

}