PHP更新不更新数据库
问题描述:
这是我的html表单的代码:
this is the code for my html form:
<!DOCTYPE html>
<head>
<title> Question </title>
<style type = "text/css">
body {
font-family:cursive;
}
a:link {
text-decoration:none;
background-color:#D0D0D0;
color:#000000;
width:100px;
display:block;
text-align:center;
padding:4px;
}
a.visited {
text-decoration:none;
background-color:#D0D0D0;
color:#000000;
width:100px;
display:block;
text-align:center;
padding:4px;
}
a.active {
text-decoration:none;
background-color:#D0D0D0;
color:#000000;
width:100px;
display:block;
text-align:center;
padding:4px;
}
a:hover {
background-color:#686868;
color:#FFFFFF;
}
#title {
text-align:center;
}
</style>
</head>
<body>
<?php
session_start();
?>
<h1 id="title"> Question 1 </h1>
<br/>
<form action="q15.php" method="POST" >
<fieldset>
<legend>Who wrote the music we all recognise from the Paralympics?</legend>
<p>
<input
type="checkbox"
value="your friend"
name="answer"
/>Your Friend
</p>
<p>
<input
type="checkbox"
value="public friend"
name="answer"
/>Public Friend
</p>
<p>
<input
type="checkbox"
value="your enemy"
name="answer"
/>Your Enemy
</p>
<p>
<input
type="checkbox"
value="public enemy"
name="answer"
/>Public Enemy
</p>
<p>
<input
type="submit"
value="Submit"
/>
</p>
</fieldset>
</form>
</body>
</html>
这是我的页面的代码,它将处理数据并更新具有空白空间的数据库(如现在)
and this is the code for my page which will process the data and update a database which has empty spaces left blank to be filled in later (as in now)
<body>
<h1 id="title"> Quiz </h1>
<?php
session_start();
$connection = mysql_connect("mysql15.000webhost.com", "a4987634_quiz", "********")
or die (mysql_error());
mysql_select_db("a4987634_quiz", $connection)
or die (mysql_error());
$fname = $_SESSION['fname'];
$lname = $_SESSION['lname'];
$id = $_SESSION['ID'];
$answer = $_POST['answer'];
$id = mysql_query("SELECT ID FROM users WHERE fname=$fname LIMIT 1");
if(isset($_POST['answer']) &&
$_POST['answer'] == 'public enemy')
{
?>
<h3 id = "correct"> Correct </h3>
<?php
$sqlcorrect = "UPDATE users SET q1 = correct WHERE ID = $ID LIMIT 1";
mysql_query($sqlcorrect);
(mysql_error());
}
else {
?>
<h3 id = "incorrect"> Incorrect </h3>
<?php
$sqlwrong = "UPDATE users SET q1 = 'wrong' WHERE ID = $ID LIMIT 1";
mysql_query($sqlwrong);
(mysql_error());
}
?>
</body>
</html>
我可以完美地连接到数据库,并且知道您何时得到正确或不正确的问题,但是我的问题当你尝试更新数据库时,它不会这样做。有没有人有任何解决方案?也没有错误信息。它没有任何意义!
I can connect to the database perfectly and it knows when you get the question correct or incorrect but my problem is when you try to update the database it won't do it. Does anyone have any solutions? There is no error message as well. it doesn't make sense!
答
您的查询存在以下几个问题:
There are several issues with your query:
- PHP中的变量名称区分大小写
- 更新查询可以有一个LIMIT,但是因为给出了一个id,所以这里没有意义。
- 当更新字符串时,它们需要被追加。
id
提供:
This should work, when the right id
is provided:
$sqlwrong = "UPDATE `users` SET `q1` = 'some text' WHERE `ID` = $id";