I am making a survey and currently the database has 19 tables and roughly 100 columns. All of them will be inserted to as they are required fields. I am looking for an Elegant and Efficient way of inserting into that many tables and columns. So far what I have come up with is to create a multidimensional array that contains the first key as the table name and the second key as the column name with the field. Something like below:
$tableArray = array(
'ownerTable' => array(
'firstNameRow' => $firstName,
'lastNameRow' => $lastName
),
'dealerTable' => array(
'dealerNameRow' => $dealerName,
'dealerCityRow' => $dealerCity
)
);
foreach($tableArray as $row => $key) {
foreach($tableArray[$row] as $row1) {
$sql = "(INSERT INTO $tableArray[$key] ($tableArray[$row]) VALUES ($row1)";
}
}
I didn't test this code but I am thinking something along those lines would work. I think one problem I see with this is a separate INSERT
for each column instead of one INSERT
for each table. I can always work on writing the code to just load all the values from the array at once to solve this problem but before I start getting to carried away I want to make sure I am not making a big mistake and waste time if there is a better way to do it.
$tableArray = array(
'ownerTable' => array(
'firstNameRow' => $firstName,
'lastNameRow' => $lastName
),
'dealerTable' => array(
'dealerNameRow' => $dealerName,
'dealerCityRow' => $dealerCity
)
);
//it's for bulj query execution in single statment;
$i=1;
$sql="";
foreach($tableArray as $row => $key) {
foreach($tableArray[$row] as $row1) {
//here you can update i value as per row for execution
if($i<=25)
{
$sql = $sql + "(INSERT INTO $tableArray[$key] ($tableArray[$row]) VALUES ($row1);";
}
else
{
//execute sql statement here;
$i=0;
$sql="";
}
}
}