php update没有更新数据库

时间:2012-10-25 18:41:30

标签: php html mysql phpmyadmin

这是我的html表单的代码:

<!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>

我可以完美地连接到数据库,它知道你何时得到正确或不正确的问题但我的问题是当你尝试更新数据库时它不会这样做。有没有人有任何解决方案?也没有错误消息。它没有意义!

2 个答案:

答案 0 :(得分:6)

您的查询存在以下几个问题:

  • PHP中的变量名称区分大小写
  • 更新查询可以有LIMIT,但因为给出了一个id,所以这里没有任何意义。
  • 更新字符串时,需要对其进行查询。

当提供正确的id时,这应该有效:

$sqlwrong = "UPDATE `users` SET `q1` = 'some text' WHERE `ID` = $id";

答案 1 :(得分:2)

$sqlcorrect = "UPDATE users SET q1 = 'correct' WHERE ID = $id";

除非另一列名称正确,否则您需要用单引号括起字符串。正如评论所说,$ ID可能是$ id,具体取决于您的意图。应该以任何方式定义