0

How to insert multiple textfields value into 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>
12
  • Thanks for posting your code. Can you tell us what problem you are facing? Commented Mar 17, 2014 at 8:58
  • is there any error you are getting? Commented Mar 17, 2014 at 8:58
  • when echo $value['key']; only this field is added in db but others text field value not added so help me for that... other field take as blank field no error Commented Mar 17, 2014 at 9:00
  • A little bit out of the topic, but ALWAYS escape user input BEFORE database insertion! Either by mysql_real_escape_string() or better - use PDO prepared statement! Commented Mar 17, 2014 at 9:04
  • is ther qoutes in your data when you insert in DB Commented Mar 17, 2014 at 9:05

2 Answers 2

1
{
  <?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>

}

Sign up to request clarification or add additional context in comments.

2 Comments

What is this? If you need to add more code/information you can edit your question.
I found the one mistake at input text in my code and it's worked
1

Row1, Row2, Row3 are your table rows you need to name them to your table row.

<?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 Comment

try edited post. I deleted the if statement. Just to check if it actually works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.