Inserting Dynamic Form Values as Array in MYSQL Using Foreach

By | October 6, 2013
Store-Multiple-Array-in-MySQL

Store Multiple Array

Hello friends today i was working at some project and stuck when i was inserting dynamic form fields data in the database. So i have decided to store the values in array and then save them in database.

I have 3 fields with the same name and id so i declare the all text field name with name[]. But i dont have only one field like this i have 3 more fields which is similar with above. Let me explain it more clearly.

Our database has only 2 column 1. Product 2. Description

<form>
Enter Product name <input type="text" name"product">
Enter Description <input type="text" name"description">
Enter Product name <input type="text" name"product">
Enter Product name <input type="text" name"description">
Enter Product name <input type="text" name"product">
Enter Product name <input type="text" name"description">
</form>

In this situation i am having the two fields but they we are using them 3 times or more. Now i have to save the data into my db through php mysqli. According to our normal way we use to put $_POST[“product”] etc. But when i submit the form i got a problem that i got only last field value in my database. So my code works but not the way i want it to be. So change all the fields name with the new one.

<form>
Enter Product name <input type="text" name"product1">
Enter Description <input type="text" name"description1">
Enter Product name <input type="text" name"product2">
Enter Product name <input type="text" name"description2">
Enter Product name <input type="text" name"product3">
Enter Product name <input type="text" name"description3">
</form>

So now i have everything different and i use the $_POST[] for every single field and get the value in my all the variables. Wao lets save the data in database. I have all the data in my variable and i have to call the insert statement and that’s it. Stop we have to save the data in 3 rows with 2 column and we have 6 variable to save. We have to insert only 1 product and 1 description information in 1 row. And have to save all the data in 3 rows. This will make a big problem to use. Lets think what can do.

Yes you are right we can use the for loop to save the data. Thank god we saved our day.

Are you kidding me we have to save 30 Rows like this OMG how i will handle the form and their values !!!! this will make me crazy…….  :evil:.

So we have to think more deeply and find something better and easy one. Yes you are right arrays can do it. But how ?????.

So lets try to change our form and get all the values in array and than we will save it.

<form>
<input type="text" name"product[]">
<input type="text" name"description[]">
<input type="text" name"product[]">
<input type="text" name"description[]">
<input type="text" name"product[]">
<input type="text" name"description[]">
</form>

After changing the form we are having our name which is bit different but everything goes if our code works. So we will add the [] with the name of the field. Now we have our 6 textfield values in 2 arrays. Lets save them in our database. For saving the values in database we will use Foreach. 




We have 2 arrays so we cant use the traditional foreach way we will use MultipleIterator for saving the multiple array values.

First we will create the object of MultipleIterator Class.

$iterator = new MultipleIterator;

Now add our arrays to $iterator

$iterator ->attachIterator( new ArrayIterator( $product));
$iterator ->attachIterator( new ArrayIterator( $description));

Now we will use the foreach to save the data

foreach( $iter as $data) {
list( $product,$description) = $data;
$query = "INSERT INTO tablename (Product,Description) VALUES ('$product','$description')";
$result=mysqli_query($con,$query) or die(mysqli_error($con));
}

In the above code we create a list of our values and than one by one we will save our values in our database. Run the code you will get what you want in your database. Below is the whole code for saving the data in mysql.

$iterator = new MultipleIterator;
$iterator ->attachIterator( new ArrayIterator( $product));
$iterator ->attachIterator( new ArrayIterator( $description));
foreach( $iter as $data) {
list( $product,$description) = $data;
$query = "INSERT INTO tablename (Product,Description) VALUES ('$product','$description')";
$result=mysqli_query($con,$query) or die(mysqli_error($con));
}

So thats the end of the tutorial let me know if you face any problem with the above code please comment. I will be happy to help you.

 ~~~~ HAPPY CODING ~~~~

4 thoughts on “Inserting Dynamic Form Values as Array in MYSQL Using Foreach

  1. Zaheer

    this is my index.php code

    nad this is my postdatda.php code

    attachIterator( new ArrayIterator( $product));
    $iterator ->attachIterator( new ArrayIterator( $description));

    foreach( $iter as $data) {
    list( $product,$description) = $data;
    $query = “INSERT INTO testtable (product,discrip) VALUES (”,’$product’,’$description’)”;
    $result=mysqli_query($query);
    }

    ?>

    but y data not insert in to database table?

    Reply
  2. Divya

    I am a beginner of PHP . Kindly help us for the below error and guide me how to save the array of inputs to the database. Thanks in advance

    attachIterator( new ArrayIterator( $product));
    $iterator ->attachIterator( new ArrayIterator( $description));
    foreach( $iter as $data)
    {
    list( $product,$description) = $data;
    $query = “INSERT INTO product_sample (Product,Description) VALUES (‘$product’,’$description’)”;
    $result=mysqli_query($con,$query) or die(mysqli_error($con));
    }
    ?>

    I used like this but i m getting error like

    Fatal error: Uncaught exception ‘InvalidArgumentException’ with message ‘Passed variable is not an array or object, using empty array instead’ in C:\xampp\htdocs\Home\product.php:14 Stack trace: #0 C:\xampp\htdocs\Home\product.php(14): ArrayIterator->__construct(NULL) #1 {main} thrown in C:\xampp\htdocs\Home\product.php on line 14

    and value is not storing in the database.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *