|
 |
|
PHP MSQL รบกวนดูโค้ดเกี่ยวกับการโหวตให้คะแนนกระทู้ที่ชื่นชอบหน่อยค่ะ |
|
 |
|
|
 |
 |
|
เงือนไขการโหวตคือ 1 user / 1 คะแนน / 1 กระทู้
ตอนนี้คือยังเช็คไม่ได้ ถ้ามีการโหวตไปแล้ว
และ user_id ที่เก็บลงดาต้าเบสก็ยังไม่ตรงค่ะ คะแนนก็ยังนับไม่ได้
ต้องเขียนโค้ดยังไงให้ถูกต้องคะ
Code (PHP)
<? @session_start();
if($_SESSION['user_id'] == "")
{
echo "<script> alert (' please LOGIN !!') ;</script>";
echo"<meta http-equiv=\"refresh\" content=\"0;url='login.php'\">";
exit();
}
if($_SESSION['status'] != "0")
{
echo "<script> alert ('This page for admin only') ;</script>";
echo"<meta http-equiv=\"refresh\" content=\"0;url='login.php'\">";
exit();
}
?>
<meta http-equiv= "Content-Type" content="text/html; charset=utf-8" />
<?
include("connect.php");
// $question_id = $_GET['questionid'];
// $user_id = $_GET['userid'];
/* ทำการตรวจสอบ Counter */
$sql1= "Select * From vote where question_id = '".$_GET['questionid']."' and user_id = '".$_GET['userid']."' ";
$dbquery1=mysql_db_query($db,$sql1);
$result1= mysql_fetch_array($dbquery1);
$count_view=$result1[count_view];
$sql1 = "insert into vote (vote_id , question_id , user_id , vote_count) VALUES ('','".$_GET['questionid']."','".$_GET['userid']."','$count_view') ";
$dbquery1=mysql_db_query($db,$sql1);
echo $count_view ; // echo ค่าของ conter
$sql2 = "select * from vote where question_id='$question_id' and $user_id = '$user_id' ";
$queryC2 = mysql_db_query($db,$sql2);
$numC = mysql_num_rows($queryC2);
if ($numC != 0)// ถ้า username มีการใช้งานไปแล้ว จะมีการแจ้งเตือนให้เปลี่ยน
{
echo "<script> alert (' ขออภัยค่ะ คุณโหวตหัวข้อนี้ไปแล้ว ') ; history.back();</script>";
exit ();
}else
/* ทำการ Update Counter โดย+1 */
$vote_count=$count_view+1;
$sql2="UPDATE vote SET vote_id='' , question_id='".$_GET['questionid']."', user_id='".$_GET['userid']."' , vote_count='$vote_count' where question_id = '".$_GET['questionid']."' ";
$dbquery2=mysql_db_query($db,$sql2);
echo "<script> alert (' โหวตให้คะแนนเรียบร้อยแล้วค่ะ ') ; history.back();</script>";
mysql_close();
?>
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2011-08-17 21:58:09 |
By :
nokz |
View :
1708 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
// $_GET['user_id']
// $_SESSION['user_id'] 2 ตัวนี้มีค่าเหมือนกันไม๊ ตรวจดูดีๆน่ะครับ การใช้มันยังสะเปะสะปะครับ
// ลองเก็บใส่ตัวแปรๆซักตัวน่ะครับ....จะทำให้ดูง่าย ไล่โค้ดง่ายด้วยครับ
บรรทัดที่ 36
Code (PHP)
$sql2 = "select * from vote where question_id='$question_id' and $user_id = '$user_id' ";
$queryC2 = mysql_db_query($db,$sql2);
$numC = mysql_num_rows($queryC2);
แก้เป็น
Code (PHP)
$sql2 = mysql_query("select * from vote where question_id='$question_id' and user_id = '$user_id' ");
$numC = mysql_num_rows($queryC2);
บรรทัด 56
Code (PHP)
$sql2="UPDATE vote SET vote_id='' , question_id='".$_GET['questionid']."', user_id='".$_GET['userid']."' , vote_count='$vote_count' where question_id = '".$_GET['questionid']."' ";
$dbquery2=mysql_db_query($db,$sql2);
echo "<script> alert (' โหวตให้คะแนนเรียบร้อยแล้วค่ะ ') ; history.back();</script>";
แก้เป็น
Code (PHP)
// vote_id ไม่ autorun หรอครับ
$sql2=mysql_query("UPDATE vote SET question_id='".$_GET['questionid']."', user_id='".$_GET['userid']."' ,
vote_count= vote_count+1 where question_id = '".$_GET['questionid']."' ");
if($sql2)
{
echo "<script> alert (' โหวตให้คะแนนเรียบร้อยแล้วค่ะ ') ; history.back();</script>";
}
|
 |
 |
 |
 |
Date :
2011-08-17 22:40:38 |
By :
mangkunzo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|