从动态创建的html表中获取某些列的数据

时间:2011-01-24 13:55:43

标签: php mysql html

我目前的问题是: 我创建了一个HTML表,动态地#34;根据多少行返回一个mysql_query。第一列从查询中获取数据,第二列包含文本字段(见下文):

<?php 
    $selApart = "SELECT idAPARTMENT FROM BUILDING, APARTMENT WHERE APARTMENT.BUILDING_ID = BUILDING.idBUILDING AND idBUILDING = '$building'";
    $res = mysql_query($selApart) or die("Could not execute query.");

?>
  <table width="244" border="0" cellspacing="0" id="hours_table">
    <tr>
      <td width="121">APARTMENT</td>
      <td width="119">HOURS</td>
    </tr>
<?php 
    $rcnt = 0;
    while($r = mysql_fetch_array($res)){
        $a = $r['idAPARTMENT'];
        $rcnt++;
        $rid = 'row'.$rcnt;
    ?>
    <tr>
      <td>'<?php echo $a?>'</td>
      <td id='<?php echo $rid?>'><input type="text" name="hours" id="hours" value="0"/></td>
    </tr>
<?php } ?>
<input type="submit" name="complete" id="complete" align="middle" value="INSERT"/>

在我的表格准备就绪后#34;我想填写我的文本字段并将这些值插入到sql表中。我不知道的是如何通过我设置的ID获取每列的值,就像

if(isset($_POST['complete'])){
    for($i=0; $i<$rcnt; $i++){
       //INSERT INTO APARTMENT (idAPARTMENT, HOURS) VALUES ($a, **table.row.id**)
    }
}

有人可以帮忙吗?这有可能吗? 提前谢谢!

3 个答案:

答案 0 :(得分:0)

他,你需要做的是将$ rid作为名称添加到文本框中,然后只需提交表单,你就可以将它们放在$ _POST [“row”+ $ rid];

你可以遍历每个$ _POST,如果变量以row + num开头,则将其保存在db

foreach($_POST as $key => $value)
   //if $key starts with row execute your db save

我希望这会有所帮助

答案 1 :(得分:0)

table放入form method=POST,并在提交时,您会在input数组中按名称查找所有$_POST。< / p>

<?php

    print_r($_POST);//this will just show you the contents of the array, you play with it later on

    $selApart = "SELECT idAPARTMENT FROM BUILDING, APARTMENT WHERE APARTMENT.BUILDING_ID = BUILDING.idBUILDING AND idBUILDING = '$building'";
    $res = mysql_query($selApart) or die("Could not execute query.");

?>
<form action="" method="POST"><!-- table must be included in a form with action pointing the script location, "" means itself -->
  <table width="244" border="0" cellspacing="0" id="hours_table">
    <tr>
      <td width="121">APARTMENT</td>
      <td width="119">HOURS</td>
    </tr>
<?php 
    $rcnt = 0;
    while($r = mysql_fetch_array($res)){
        $a = $r['idAPARTMENT'];
        $rcnt++;
        $rid = 'row'.$rcnt;
    ?>
    <tr>
      <td>'<?php echo $a?>'</td>
      <td id='<?php echo $rid?>'><input type="text" name="hours<?= $a ?>" id="hours" value="0"/></td><!-- name="hourse<?= $a ?>" each name must be unique, that's why you include the ID from your table. It's not a good idea to put a counter, use actual data from the table.  -->
    </tr>
<?php } ?>
    </table>
    <input type="submit" name="complete" id="complete" align="middle" value="INSERT"/>
</form>

答案 2 :(得分:0)

帖子“从某些列的动态创建的html表中获取数据”非常有用。它对我帮助很大,但需要一点改变:

 <?php
   //database connectivity
   mysql_connect ("localhost","root","","sis")or die("cannot connect"); 
   mysql_select_db("sis")or die("cannot select DB");
    //query to fetch data
   $sql1="select * from student";
   $result1=  mysql_query($sql1);
        // forming table to view data
    if($result1){
       echo "<table cellspacing='1' cellpadding='5'>
       <th width='30%'>Roll_No </th>
       <th width='40%'>Name </th>
       <th width='30%'>Sem & Sec </th>";

         while($row=mysql_fetch_array($result1)){

          $Roll_No=$row['Roll_No'];
          $Name=$row['Name'];
          $Sem_Sec=$row['Sem_Sec'];
          echo"<tr>
         <td>$Roll_No</td>
         <td>$Name</td>
         <td>$Sem_Sec</td>
         </tr>";

        }
        ?>