I am trying to add this array into the database correctly. I have 3 input boxes and when I echo $key all my info is there like this Jonesbass23 but when I run code each field gets a new row in the database it doesn't all go into 1 row and the values are going into the wrong columns as well. Also will add my $_POST array dynamically changes based on how many input fields the user wants so my database entries could be multiple. Thought I had a handle on it but guess not. Any help would be appreciated thank you.
<?php
session_start();
$errmsg_arr = array();
$errflag = false;
require_once 'dbconfig.php'; // Database Connection
//Retrieve data input from addfish.php
$username = $_SESSION['username'];
print_r($_POST);
}
//Returns error message text on screen
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: addfish.php");
exit();
}
//Insert data into database
$statement = $dbc->prepare("INSERT INTO entries(username, species, length, weight)
VALUES(:username, :species, :length, :weight)");
foreach($_POST as $key => $data) {
echo $key;
echo $data['species']; // The data of species1, species2, or species3, etc.
echo $data['length'];
echo $data['weight'];
try {
$statement->execute(array(
"username" => "$username",
"species" => $data['species'],
"length" => $data['length'],
"weight" => $data['weight']
));
}
catch(PDOException $e) {
echo "Exception caught: $e";
}
}
?>