PHP how to submit array elements to database
Question
I made a list of checkboxes that upon click get sent to a database. However, the only values that show in the database are 0. For example, a person can click 2 checkboxes, but the values sent to the database are 0 and 0. How can I submit the real values of the elements to the database?
Here is my code:
<form method="post">
<?php
global $wpdb;
$Result = $wpdb->get_results("SELECT Product1 FROM A");
foreach($Result as $Thing1) {
echo '<input type="checkbox" name="Num1[]" />' . $Thing1->Product1 . "<br />";
}
$Age = $_POST['Num1'];
if($_POST['btn']) {
if(!empty($Age)) {
foreach($Age as $id) {
$Word = $id;
// does not matter if checkbox value is 1 or 4, still goes as 0 to db
$wpdb->query("INSERT INTO A(Product1) VALUES('$Word');");
}
}
}
?>
<input type="submit" name="btn" />
</form>
0
1 month
2023-04-27T10:07:16-05:00
2023-04-27T10:07:16-05:00 0 Answers
0 views
0
Leave an answer