php update is not updating database [closed]

2020-05-03 12:59发布

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!

2条回答
▲ chillily
2楼-- · 2020-05-03 13:32
$sqlcorrect = "UPDATE users SET q1 = 'correct' WHERE ID = $id";

unless correct is another column name then you need to surround strings with single quotes. And as the comment says, $ID might be $id depending on your intentions. Should be defined either way though

查看更多
Root(大扎)
3楼-- · 2020-05-03 13:33

There are several issues with your query:

  • Variable names in PHP are case sensitive
  • An update query can have a LIMIT but because a single id is given it makes no sense here.
  • When updating strings they need to be quouted.

This should work, when the right id is provided:

$sqlwrong = "UPDATE `users` SET `q1` = 'some text' WHERE `ID` = $id";
查看更多
登录 后发表回答