这是我的HTML表单代码:PHP更新不更新数据库
<!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>
,这是我的网页,其中将处理数据并更新其具有空的空间留白是一个数据库的代码填写后(如现在)
<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>
我可以连接到数据库的完美,它知道,当你的问题正确或不正确,但我的问题是,当您尝试更新它不会做数据库。有没有人有任何解决方案?也没有错误信息。它没有任何意义!
你在哪里定义'$ ID'? – andrewsi
它在fname和lname下面。高于其他ID。我只是注意到这是不对的。这可能是问题所在。多谢你们。 –
这就是您定义'$ id'的地方 - 变量名称区分大小写。第二个'$ id'是一个记录集,您需要从中获取结果,然后才能将其添加到查询中。 – andrewsi