Forgive me for I am relatively new to PHP and MYSQL and I believe this maybe be an easy question to answer(or maybe not, i'm not sure). Here is my HTML form of checkboxes
<form method="post" action="process.php">
<input type="checkbox" name="athlete[]" value="1">athlete 1
<br>
<input type="checkbox" name="athlete[]" value="2">athlete 2
<br>
<input type="checkbox" name="athlete[]" value="3">athlete 3
<br><br>
<input type="submit" value="Submit">
</form>
Fairly simple. Then I have this PHP in process.php:
$checkboxes = isset($_POST['athlete']) ? $_POST['athlete'] : array();
foreach($checkboxes as $value) {
$sql = "INSERT INTO draftPick(user_id, athlete_id)VALUES('77', '$value' )";
}
if(mysqli_query($conn,$sql)) {
echo 'Data added sucessfully';
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
I am attempting to make a foreach loop with my checkboxes and then insert each checkbox value into a new row in my MYSQL table. However when I run this PHP, it only inserts the last checked checkbox value into the MYSQLtable and does not insert all. How can I loop it so it will insert all checked values into my table. Thank you for the help!