|
 |
|
อยากทราบว่าเก็บคะแนนลงdatabase แบบมีผู้ใช้ทำไงหรอค่ะ |
|
 |
|
|
 |
 |
|
ตามหัวข้อค่ะ คือว่า เราทำข้อสอบจะเก็บคะแนนลงdatabase
แต่เราทำเว็บมีloginอะไรแบบนี้อ่า แล้วมีแบ่งผู่ใช้เป็น Adnim ,User , ครู
เราอยากจะเก็บคะแนนลงแต่Userอ่า จะต้องเขียนโค้ดไงหรอค่ะ
โค้ดข้อสอบ Sample1.php
Code (PHP)
<?php require_once('Connections/member.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_member, $member);
$query_Recordset1 = "SELECT * FROM testing";
$Recordset1 = mysql_query($query_Recordset1, $member) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<html>
<head>
<script language="javascript">
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}
</script>
</head>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<p> </p>
<table width="1136" height="644" border="0" align="center">
<tr>
<td height="173" valign="top"><p> </p>
<p> </p></td>
</tr>
<tr>
<td valign="top"><p> </p>
<form name="form1" method="post" action="Sample2.php">
<?
$host="localhost";
$username="root";
$password="1234";
$db="member";
$tb="testing";
mysql_connect( $host,$username,$password) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
$sql="Select * From $tb order by rand() limit 10";
$db_query=mysql_query($sql);
$i=0;
while($result=mysql_fetch_array($db_query))
{
$i++;
?>
<table width="1125" border="0" align="center">
<tr>
<td width="650"><div align="center">
<input name="id<?=$i;?>" type="hidden" value="<?=$result["id"];?>">
<?=$result["question"];?>
</div></td>
<td width="165"><input type="radio" name="c<?=$i;?>" value="1" checked>
<?=$result["c1"];?></td>
<td width="165"><input type="radio" name="c<?=$i;?>" value="2">
<?=$result["c2"];?></td>
<td width="165"><input type="radio" name="c<?=$i;?>" value="3">
<?=$result["c3"];?></td>
<td width="165"><input type="radio" name="c<?=$i;?>" value="4">
<?=$result["c4"];?>
<input name="answer<?=$i;?>" type="hidden" value="<?=$result["answer"];?>"></td>
</tr>
</table>
<?
}
mysql_close();
?>
<div align="center"><br>
<input type="hidden" name="line" value="<?=$i;?>">
<input type="submit" name="Submit" value="ตรวจคะแนน">
</div>
</form></td>
</tr>
</table>
<p> </p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
โค้ด คะแนน Sample2.php
Code (PHP)
<?php
$server="localhost";
$db_user="root";
$db_pwd="1234";
$db_name="member";
$conn = @mysql_pconnect($server,$db_user,$db_pwd) or die(mysql_error());
mysql_query("SET NAMES UTF8");
@mysql_select_db($db_name,$conn) or die(mysql_error());
?>
<?
$score=0;
for($i=1;$i<=$_POST["line"];$i++)
{
If($_POST["c$i"] == $_POST["answer$i"])
{
$score=$score+1;
}
}
echo "<center><br><br><br><br><br><br>จาก10 ข้อได้<br> $score คะแนน<br></center>";
?>
นี้ฐานข้อมูล

Tag : PHP, MySQL, HTML/CSS

|
ประวัติการแก้ไข 2014-06-20 20:13:37 2014-06-20 20:17:27
|
 |
 |
 |
 |
Date :
2014-06-20 20:11:36 |
By :
l3_l3aD |
View :
1093 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
sample2.php
Code (PHP)
01. <?php
// แทรกบันทัดข้างล่างนี้เพื่อตรวจสอบ
print_r($_REQUEST); exit;
02. $server="localhost";
ก๊อบปี่ รหัสต้นฉบับมาดูกันครับ ว่า sample1 มันส่งอะไรมาให้ sample2 บ้าง
จะได้รู้กันว่า จะต้องเพิ่มอะไร เพราะ code sample1 บอกตรงๆ สะเปะสะปะอยู่มากครับ
dbconnect ก็มีสองที่ select_db ก็มีสองที่ แบบเอาโค๊ดเขามายำ โดยไม่ได้อ่านมากกว่าครับ
|
ประวัติการแก้ไข 2014-06-21 17:25:35
 |
 |
 |
 |
Date :
2014-06-21 17:24:46 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|