koc
koc

Reputation: 955

How to make array for add multiple row in a sql table by same form

I want to add multiple row in a table by a form.

I know how to a make only one data array but in case of all name array, all detail array, all contact array I can't do this.

I also read here and google many article but I failed to make any result.

So please help me to create it?

So my from is:

<form action="" method="POST">
Name:    <input type="text" name="name[]" value="">
Detail:  <input type="text" name="detail[]" value="">
Address: <input type="text" name="add[]" value="">
<br />
Name:    <input type="text" name="name[]" value="">
Detail:  <input type="text" name="detail[]" value="">
Address: <input type="text" name="add[]" value="">
<br />
Name:    <input type="text" name="name[]" value="">
Detail:  <input type="text" name="detail[]" value="">
Address: <input type="text" name="add[]" value="">
<input type="submit" name="submit" id="submit" value="Submit">

And my php for insert at sql

include("../db.php");
global $dbh;
if(isset($_POST['submit'])){
$data = array(); // now how to do all single post array?
$name = mysqli_real_escape_string($_POST['name']);
$detail = mysqli_real_escape_string($_POST['detail']);
$add = mysqli_real_escape_string($_POST['add']);

$result=mysqli_query($dbh,"INSERT INTO varify (id,name,detail,add) VALUES('','$name','$detail','$add')");

Upvotes: 3

Views: 2021

Answers (4)

Kevin
Kevin

Reputation: 41903

Of course first off you need to loop them, you can use foreach. Then you can use the key inside the foreach to access the other lined up indices on the form:

Rough example:

if(isset($_POST['submit'])) {

    foreach($_POST['name'] as $k => $name) {
        $name = mysqli_real_escape_string($dbh, $name);
        $detail = mysqli_real_escape_string($dbh, $_POST['detail'][$k]);
        $add = mysqli_real_escape_string($dbh, $_POST['add'][$k]);

        // continue insertion
    }
}

Or with using prepared statements:

if(isset($_POST['submit'])) {
    foreach($_POST['name'] as $k => $name) {
        $insert = $db->prepare('INSERT INTO varify (id, name, detail, add) VALUES('', ?, ?, ?)');
        $insert->bind_value('sss', $name, $_POST['detail'][$k], $_POST['add'][$k]);
        $insert->execute();
    }
}

Another alternative would be to create the grouping structure inside your html markup form.

Use the keys inside the name grouping to group them. So the markup would look something like this:

<form action="" method="POST">
Name:    <input type="text" name="input[0][name]" value="">
Detail:  <input type="text" name="input[0][detail]" value="">
Address: <input type="text" name="input[0][add]" value="">
<br />
Name:    <input type="text" name="input[1][name]" value="">
Detail:  <input type="text" name="input[1][detail]" value="">
Address: <input type="text" name="input[1][add]" value="">
<br />
Name:    <input type="text" name="input[2][name]" value="">
Detail:  <input type="text" name="input[2][detail]" value="">
Address: <input type="text" name="input[2][add]" value="">
<br />
<input type="submit" name="submit" id="submit" value="Submit">

When you handle it in PHP, now they are grouped by row and can be accessed like this:

<?php

if(isset($_POST['submit'])) {
    $input = $_POST['input'];

    foreach($input as $values) {
        if(!empty($values['name']) && !empty($values['detail']) && !empty($values['add'])) {
            $insert = $db->prepare('INSERT INTO varify (id, name, detail, add) VALUES('', ?, ?, ?)');
            $insert->bind_value('sss', $values['name'], $values['detail'], $values['add']);
            $insert->execute();
        }   
    }
}

Upvotes: 0

Md. Sahadat Hossain
Md. Sahadat Hossain

Reputation: 3236

make an array using foreach loop

$data = '(';
foreach($_POST['name'] as $key => $name){ //here use a mandatory field as foreach paremeter
   $data .= "'".mysqli_real_escape_string($name)."'", "'".mysqli_real_escape_string($_POST['detail'][$key])."'", "'".mysqli_real_escape_string($_POST['add'][$key])."'";
   $data .= '),';
}
$data = rtirm($data, ',');
$result=mysqli_query($dbh,"INSERT INTO varify (name, detail, add) VALUES". $data);

Upvotes: 0

NaijaProgrammer
NaijaProgrammer

Reputation: 2967

Replace your code with this:

include("../db.php");
global $dbh;
if(isset($_POST['submit'])){
   $data = array(); // now how to do all single post array?
   $names = $_POST['name'];
   $details = $_POST['detail'];
   $adds    = $_POST['add'];

   for($i = 0; $i < count($names); $i++)

      $curr_name = mysqli_real_escape_string($names[$i]);
      $curr_detail = mysqli_real_escape_string($details[$i]);
      $curr_add = mysqli_real_escape_string($adds[$i]);

      $result=mysqli_query($dbh,"INSERT INTO varify (id,name,detail,add) VALUES('','$curr_name','$curr_detail','$curr_add')");
   }

}

If your id field is an auto-increment field, I suggest you don't manually insert the id, especially as you are passing an empty string. Instead, replace the insert code with this:

$result=mysqli_query($dbh,"INSERT INTO varify (name,detail,add) VALUES('$curr_name','$curr_detail','$curr_add')");

Upvotes: 1

Manoj Dhiman
Manoj Dhiman

Reputation: 5166

use like this

   <?
if(isset($_POST['submit'])){
        $1=0;
        foreach($_POST['name'] as $key=>$inp)
        {
        $name = mysqli_real_escape_string($_POST['name'][$i]);
        $detail = mysqli_real_escape_string($_POST['detail'][$i]);
        $add = mysqli_real_escape_string($_POST['add'][$i]);
        $result=mysqli_query($dbh,"INSERT INTO varify (id,name,detail,add) VALUES('','".$name."','".$detail."','".$add."')");

        }
        $1++;
}
        ?>

Upvotes: 0

Related Questions