我想将表单中的数据插入到数据库中,但它会提供大量错误通知。
Notice: Array to string conversion in C:\xampp\htdocs\thesis\admin\gov_sched_save.php on line 181
很多错误都会在不同的行中注意到。
这是表单结构structure
和表单的代码(只是我发现令人困惑的部分的片段):
<table class="table table-bordered table-striped" style="margin-right:-10px">
<thead>
<tr>
<th>Time</th>
<th>M</th>
<th>T</th>
<th>W</th>
<th>Th</th>
<th>F</th>
</tr>
</thead>
<?php
include('dbcon.php');
$query=mysql_query("select * from time where days='mtwthf' order by time_in")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['time_id'];
$start=date("h:i a",strtotime($row['time_in']));
$end=date("h:i a",strtotime($row['time_out']));
?>
<tr >
<td><?php echo $start."-".$end;?></td>
<td><input type="checkbox" id="check" name="m[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"><input style="width: 150px;" type="text" placeholder="Post Area" name="area[]"></td>
<td><input type="checkbox" id="check" name="t[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"><input style="width: 150px;" type="text" placeholder="Post Area" name="area[]"></td>
<td><input type="checkbox" id="check" name="w[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"><input style="width: 150px;" type="text" placeholder="Post Area" name="area[]"></td>
<td><input type="checkbox" id="check" name="th[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"><input style="width: 150px;" type="text" placeholder="Post Area" name="area[]"></td>
<td><input type="checkbox" id="check" name="f[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"><input style="width: 150px;" type="text" placeholder="Post Area" name="area[]"></td>
</tr>
<?php }?>
</table>
</div><!--col end -->
<button class="btn btn-lg btn-primary" id="daterange-btn" name="submit" type="submit">
Save Schedule
</button>
这是错误来自的php部分。可能是代码故障的根源。 这部分是插入查询:
<?php
include('dbcon.php');
if (isset($_POST['submit'])){
$month = $_POST['month'];
$guard = $_POST['guard_id'];
$area = $_POST['area'];
$time = $_POST['time_id'];
$m = $_POST['m'];
$t = $_POST['t'];
$w = $_POST['w'];
$th = $_POST['th'];
$f = $_POST['f'];
//monday sched
foreach ($m as $daym){
//check conflict
$query=mysql_query("select *,COUNT(*) as count from gov_sched
natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$daym' and area='$area'
and day='m' and month='$month'")or die(mysql_error());
$row=mysql_fetch_array($query);
$count=$row['count'];
$time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
$guard1=$row['lastname']." ".$row['firstname'];
$area1=$row['area'];
$queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
$rowt=mysql_fetch_array($queryt);
$guardt=$rowt['lastname']." ".$rowt['firstname'];
$querytime=mysql_query("select * from time where time_id='$daym'")or die(mysql_error($con));
$rowt=mysql_fetch_array($querytime);
$timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));
if ($count==0)
{
mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$area','$daym','m')")or die(mysql_error());
echo "<span class='text-success'>$month $timet $guardt at $area every Monday successfully added!</span><br>";
}
else{
echo "<span class='text-danger'>$month $time1 is already taken by $guard1 at $area every Monday </span><br>";
}
}
foreach ($t as $dayt){
//check conflict
$query=mysql_query("select *,COUNT(*) as count from gov_sched
natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$dayt' and area='$area'
and day='t' and month='$month'")or die(mysql_error());
$row=mysql_fetch_array($query);
$count=$row['count'];
$time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
$guard1=$row['lastname']." ".$row['firstname'];
$area1=$row['area'];
$queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
$rowt=mysql_fetch_array($queryt);
$guardt=$rowt['lastname']." ".$rowt['firstname'];
$querytime=mysql_query("select * from time where time_id='$dayt'")or die(mysql_error($con));
$rowt=mysql_fetch_array($querytime);
$timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));
if ($count==0)
{
mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$area','$dayt','t')")or die(mysql_error());
echo "<span class='text-success'>$month $timet $guardt at $area every Tuesday successfully added!</span><br>";
}
else{
echo "<span class='text-danger'>$month $time1 is already taken by $guard1 at $area every Tuesday </span><br>";
}
}
foreach ($w as $dayw){
//check conflict
$query=mysql_query("select *,COUNT(*) as count from gov_sched
natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$dayw' and area='$area'
and day='w' and month='$month'")or die(mysql_error());
$row=mysql_fetch_array($query);
$count=$row['count'];
$time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
$guard1=$row['lastname']." ".$row['firstname'];
$area1=$row['area'];
$queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
$rowt=mysql_fetch_array($queryt);
$guardt=$rowt['lastname']." ".$rowt['firstname'];
$querytime=mysql_query("select * from time where time_id='$dayw'")or die(mysql_error($con));
$rowt=mysql_fetch_array($querytime);
$timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));
if ($count==0)
{
mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$area','$dayw','w')")or die(mysql_error());
echo "<span class='text-success'>$month $timet $guardt at $area every Wednesday successfully added!</span><br>";
}
else{
echo "<span class='text-danger'>$month $time1 is already taken by $guard1 at $area every Wednesday </span><br>";
}
}
foreach ($th as $dayth){
//check conflict
$query=mysql_query("select *,COUNT(*) as count from gov_sched
natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$dayth' and area='$area'
and day='th' and month='$month'")or die(mysql_error());
$row=mysql_fetch_array($query);
$count=$row['count'];
$time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
$guard1=$row['lastname']." ".$row['firstname'];
$area1=$row['area'];
$queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
$rowt=mysql_fetch_array($queryt);
$guardt=$rowt['lastname']." ".$rowt['firstname'];
$querytime=mysql_query("select * from time where time_id='$dayth'")or die(mysql_error($con));
$rowt=mysql_fetch_array($querytime);
$timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));
if ($count==0)
{
mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$area','$dayth','th')")or die(mysql_error());
echo "<span class='text-success'>$month $timet $guardt at $area every Thursday successfully added!</span><br>";
}
else{
echo "<span class='text-danger'>$month $time1 is already taken by $guard1 at $area every Thursday </span><br>";
}
}
foreach ($f as $dayf){
//check conflict
$query=mysql_query("select *,COUNT(*) as count from gov_sched
natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$dayf' and area='$area'
and day='f' and month='$month'")or die(mysql_error());
$row=mysql_fetch_array($query);
$count=$row['count'];
$time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
$guard1=$row['lastname']." ".$row['firstname'];
$area1=$row['area'];
$queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
$rowt=mysql_fetch_array($queryt);
$guardt=$rowt['lastname']." ".$rowt['firstname'];
$querytime=mysql_query("select * from time where time_id='$dayf'")or die(mysql_error($con));
$rowt=mysql_fetch_array($querytime);
$timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));
if ($count==0)
{
mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$area','$dayf','f')")or die(mysql_error());
echo "<span class='text-success'>$timet $guardt at $area every Friday successfully added!</span><br>";
}
else{
echo "<span class='text-danger'>$time1 is already taken by $guard1 at $area every Friday </span><br>";
}
}
}
?>
事情是我不知道我在html表单结构或php方面是错的。我发现它真的很混乱。
数据库中area
字段的字段强制存储单词Array。