我有一些复选框,值为1-10,该值位于名为“locationID”的列下。问题是如果我勾选例如2个盒子,它只选择输入两个盒子的最高值,我想为我勾选的每个盒子换一个新行。
我的数据库结构:
表名:事件
列:column1,column2 ..,locationID
复选框php代码示例:
<input class="checkboxarna" name="locationID" style="z-index: 1;position: relative;" type="checkbox" value="1">
PHP中的MySQL查询: http://pastebin.com/ri7LCib2
我在谷歌和这里搜索过,但是因为我目前不擅长PHP,所以我很难“理解别人的代码并将其改造为我工作”,希望你接受这一点。
答案 0 :(得分:3)
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
// Run Your Insert query here.
}
}
希望这有助于
答案 1 :(得分:1)
您需要将表单更改为:
<input type="checkbox" name="locationID[]" value="1" />A<br />
<input type="checkbox" name="locationID[]" value="2" />B<br />
<input type="checkbox" name="locationID[]" value="3" />C<br />
然后你的php需要一个循环来检查值:
$loc= $_POST['locationID'];
if ( empty( $loc ) ) {
echo("You didn't select any checkboxes");
} else {
for( $i = 0; $i < count($loc); $i++ ) {
//echo( $loc[$i] . " " ); //ideally insert into DB here
mysql_query("INSERT INTO event
(EventName, EventStart, EventEnd, FromDate, ToDate, locationID, EventValue, isActive, photo)
VALUES
('$_POST[namn]',
'$_POST[eventstart]',
'$_POST[eventend]',
'$_POST[startdate]',
'$_POST[enddate]',
'$loc[$i]',
'$AddValue',
'$isActive',
'$pic'
)
");
}
}
答案 2 :(得分:0)
$location_ids = $_POST['location_id'];
if(!empty($location_ids)){
foreach($location_ids as $location_id)
{
if($location_id == 'on' || $location_id)
{
mysql_query("INSERT INTO event (EventName, EventStart, EventEnd, FromDate,
ToDate, locationID, EventValue, isActive, photo)
VALUES
('$_POST[namn]','$_POST[eventstart]','$_POST[eventend]','$_POST[startdate]',
'$_POST[enddate]', '$_POST[locationID]','$AddValue','$isActive','$pic')");
/* Sending locationID to jointable */
mysql_query("INSERT INTO `jointable` VALUES('','".$_POST["locationID"]."')");
}
}
mysql_close($con);