在Php中更新多个值时,复选框问题

时间:2013-01-14 11:06:19

标签: php

我写了一个非常简单的脚本来更新不同列中的多个值。 我的问题是,当我尝试更新列中的值时,它工作正常,但在复选框的情况下,值插入不正确。

代表。如果我有7行要更新,在选择选项中我选择1,2,3复选框,我不选择4 5并再次选择6 7然后在更新时选择1,2,3,4,5和离开6 7未选中

我在这里粘贴我的代码

形式

     <?php include("db.php");

          $id = $_GET['id'];
   $tDate2=$_GET['tDate2'];
 ?> <form method="POST" style="width:50%" action="updatemonthly.php"  >
  <table id="rounded-corner">


    <?php
    $conn=mysql_connect("localhost","root","root") or die(mysql_error());
    mysql_select_db("monthly_pmc_admin") or die(mysql_error()); 
     $result1 = mysql_query("SELECT * from project_details where id ='$id' ;");
     $row = mysql_fetch_assoc($result1);
     $pno=$row['Project_No'];
    $result = mysql_query("SELECT * from capex_phasing_data where Project_Number ='$pno' order by id ;");
     ?>
    <thead>
      <tr>
        <th colspan="6" scope="col" class="rounded-company">Edit/ Delete Capex Graph Values</th>
        <th scope="col" class="rounded-q1"> <?php echo $tProjNo ?></th>
        <th scope="col" class="rounded-q4"></th>
      </tr>
    </thead>
    <tfoot>
      <tr>
        <td colspan="7" class="rounded-foot-left" align="center"><input type="submit" name="submit" value="DELETE" id='myButton1' />
          <input type="submit" name="submit" value="UPDATE" id='myButton1' />
         <?php 
         echo 
          '<a href="../capex.php?id=' . $id . '&tDate2='.$tDate2.'"> Add New Capex Value</a>';
          ?></td>
        <td class="rounded-foot-right"></td>
      </tr>
       <tr>
            <td colspan="4" class="rounded-foot-left" align="center"> <?php 
            echo('<A hREF="http://10.26.162.84/graph1/default.aspx?id='.$pno .'&tDate2='. $tDate2. '" target="_blank"> View Graph </A>');?></td>
            <td class="rounded-foot-right"></td>
          </tr>
    </tfoot>
    <tr>
      <th align='center'> Select to <br/>
        Delete Record </th>
      <th >Month</th>
      <th>Planned Phasing </th>
      <th>Actual Phasing </th>
       <th>Select To Display Value in Graph</th>

    </tr>
    <?
    for($i=1;$i<=mysql_num_rows($result);$i++) {
    $row = mysql_fetch_assoc($result);
    ?>
    <tr>
      <td align='left' ><input  type="checkbox" name="chk[]"  value="<?=$row['id']?>" align='center' />
      </td>
      <td align='left'><input type="text" name="month[]" size="10" id="jj" value="<?php echo $row['Month'];?>"  />
      </td>
      <td align='center' ><input name="planman1[]"  size="50" type="text" id="rr" value="<?php echo $row['Planned_Phasing'];?>"  />
      </td>
      <td align='center' ><input name="planman2[]" type="text" size="8" id="r1r" value="<?php echo $row['Actual_Phasing'];?>"  />
      </td>

      <td align='center' ><input name="planman5[]" type="checkbox" id="r4r"  <? if($row['Select_project']==1) {echo 'checked';} ?>  />
      </td>

        <input  type="hidden" name="rowid[]"  value="<?=$row['id']?>" align='center' />
        <input type="hidden" name="ProjNo" value="<?php echo $id; ?>">
          <input type="hidden" name="tDate2" value="<?php echo $tDate2; ?>">
        <!--<input type="hidden" name="ProjNo" value="<?php echo $tProjNo; ?>">-->
      </td>
    </tr>
    <?php
     }
     ?>

更新查询

<?php
session_start();
require_once('dbclass/database.php');
$myClass=new database();
//echo $_POST['name'];
  $array=$_POST['rowid'];
  $month=$_POST['month'];
  $planman1=$_POST['planman1'];
  $planman2=$_POST['planman2'];
  $planman3=$_POST['planman3'];
  $planman4=$_POST['planman4'];
  $planman5=$_POST['planman5'];
  $actuanman=$_POST['actuanman'];
  $length=count($_POST['rowid']);
  $chk=$_POST['chk'];
  $projno=$_POST['ProjNo'];
  $tDate2=$_POST['tDate2'];

  if($_POST['submit']=='UPDATE'){


for($a=0;$a<$length;$a++){
    $rid=$array[$a];
   $array[$a];
   $month[$a];
   $planman1[$a];
   $planman2[$a];
   $planman3[$a];
   $planman4[$a];
   $planman5[$a];
   $ac= $actuanman[$a];

 if($ac=='on'){$ch=1;} else {$ch=0;}


 if($planman5[$a]=='on'){$planman5[$a]='1';} else {$planman5[$a]='0';}

 $sql="UPDATE  capex_phasing_data set   Month='".$month[$a]."',   Planned_Phasing='".$planman1[$a]."' , Actual_Phasing='".$planman2[$a]."',    Select_project='".$planman5[$a]."' where id='".$rid."'";

mysql_query($sql) or die(); 

header("location:#");
}

}

3 个答案:

答案 0 :(得分:0)

如果您提交的表单包含多个选择框,则只有已选中复选框的名称将位于$_POST数组中。因此,要知道复选框是否已打开,您必须在$_POST中检查其名称是否出现。

答案 1 :(得分:0)

您必须对属于一个属性的所有复选框使用相同的名称,后跟“[]”。

e.g。如果你把爱好复选框作为音乐,阅读和冲浪,那么所有人应该有相同的名字,比如爱好[]和价值观,如音乐,阅读或冲浪。

<input type='checkbox' name='hobbies[]' value='Music'>Music</input>
<input type='checkbox' name='hobbies[]' value='Reading'>Reading</input>
<input type='checkbox' name='hobbies[]' value='Surfing'>Surfing</input>

因此,如果您查看音乐和冲浪,您将在$ _POST ['爱好']

中获得已选中复选框的
$_POST['hobbies'] as array('Music','Surfing')

因此,您可以相应地管理数据库值。

答案 2 :(得分:0)

终于在理解之后我找到了解决我自己的问题的方法谢谢lex和sujit的宝贵时间和帮助

我的chck框格式改为

<td align='center' ><input name="planman2[]" type="text" size="8" id="r1r" value="<?php echo $row['Actual_Phasing'];?>"  />
  </td>

  <td align='center' ><input name="**planman5<?=$i?>**" type="checkbox" id="r4r"  <? if($row['Select_project']==1) {echo 'checked';} ?>  />
  </td>

并在更新脚本中

for($a=0;$a<$length;$a++){
$rid=$array[$a];
$array[$a];
$month[$a];
$planman1[$a];
$planman2[$a];
$planman3[$a];
$planman4[$a];
**$p=$a+1;
$p5= $_POST['planman5'.$p];
if($p5=='on'){$ch1=1;} else {$ch1=0;}**

现在它的工作非常好......

感谢您的帮助和建议