I know that this question has been already asked here, but I went through 20+ and couldn't get below code to work
<form action="" method="post" >
<td><input name="field1[]" value="" ></td>
<td><input name="field2[]" value="" ></td>
<td><input name="field3[]" value="" ></td>
<td><input name="field1[]" value=""></td>
<td><input name="field2[]" value=""></td>
<td><input name="field3[]" value=""></td>
<td><input name="field1[]" value=""></td>
<td><input name="field2[]" value=""></td>
<td><input name="field3[]" value=""></td>
<button type="submit" id="submit" name="submit" > Go </button>
</form>
<?php
if(isset($_POST["submit"])){
foreach($_POST['field1'] as $key => $value) {
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "dbuser", "dbpass", "dbname");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Prepare an insert statement
$sql = "INSERT INTO test (field1, field2, field3) VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $field1, $field2, $field3);
// Set parameters
$field1 = $_REQUEST['field1'];
$field2 = $_REQUEST['field2'];
$field3 = $_REQUEST['field3'];
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
}
} else{
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
}
}
?>
My problem is clear - how to store this array to database. I am doing something wrong because my DB stores Array word instead of value. Any help would be massively appreciated