How can i post this type of inputs to php and insert him to mysql with mysqli
<tr>
<td>
<input type="text" name="fii[]">
<input type="text" name="foo[]">
<input type="text" name="faa[]">
</td>
<td>
<input type="text" name="fii[]">
<input type="text" name="foo[]">
<input type="text" name="faa[]">
</td>
</tr>
the structure of the table |fii|foo|faa|
Note:
1) Below is the logic, Try with your coding method and fields
2) Edited: It will be work perfectly with name="field1[]"
instead name="field1['.$i.']"
.
However If you want to edit the saved data, $i
can be replace with database id OR you need to pass an hidden input field with the database id <input type="hidden" name="database_id['.$unique_id.']">
in for loop. (is upto your logic and implementations)
Form Page
<form name="form_insert" action="your_action_page.php" method="post">
<table border="0">
<?php
/*
If dynamic via page load,
$total_rows = isset($_GET['total_rows']) ? $_GET['total_rows'] : 1;
*/
$total_rows = 2;
for ($i=1; $i<=$total_rows; $i++)
{
echo '<tr>
<td><input type="text" name="field1['.$i.']"></td>
<td><input type="text" name="field2['.$i.']"></td>
<td><input type="text" name="field3['.$i.']"></td>
</tr>
';
}
?>
</table>
<input type="submit" name="submit" value="Submit" />
</form>
your_action_page.php
<?php
if (isset($_POST))
{
$field1_array = isset($_POST['field1']) ? $_POST['field1'] : array();
$field2_array = isset($_POST['field2']) ? $_POST['field2'] : array();
$field3_array = isset($_POST['field3']) ? $_POST['field3'] : array();
$total_rows = count($field1_array);
if ($total_rows > 0)
{
for ($i=1; $i<=$total_rows; $i++)
{
$field1_val = $field1_array[$i];
$field2_val = $field2_array[$i];
$field3_val = $field3_array[$i];
$sql = "INSERT INTO your_table (first, second, third) VALUES ('".$field1_val."', '".$field2_val."', '".$field3_val."')";
$query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));
//if ($query)
//echo 'data inserted successfully';
}
}
}
?>
This is the dynamic form creation for your problem.Then you can use a same way to get these values in php file
<form action='action.php' method=post>
<tr>
<?php
for ($index=0 ;$index<10 ; $index++){
<?
<td>
<input type="text" name="fii_<?php echo $index ?>">
<input type="text" name="foo_<?php echo $index ?>">
<input type="text" name="faa_<?php echo $index ?>">
</td>
<?php
}
?>
<td>
</form>
Add an index
<tr>
<td>
<input type="text" name="fii[0]">
<input type="text" name="foo[0]">
<input type="text" name="faa[0]">
</td>
<td>
<input type="text" name="fii[1]">
<input type="text" name="foo[1]">
<input type="text" name="faa[1]">
</td>
</tr>
I like to be specific so I can see it on the client side and then see the result index for index on the server side
So like Dragon stated in another comment, you'll go through your $_GET['fii'] etc. and insert into your database
pseudo code:
while i < count($_GET['fii'])
insert into table $_GET['fii'][i], etc
increment i