ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programming[php]สอบถามผู้รู้หน่อยครับ เกี่ยวกับการเชื่อมต่อ
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: [php]สอบถามผู้รู้หน่อยครับ เกี่ยวกับการเชื่อมต่อ  (อ่าน 687 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
KONINW
ก๊วนเสียว
*

พลังน้ำใจ: 62
ออฟไลน์ ออฟไลน์

กระทู้: 452



ดูรายละเอียด
« เมื่อ: 10 กุมภาพันธ์ 2011, 16:03:04 »

คือว่าผมเว็บอีเลิร์นและได้สร้างข้อสอบมา แล้วอยากจะให้มัน เชื่อมต่อกับฐานข้อมูลเว็บครับ  และโชว์ ชื่อ - นามสกุล ในตารางข้อมูล member ของผมครับ

นี่คือโค้ดข้อสอบนะครับ

โค๊ด:
<?php require_once(&#39;Connections/elearn.php&#39;); ?>
<?php
$colname_Recordset1 
"-1";
if (isset(
$_GET[&#39;idl&#39;])) {
  
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET[&#39;idl&#39;] : addslashes($_GET[&#39;idl&#39;]);
}
mysql_select_db($database_elearn$elearn);
$query_Recordset1 sprintf("SELECT * FROM tb_test WHERE idl = %s"$colname_Recordset1);
$Recordset1 mysql_query($query_Recordset1$elearn) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />

<title>[url=http://www.kakarndee.com]www.kakarndee.com[/url]</title>


<style type="text/css">
<!--
body,td,th {
font-family: Microsoft Sans Serif;
font-size: small;
}
a {
font-family: Microsoft Sans Serif;
font-size: small;
}
h1,h2,h3,h4,h5,h6 {
font-family: Microsoft Sans Serif;
border: 1;
}
body {
background-color: #FFFFFF;
margin-top: 0px;
margin-bottom: 0px;
background-image: url(../images/body.jpg);
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
.borderG {
border: 2px dotted #FF0000;
}
.borderB {
border: 2px dotted #0000FF;
}
.borderG {
border: 1px dotted #0000FF;
}
.borderB {
border: 1px dotted #FF0000;
}
-->
</style>
<script type="text/JavaScript">
<!--

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i  ) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i  )
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j  ].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p 1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i  ) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i  ) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i =3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j  ]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i 2];}
}
//-->
</script>
<style type="text/css" media="all">
<!--
@import url("12.css");
.style1 {
color: #FF0000;
font-weight: bold;
}
.style3 {font-size: 36px}
.style4 {color: #FF0000}
-->
</style>
</head>

<body onload="MM_preloadImages('../moom/images1/Untitled-1_01.gif','moom/images1/Untitled-1_02.gif','moom/images1/Untitled-1_03.gif','moom/images1/Untitled-1_04.gif','moom/images1/Untitled-1_05.gif','moom/images1/Untitled-1_06.gif','moom/images1/Untitled-1_07.gif')">
<span class="style3"></span>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" class="lo">
  <!--DWLayoutTable-->
  <tr>
    <td width="780" height="180" valign="top"><img src="../images/head2.png" width="728" height="200" /></td>
  </tr>
  <tr valign="middle">
    <td height="16" align="center" valign="top"><a href="index.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image20','','moom/images1/Untitled-1_01.gif',1)"></a><a href="uppic/boardpic.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image21','','moom/images1/Untitled-1_02.gif',1)"></a><a href="Board/webboard.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image22','','moom/images1/Untitled-1_03.gif',1)"></a><a href="kguest.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image23','','moom/images1/Untitled-1_04.gif',1)"></a><a href="kmsn.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image24','','moom/images1/Untitled-1_05.gif',1)"></a><a href="Download.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image25','','moom/images1/Untitled-1_06.gif',1)"></a><a href="kadmin.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image26','','moom/images1/Untitled-1_07.gif',1)"></a><a href="index.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image4','','../moom/images1/Untitled-1_01.gif',1)"><strong>
      <?php $correct=0;
$total=0;
include 
"connect.php";
$sql="select id, answer from tb_test where idl=&#39;1&#39; order by id";
$result=mysql_db_query("$dbname",$sql);
$total=mysql_num_rows($result);
while (
$r=mysql_fetch_array($result)){
$id=$r[id];
$answer=$r[answer];

$select="select_".$id;
if(${
$select}==$answer) {
$correct  ;
}
}
echo 
"แบบทดสอบทั้งหมด $total  ข้อ ";
 
?>

    </strong></a></td>
  </tr>
  <tr>
    <td height="135" valign="top"><table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="65">&nbsp;</td>
        <td width="620">&nbsp;</td>
        <td width="65">&nbsp;</td>
      </tr>
      <tr>
        <td height="25">&nbsp;</td>
        <td>&nbsp;&nbsp;<img src="../images/admin/pin.gif" width="16" height="16" /> คำชี้แจง&nbsp; กรุณากรอกข้อมูลนักศึกษาให้ครบก่อน แล้ว จงเลือกคำตอบที่ถูกที่สุดเพียงข้อเดียว <br />
          นำคะแนนที่ได้แค่ครั้งแรกครั้งเดียวเท่านั้นไปเป็นคะแนนจริง ดูตาม เวลาที่นักศึกษาได้ทำการส่งคะแนน กำหนดเวลาแค่ <span class="style1">30</span> นาที ในการส่งคะแนน <span class="style4">ระบบจะจัดส่งคะแนนเองอัตโนมัติเมื่อหมดเวลาในการทำข้อสอบ <br />
          <br />
          <br />
          </span></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><form action="Atest2.php" method="post" name="form1" id="form1">
          <table width="600" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td align="center">ชื่อ</td>
              <td><input name="name" type="text" id="name" />
              &nbsp;<span class="style4">***</span></td>
              <td align="center">นามสกุล</td>
              <td><input name="lname" type="text" id="lname" /> <span class="style4">***</span></td>
            </tr>
            <tr>
              <td align="center">แผนก</td>
              <td><input name="class" type="text" id="class" />
                &nbsp;<span class="style4">***</span></td>
              <td align="center">รหัสประจำตัวนักศึกษา</td>
              <td><input name="on" type="text" id="on" />
                <span class="style4">*****</span></td>
            </tr>
            <tr>
              <td align="center">ปีการศึกษา</td>
              <td><input name="pee" type="text" id="pee" /></td>
              <td>&nbsp;</td>
              <td><input name="etc" type="hidden" id="etc" value="ปลายภาค" /></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </table>
          <?php 
$number
=1;
include 
"connect.php";
$sql="select * from tb_test where idl=&#39;1&#39; order by id";
$result=mysql_db_query("$dbname",$sql);
while(
$r=mysql_fetch_array($result)){
$id=$r[id];
$question=$r[question];
$choice1=$r[choice1];
$choice2=$r[choice2];
$choice3=$r[choice3];
$choice4=$r[choice4];

echo  
"<b>ข้อ  $number.$question </b> <br>
<input type=&#39;radio&#39; name=&#39;select_
$id&#39; value=&#39;1&#39;>
 
$choice1 <br>
<input type=&#39;radio&#39; name=&#39;select_
$id&#39; value=&#39;2&#39;>
 
$choice2 <br>
<input type=&#39;radio&#39; name=&#39;select_
$id&#39; value=&#39;3&#39;>
 
$choice3 <br>
<input type=&#39;radio&#39; name=&#39;select_
$id&#39; value=&#39;4&#39;>
 
$choice4 <br>   ";
$number  ;
}
 
?>
<br />
            <input type="submit" name="Submit" value="ส่งคะแนน" />
            <input type="submit" name="Submit2" value="ยกเลิก" />
        </form>
            <br /></td>
        <td>&nbsp;</td>
      </tr>
    </table>
   
      <table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>&nbsp;</td>
        <td align="center"><strong>
          <?php $correct=0;
$total=0;
include 
"connect.php";
$sql="select id, answer from tb_test where idl=&#39;1&#39; order by id";
$result=mysql_db_query("$dbname",$sql);
$total=mysql_num_rows($result);
while (
$r=mysql_fetch_array($result)){
$id=$r[id];
$answer=$r[answer];

$select="select_".$id;
if(${
$select}==$answer) {
$correct  ;
}
}

echo 
"
แบบทดสอบทั้งหมด 
$total  ข้อ ";
 
?>

        </strong></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td align="center">ตรวจสอบข้อมูลให้ถูกต้องทุกครั้งก่อนส่งคะแนนด้วยนะคับ</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
 
  <tr>
    <td height="32" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
  </tr>
</table>
<div align="center"></div>
</body>
</html>
<?php
mysql_free_result
($Recordset1);
?>

ผมจะต้องเพิ่มโค้ดใดบ้างในการเชื่อมต่ออีกครับ เพื่อที่จะให้มันโชว์ ชื่อ - นามสกุล ในตารางข้อมูล member ของผมครับ

ขอบคุณล่วงหน้าครับ    wanwan017  wanwan017


บันทึกการเข้า

ไม่มีอะไรที่ไม่สำเร็จหากเราไม่ลงมือทำมัน .
candygurl
สมุนแก๊งเสียว
*

พลังน้ำใจ: 94
ออฟไลน์ ออฟไลน์

กระทู้: 670



ดูรายละเอียด เว็บไซต์
« ตอบ #1 เมื่อ: 10 กุมภาพันธ์ 2011, 16:19:47 »

เปลี่ยน SQL Statement ที่ใช้ Query ข้อมูลจาก Database ค่ะ

ข้อสอบเก็บรหัสนักเรียนไว้หรือเปล่าค่ะ ว่าเป็นนักเรียนคนไหนทำข้อสอบ ถ้าเก็บ....

ก็นำตาราง ข้อสอบ กับ ตารางข้อมูลนักเรียนมา JOIN เข้าด้วยกัน (เขียนเป็น SQL นั่นแหละ)

ถ้าไม่เคยเขียน JOIN ตาราง ก็ลอง ค้นหาในgoogle ก็ได้ค่ะ JOIN Table SQL อะไรก็ว่าไป

มีสอนแน่ค่ะ แต่ถ้างงกับคำสั่ง JOIN อาจจะได้คำสั่ง WHERE เปรียบเทียบระหว่างสองตารางก็ได้

พี่Goo ก็คงจะมีบอกแหละค่ะหาดู ไม่ยากเลย wanwan019
บันทึกการเข้า

หน้า: [1]   ขึ้นบน
พิมพ์