2011-01-24 3 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]에 넣을 수 있습니다.

당신은 DB에 저장 행 + NUM와 변수를 시작할 때마다 $ _POST 통해 만약 루프

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

나는이

+0

이 작품은 작동합니다! 고마워요. 많은 TriLLi! –

+0

환영합니다 –

0

tablemethod=POSTform에 때 당신을 제출하는 것을 넣어 도움이되기를 바랍니다 수 있습니다 'input'이 모두 $_POST 배열에 있습니다.

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

     } 
     ?> 
+1

신중하게 데이터베이스에 따라 속성을 입력하십시오. – saurabh

관련 문제