2012-12-12 94 views
2

我有一個積分系統是有一個測驗。當他們點擊提交測驗時,我想讓他們在mysql中獲得+1分。我有一個名爲登錄和表用戶的數據庫,並且在用戶希望點數在參加測驗時更新爲1+。 爲quiz_form代碼我使用:Mysql更新與PHP

:<!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"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Registration Form</title> 
</head> 

<body> 
<table width="328" border="0" align="center"> 
<form id="form1" name="form1" method="post" action="script.php"> 
<tr> 
<td colspan="2"><h2 style="color:#FF0000">Quiz1</h2></td> 
</tr> 
<tr> 
<td>UserName:</td> 
<td> 
<input name="txtusername" type="text" id="txtusername" value="<?php 
session_start(); 

if ($_SESSION['username']) 
{ 

    echo "".$_SESSION['username']; 


} 

else 
    header ("location: welcome1.html"); 

?> 
" /> 
</td> 
</tr> 
<tr> 
<td>Who is Obama?</td> 
<td><input type="password" name="txtPassword" id="txtPassword" /></td> 
</tr> 
<tr> 
    <td>What was the bing thing that were going to happend in 2012?</td> 
    <td><input type="text" name="txtemail" id="txtemail" /></td> 
</tr> 
<tr> 
<td>What is a farrari?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 


</td> 
</tr> 
<tr> 
<td>What is a spoon used for?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td>Witch letter does this url start with?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td>Are COD a game?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td colspan="2" align="center"> 
<input type="submit" name="btnRegister" id="btnRegister" value="submit" /> 

</td> 
</tr> 
</form> 
</table> 
<table width="328" border="0" align="center"> 
<form id="form1" name="form1" method="post" action="script2.php"> 
<tr> 
<td colspan="2"><h2 style="color:#FF0000">Quiz2</h2></td> 
</tr> 
<tr> 
<td>UserName:</td> 
<td> 
<input name="txtusername" type="text" id="txtusername" value="<?php 

if ($_SESSION['username']) 
{ 

    echo "".$_SESSION['username']; 


} 

else 
    header ("location: welcome1.html"); 

?> 
" /> 
</td> 
</tr> 
<tr> 
<td>Who evented facebook?</td> 
<td><input type="password" name="txtPassword" id="txtPassword" /></td> 
</tr> 
<tr> 
    <td>What is a cat?</td> 
    <td><input type="text" name="txtemail" id="txtemail" /></td> 
</tr> 
<tr> 
<td>What is a lamborgini?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 


</td> 
</tr> 
<tr> 
<td>What is goodel used for?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td>What is the last letter in this url</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td>Are Counter strik a game?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td colspan="2" align="center"> 
<input type="submit" name="btnRegister" id="btnRegister" value="submit" /> 

</td> 
</tr> 
</form> 
</table> 
<table width="328" border="0" align="center"> 
<form id="form1" name="form1" method="post" action="script3.php"> 
<tr> 
<td colspan="2"><h2 style="color:#FF0000">Quiz3</h2></td> 
</tr> 
<tr> 
<td>UserName:</td> 
<td> 
<input name="txtusername" type="text" id="txtusername" value="<?php 

if ($_SESSION['username']) 
{ 

    echo "".$_SESSION['username']; 


} 

else 
    header ("location: welcome1.html"); 

?> 
" /> 
</td> 
</tr> 
<tr> 
<td>What is yourube used for?</td> 
<td><input type="password" name="txtPassword" id="txtPassword" /></td> 
</tr> 
<tr> 
    <td>What is a dog?</td> 
    <td><input type="text" name="txtemail" id="txtemail" /></td> 
</tr> 
<tr> 
<td>What is a kebab?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 


</td> 
</tr> 
<tr> 
<td>Who is the most famed tenager in 2012/13?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td>What is the 4th letter in this url?</td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td>100*2 - 5 +105 -55 +45= </td> 
<td> 
<input type="text" name="txtnummer" id="txtnummer" /> 
</td> 
</tr> 
<tr> 
<td colspan="2" align="center"> 
<input type="submit" name="btnRegister" id="btnRegister" value="submit" /> 

</td> 
</tr> 
</form> 
</table> 
</body> 
</html> 

這裏是劇本我使用的測驗:

<?php 
//=============Configuring Server and Database======= 
$host  = 'localhost'; 
$user  = 'root'; 
$password = ''; 
//=============Data Base Information================= 
$database = 'login'; 

$conn  = mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server 
mysql_select_db($database,$conn) or die('Database Information is not correct'); 

//===============End Server Configuration============ 

//=============Starting Registration Script========== 


$username = mysql_real_escape_string($_POST['txtusername']); 

//=============To Encrypt Password=================== 

//============New Variable of Password is Now with an Encrypted Value======== 

if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button to register 
{ 
$query = mysql_query("SELECT * FROM quiz WHERE username='$username'"); 
if(mysql_num_rows($query) > 0){ 
echo "Sorry but you can only take a quize once:S"; 

}else{ 

mysql_query ("insert into quiz(username)values('$username')"); 
$sql = "UPDATE users SET points = COALESCE(level,0)+1 WHERE username = $username"; 
header('location: succes.php'); 
} 
} 
?> 

再次,這個工作很大。但我只需要讓他們得到1分以上。所以,如果他們有10分和藉此測驗提交

+0

有很多在這個問題上太多的東西。如果你不能將它降低到對問題重要的程度,那麼你可能會問得太早,或者不太瞭解你的代碼! – markus

+2

不要再使用古代的mysql_ *函數!他們不安全,效率低下。而是使用PDO或mysqli和準備好的語句! – markus

+0

現在的問題是:我需要什麼代碼才能讓人們點擊提交,然後再獲得1點。更新代碼 – carl

回答

0

試試這個更新查詢,他們得到11點擊後

$qry = "UPDATE users SET points = points+1 WHERE username = $username" 

,並在評論中說,開始使用PDO或mysqli的與你未來的代碼準備語句,因爲舊的mysql命令已被棄用。

+0

謝謝,哈,我會如果我設法做到這一點:) – carl

+0

你的歡迎,很高興我可以幫助:)。我改變了我的舊代碼現在我的一些項目 – kabuto178

+0

我發現我沒有工作:/但我認爲這是正確的代碼,但沒有發生。我沒有在我的桌子上多得1分。順便說一句,代碼結束了另一個表然後是點是 – carl

0

我想你在這裏有一個問題:

$sql = "UPDATE users SET points = COALESCE(level,0)+1 WHERE username = $username"; 

所以,如果你正在做這樣。

試試這個,

$sql = "UPDATE users SET points = COALESCE(level+1,0) WHERE username = $username"; 
+0

我沒有得到錯誤或東西,但它似乎不工作。沒有其他事情發生 – carl