วันศุกร์ที่ 6 มีนาคม พ.ศ. 2563

การดึงข้อมูลจากฐานข้อมูล มาแสดงใน Select box


 สำหรับวิธีการดึงข้อมูลจากฐานข้อมูลออกมาแสดงที่ Select box นั้นเราจะใช้ While Loop ในการสั่งให้ดึงข้อมูลวนซ้ำจนครบ และนำมาแสดงที่หน้าเว็ปไซต์

Syntax
     while($result = mysql_fetch_array)
     {
          //สิ่งที่ต้องการห้แสดง
     }

ตัวอย่างการใช้งาน
Code
register.php

<form name='regis' method='POST' action='preview.php' onsubmit='return chkfrom();'>
<table width='500' align='center' border='1'>
<tr>
<td>Name :</td>
<td><input type='text' name='Name'></td>
</tr>
<tr>
<td>Lastname :</td>
<td><input type='text' name='lastname'></td>
</tr>
<tr>
<td>Faculty :</td>
<td>
<?php
include('config/config.php');
$strsql = "SELECT * FROM faculty ORDER BY id ASC";
$result = mysql_query($strsql);
?>
<select name='faculty'>
<option value='0'>กรุณาเลือกคณะฯ</opton>
<?php
while($rs = mysql_fetch_array($result))
{
?>
<option value='<?php echo $rs['faculty_name'];?>'><?php echo $rs['faculty_name'];?></opton>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='submit' value='Submit'></td>
</tr>
</table>

preview.php

<meta http-equiv="Content-type" content="text/html; charset=utf8"/ >
<?php
echo $_POST['faculty'];
exit;
?>

ตัวอย่างไฟล์ Database  >>Download<<

ไม่มีความคิดเห็น:

แสดงความคิดเห็น