how can return array values if first element is null in php

时间:2015-04-29 04:04:13

标签: javascript php

I need to insert array values into database.But i can not insert array values properly when first value of array is null.How can i fix my problem

<form name=myform action="addcanteen.php" method=post>
<table><tr><td width="11%"> Date:</td><td width="89%"><input type="date" name="cdate" id="cdate"/></td></tr></table>
<table border="1" bgcolor="#F5F9C1">
<tr>
<!--<th>ID</th>-->
<th>Name</th>
<td><!--<input type="text" id="datepicker">-->
  <input type="checkbox" id="selectall" name="chk[]"/></td>
<td align="center">coffee</td>  
<td align="center">tea</td>
</tr>
<?php while($row=mysql_fetch_array($result))
{
?>
<tr>
<?php /*?><td><?php echo $row['emp_id'];?></td><?php */?>
<td><?php echo $row['emp_name'];?></td>

<td align="center"><input type="checkbox" class="name" name="chk1[]" value="<?php echo $row['emp_name'];?>"/></td>

<td><input type="text" name="coffee[]" id="coffee" value="" /></td>
<td><input type="text" name="tea[]" id="tea" value=""/></td>
</tr>
<?php } ?>
<tr>
<td>
</td>
<td>
</td>
<td align="center">
<input type="submit" class="button" name="submit" id="submit" value="submit"/>
</td>
</tr>
</table>
</form>

above is my form and this is my insertion code.I inserted entire value if it is not null.But if first one is null i can not insert into database

 <?php
        include(dbcon.php);
        date_default_timezone_set('UTC');
         $date=date("d");
        $month=date("m");
        $year=date("Y");
        $fd=date("d-m-Y");
        if(isset($_POST['submit']))
        {
            $cdate=$_POST['cdate'];
         $checkbox1=$_POST['chk1'];
        $tea=$_POST['tea'];
        $coffee=$_POST['coffee'];
        for ($i=0; $i<sizeof($checkbox1);$i++) 
        {
        $query1="INSERT INTO canteen(name,coffee,tea,date)VALUES('".$checkbox1[$i]."','".$coffee[$i]."','".$tea[$i]."','$cdate')";
        $sql1=mysql_query($query1);
        }
        }
        header("location:canteen.php");
        ?>

This is my java script for the function select the check box

$(function () {
    // add multiple select / deselect functionality
    $("#selectall").click(function () {
        $('.name').attr('checked', this.checked);
    });
    // if all checkbox are selected, then check the select all checkbox
    // and viceversa
    $(".name").click(function () {
        if ($(".name").length == $(".name:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
    });
});

1 个答案:

答案 0 :(得分:0)

也许......试试这个:

for ($i=0; $i<sizeof($checkbox1);$i++) 
{
  if($checkbox1[$i] != null && $coffee[$i] != null && $tea[$i]  != null) {
    $query1="INSERT INTO canteen(name,coffee,tea,date)VALUES('".$checkbox1[$i]."','".$coffee[$i]."','".$tea[$i]."','$cdate')";
    $sql1=mysql_query($query1);
  }
}