2012-12-12 96 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