How to add multiple Checkbox value to database
I have a database named "student1905" where there is a table named
"programmata" where there is a column named "Periorismoi".
I have a page where there are some checkboxes. All i want to do is add the
value of the TICKED (selected) checkboxes into the column "Periorismoi"
with a "," between them. I am thinking of a function where variable named
"ids" will take all the values. And then ids will be added in the column.
But something is wrong and I cannot find it although I am googling like
hell. Here is a part of the code from the page with the checkboxes :
<form action="Insert4.php" method="POST" periorismoi="periorismoi">
<p>
<input type="checkbox" name="periorismoi[]" value="TRALALALA">ÐñïâëÞìáôá
¼ñáóçò<br>
<input type="checkbox" name="periorismoi[]" value="2">ÐñïâëÞìáôá ÁêïÞò<br>
<input type="checkbox" name="periorismoi[]" value="3">ÊéíçôéêÜ ÐñïâëÞìáôá
<br>
<input type="checkbox" name="periorismoi[]" value="4">ÓïâáñÝò êéíçôéêÝò
äéáôáñá÷Ýò (óå áíáðçñéêü êáñïôóÜêé)<br>
<input type="checkbox" name="periorismoi[]" value="5">ÊáôÜèëéøç<br>
<input type="checkbox" name="periorismoi[]" value="6">¢ã÷ïò<br>
<input type="checkbox" name="periorismoi[]" value="7">Äåí ìðïñåß íá ãñÜöåé
êáé íá äéáâÜæåé
</p>
<p align="center"><input type="submit" name="Submit" value="ÐÑÏÓÈÇÊÇ"></p>
<p align="left"> <br></p>
</form>
</body>
and here is the code from the Insert (to database) page I had at the
beginning which is wrong because it is putting at the database the
checkbox number:
$link = mysql_connect('localhost', 'student1905','123456');
mysql_select_db('student1905');
if(!$link)
{
die('Could not connect: ' . mysql_error());
}
else {
$periorismoi = $_POST['periorismoi'];
for($i=0; $i<sizeof($periorismoi);$i++){
$sql="INSERT INTO programmata (Auxon,Title,Periorismoi)
VALUES ('','$title','$periorismoi[$i]')";
mysql_query($sql);
}
header("Location:dokimastiko.php"); ;
}
?>
I tried to do that but it didnt work:
$i = 0;
$ids = "";
$first = true;
while(isset($_POST['periorismos[$i]']))
{
if($first) {
$first = false;
} else {
$ids += ","
}
$ids += $_POST['periorismos[$i]'];
$i++;
}
$link = mysql_connect('localhost', 'student1905','123456');
mysql_select_db('student1905');
if(!$link)
{
die('Could not connect: ' . mysql_error());
}
else {
mysql_query("SET NAMES utf8");
$sql = "INSERT INTO programmata (Auxon, Title, Periorismoi) VALUE
('','$title','$ids')";
mysql_query($sql);
header("Location:dokimastiko.php"); ;
}
No comments:
Post a Comment