php - Dynamically insert into MySQL table based on field value

I am using the following code snippet to insert a record into another table in the same database
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
$data["Division"] = $values['Division'];
DB::Insert("Training", $data );
I have multiple tables I want to insert this data into, so I was wondering if there is a way, and how to accomplish it, of telling the code based on the value entered into `Division' field, which table to insert the data into?
For example:
- If 'Training' is entered into the 'Division' field > enter data into 'Training' table.
- If 'Novice A' is entered into the 'Division' field > enter data into 'Novice - A' table.
- If 'BN A' is entered into the 'Division' field > enter data int 'BN - A' table
- etc...
I'm not sure if this is possible or not, but if it is, could someone please give me some direction on how to possibly accomplish this please?
if-elseif code I used:
// Insert records into Division tables //
if($data["Division"] == $values['Training']){
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
DB::Insert("Training", $data );
} elseif($data["Division"] = $values['Novice A']){
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
DB::Insert("Novice - A", $data );
} elseif($data["Division"] = $values['Novice B']){
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
DB::Insert("Novice - B", $data );
} elseif($data["Division"] = 'BN A'){
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
DB::Insert("BN - A", $data );
} elseif($data["Division"] = 'BN B'){
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
DB::Insert("BN - B", $data );
} elseif($data["Division"] = 'Starter A'){
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
DB::Insert("Starter - A", $data );
} elseif($data["Division"] = 'Starter B'){
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
DB::Insert("Starter - B", $data );
} elseif($data["Division"] = 'Starter C'){
$data = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
DB::Insert("Starter - C", $data );
}
return true;
Answer
Solution:
For your elsif statements, all the elsif statements should use == instead of = as comparison operators (we are not doing value assignment, right ?)
For example:
elseif($data["Division"] = 'Starter B')
should be
elseif($data["Division"] == 'Starter B')
Please change all of them.
However, as @esqew has said, please normalize your database instead of using the current "split tables" design.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mark bundle as not supporting multiuse
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.