php - 使用 php 更新 Mysql

标签 php mysql database forms

<分区>

如果有测验,我有一个积分系统。当他们从测验中单击提交时,我希望他们在 mysql 中获得 +1 点。我有一个名为登录和表用户的数据库,在用户中,我希望在他们参加测验后将积分更新为 1+。 我使用的 quiz_form 代码:​​

:<!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 分并参加这个测验,他们在点击提交后会得到 11 分

最佳答案

试试这个更新查询

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

正如评论中所说,开始使用 PDO 或 MySqli 为您 future 的代码准备好的语句,因为旧的 mysql 命令已被弃用。

关于php - 使用 php 更新 Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13840145/

相关文章:

php - 使用 Yii2 在 Gridview 中显示相关数据

php - 清除文本框字段后再次加载选择框

php - 根据 mysql 数据库的分解数据动态创建菜单

php - 将user_id添加到Session mysql中

ruby-on-rails - rake 中止 : Wrong number of arguments (0 for 1)

php - 无法将 slider 文本更改为 slider 标题

php - 使用所选国家/地区的拨号代码填充“电话号码”字段

mysql - 在其他列旁边插入值

php - CodeIgniter 的喜欢和地点

mysql - 计算转发的推文数量和已转发的推文总数