I have some html form like that:
...
<input type="text" id="color" name="color" value="" placeholder="Color" />
<input type="text" name="size" id="size" value="" placeholder="Size" />
...
During form submissions, I'm checking if above inputs' values are separated by commas.
If yes, then doing something like that:
Lets say user entered, 1 color: black and multiple sizes: 42, 43, 44. The code will insert 3x time with different sizes but all other values will be same
Lets say user entered, multiple colors: black, yellow and 1 size: 42. The code will insert 2x time with different colors but all other values will be same
Question
What if user entered multiple colors and multiple sizes. Something like, colors: black, orange, blue and sizes:45,46,48. The code must insert 9 x time: black - 45, black - 46... blue-48. How can I do it?
Here is PHP code:
public function add() {
if ($_POST['formID'] == 'add_form') {
$stmt = $this->db->prepare("INSERT INTO `items`
(`refno`, `color`, `size`, `qt`, `stackno`, `notes`, `price`, `add_date`)
VALUES (?, ?, ?, ?, ?, ?, ?, NOW())") or die($db->error);
$count = array();
$size_arr = explode(",", $_POST['size']);
$color_arr = explode(",", $_POST['color']);
$count['size'] = count($size_arr);
$count['color'] = count($color_arr);
if ($count['color'] > 1) {
foreach ($color_arr as $color) {
$stmt->bind_param("ssiiisi", $_POST['refno'], $color, $_POST['size'], $_POST['qt'], $_POST['stackno'], $_POST['notes'], $_POST['price']) or die($stmt->error);
$stmt->execute() or die($stmt->error);
}
}
if ($count['size'] > 1) {
foreach ($size_arr as $size) {
$stmt->bind_param("ssiiisi", $_POST['refno'], $_POST['color'], $size, $_POST['qt'], $_POST['stackno'], $_POST['notes'], $_POST['price']) or die($stmt->error);
$stmt->execute() or die($stmt->error);
}
} else {
$stmt->bind_param("ssiiisi", $_POST['refno'], $_POST['color'], $_POST['size'], $_POST['qt'], $_POST['stackno'], $_POST['notes'], $_POST['price']) or die($stmt->error);
$stmt->execute() or die($stmt->error);
}
$stmt->close();
$this->ajax->respond("nb", "Successfuly added", 1);
}
}