2011-01-24 26 views
0

我目前的問題是: 我有一個根據多少行帶回mysql_query「動態」創建的HTML表。第一列從查詢中獲取第二列有一個文本字段塔數據(見下文):從某些列的動態創建的html表中獲取數據

<?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"/> 

我的表之後是「準備好了」,我想,以填補在我的文本字段,並插入這些值一個sql表。我不知道的是我怎麼能通過我設置的id來得到每列的值,就像

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

有人可以幫忙嗎?這是可能做到的嗎? 在此先感謝!

回答

0

他,你必須做的是將$ rid作爲名稱添加到文本框中,然後只需提交表單,然後在$ _POST [「row」+ $ rid]中添加$ rid。

您可以通過每$ _ POST,如果在數據庫與行+ NUM將它保存變量開始循環

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

我希望這有助於

+0

This Works!非常感謝TriLLi! – 2011-01-24 14:23:28

+0

歡迎您 – 2011-01-24 15:28:38

0

將與tableformmethod=POST,當提交你將按名稱查找$_POST陣列中的所有input

<?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> 
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>"; 

     } 
     ?> 
相關問題