2015-07-20 75 views
0

其實我想開發一個應用程序,從哪裏用戶可以爲學生設置考勤。所以出席的HTML表格將來自我的數據庫查詢。它也來了,但問題是,我如何可以將該表單信息插入到我的數據庫。實際上我搜索了很多,但我沒有得到任何結果,因爲這是完美的,因爲我想我的意思請任何人都可以幫助我。在此先感謝php-mysql從回聲單選按鈕插入多行

<form action="attendance.php" method="post"> 
<?php include '../database-config.php'; 
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row){ 
    echo "<div> 
    <label>".htmlentities($row['student_id'])."</label> 
    <input type='radio' name='atten".htmlentities($row['student_id'])."' checked='checked'>Present   
    <input type='radio' name='atten".htmlentities($row['student_id'])."'>Absent 
     </div></br>"; 
} 
?> 
<button type="submit" class="btn btn-success btn-lg">Submit</button> 
<button type="reset" class="btn btn-danger btn-lg">Reset</button> 
</form> 
+0

什麼是你從期待添加列attendency到數據庫表:'foreach($ dbh-> query(「SELECT * FROM student WHERE active_class ='VII'」)as $ row){'?? – 2015-07-20 04:39:53

+0

您可以使用'hidden'字段存儲學生ID或使用輸入數組。 –

+0

實際上我對php沒有很好的瞭解。所以你可以只提供給我任何例子,我的意思是html多個單選按鈕到php myqsl多個數據插入。謝謝 –

回答

0
<form action="attendance.php" method="post"> 
    <?php include '../database-config.php'; 
    $result = mysql_query("SELECT * FROM student WHERE active_class='VII'"); 
    foreach($result as $row) 
    { 
     ?> 
     <div> 
     <label><?php echo $row['student_id']?></label> 
     <input type="radio" name="attend" value="present" checked>Present 
     <input type="radio" name="attend" value="absent">Absent 
     </div> 
      </br> 
     <?php 
    } 
    ?> 
    <button type="submit" class="btn btn-success btn-lg">Submit</button> 
    <button type="reset" class="btn btn-danger btn-lg">Reset</button> 
</form> 

所以在PHP中,你可以得到價值這樣

<?php 

$attend = $_POST['attend']; 

echo $attend; 

?> 

所以$attend它包含單選按鈕的值(value="present")。

可能present或任absent

+0

如何插入mysql –

+0

'$ attend'包含您的數據。所以你可以插入 –

0

該死厭倦的xD 這應該工作,雖然,但你必須通過自己的歡呼聲

<form action="" method="post"> 
    <?php 

    include '../database-config.php'; 
    if(isset($_POST['attendency']) && isset($_POST['id'])) 
    { 
     $id_to_update = $_POST['id']; 
     $status = $_POST['attendency']; 
     $ar = array('p','a'); 
     $attend = !empty($status) && in_array($status,$ar) ? $status : 'p'; 


     //you have to create a column named attendency for this to work 
     $sql = "INSERT INTO student(attendency) VALUES ('$attend ') WHERE user_id = '$id_to_update '"; 
     $dbh->query($sql); 
} 
    foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row) 
    { 

     if($row['attendency'] == 'p') 
     { 
      $p = 'checked="checked"'; 
      $a = ''; 
     } else { 
      $a = 'checked="checked"' 
    $p = ''; 
    } ?> 

     <div> 
      <input type="hidden" name="id" value="<?=$row['student_id']?>"> 
      <label><?=$row['student_id']?></label> 
      <input type='radio' name='attendency' <?=$p?>>Present 
      <input type='radio' name='attendency' <?=$a?>>Absent 
     </div></br> 

    <?php } ?> 

    <button type="submit" class="btn btn-success btn-lg">Submit</button> 
    <button type="reset" class="btn btn-danger btn-lg">Reset</button> 
</form>