I currently have a form where I can subscribe and unsubscribe users from newsletters wich looks like this:
The code for the bottom part (the subscriptions part) is this:
<?php
$i = 0;
while($objResult1 = mysql_fetch_array($objQuery1))
{
$i++;
?>
<tr>
<td><div align="center"><?=$objResult1["ID"];?><input type="hidden" name="mailid" value="<?=$objResult1["ID"];?>"> </div></td>
<td><div align="center"><?=$objResult1["Titel"];?> </div></td>
<td><div align="center"><input type="checkbox" name="sub" value="10"> </div></td>
<td><div align="center"><input type="checkbox" name="sub" value="90"> </div></td>
</tr>
<?php
}
?>
And the code to insert the values into the database is this:
<?php
mysql_connect('localhost','root','root');
mysql_select_db('NAW') or die (mysql_error());
$Klant_ID = $_POST['klantid'];
$Mail_ID = $_POST['mailid'];
$Status = $_POST['sub'];
$Datum = date("d-m-Y");
$sql = mysql_query(
"INSERT INTO Subscriptions (
Klant_ID,
Mail_ID,
Status,
Datum
) VALUES (
'".$Klant_ID."',
'".$Mail_ID."',
'".$Status."',
'".$Datum."'
)"
);
if ($sql === false) {
die (mysql_error());
} else {
echo 'Je gegevens zijn succesvol in de database geplaatst.<br><br>Om de gegevens uit de database te bekijken klik <A HREF="klanten.php">hier</A>. <br>Om meer gegevens in te voeren klik <A HREF="index.html">hier</A>.';
}
?>
The database is:
ID Klant_ID Mail_ID Status Datum
1 6 1 test
2 6 1 test test
3 10 10 10 19-03-13
16 6 4 90 20-03-2013
17 6 4 10 20-03-2013
18 7 4 90 20-03-2013
Now it will just add 1 row to the database where I checked a checkbox. I want to get it working so it will add 3 rows to the database, for example: When I select the boxes like this:
I want these rows to be added to the database:
ID Klant_ID Mail_ID Status Datum
20 6 1 10 20-03-2013
21 6 2 90 20-03-2013
22 6 4 10 20-03-2013
So my question is if this is posible using a while loop with mysql_fetch_array? I am not a pro in php cause I'm still a student and I can't solve this problem on my own. I hope my question is clear enough but if you have any question just ask in the comments^^ If anyone can show me how it is done or push me in the right direction it would be great!
(I know I shouldn't be using mysql_* anymore but that is not the point here^^)
Change:
<input type="checkbox" name="sub" value="10">
<input type="hidden" name="mailid" value="<?=$objResult1["ID"];?>">
to:
<input type="checkbox" name="sub[]" value="10">
<input type="hidden" name="mailid[]" value="<?=$objResult1["ID"];?>">
and do in php:
$sql = mysql_query("INSERT INTO Subscriptions (Klant_ID, Mail_ID, Status, Datum) VALUES ('".$Klant_ID."', '".$Mail_ID."', '".$Status."', '".$Datum."')") or die (mysql_error());
to:
foreach($_POST['sub'] as $i=>$s){
$sql = mysql_query("INSERT INTO Subscriptions (Klant_ID, Mail_ID, Status, Datum) VALUES ('".$Klant_ID."', '".$_POST['mailid'][$i]."', '".$s."', '".$Datum."')") or die(mysql_error());
}
NOTE: This is solution is WITHOUT injection and other checks , and created just to show you direction to think.
You can just loop through the $_POST['sub'] and do your insert in that loop.
First, you have to make sub an array, so replace name="sub"
with name="sub[]"
Now you can easily loop through the checked values.
foreach($_POST['sub'] as $subscription){
mysql_query("INSERT INTO Subscriptions (Klant_ID, Mail_ID, Status, Datum) VALUES ('".$Klant_ID."', '".$Mail_ID."', '".$subscription."', '".$Datum."')") or die (mysql_error());
}
I would advice you not to use mysql_ functions, as they are deprecated. Look into MySQLi or PDO (my favorite) instead. :)
With PDO, you could just prepare the query before the loop, and then execute it inside the loop. That way, PHP only have to prepare the query once.
$insert = $db->prepare("INSERT INTO Subscriptions (Klant_ID, Mail_ID, Status, Datum) VALUES (?, ?, ?, ?)")
foreach($_POST['sub'] as $subscription){
$db->execute(array($Klant_ID, $Mail_ID, $subscription, $Datum));
}
That way, your script would also be injection-safe, as with your current query, it is very vulnerable for injections.