Updating info to database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Updating info to database

Post by Linkjames »

No doubt a case of missing somthing obvious, but this one has me stumped. Code should update a MySql row. But it dosen't (Suprise). Any ideas?

Code: Select all

<html>
<head>
<title>Question update</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<? // First part of script time script ?>
<?PHP include("../includes/exetime1.php"); ?>
<body>
<?PHP
//Connect to MySql
include("../includes/connect.php");
//Select Database
include("../includes/dbselect.php");
// Set ID variable from the GET URL
$answer = $_POST['answer'];
$updatequestion = @mysql_query("UPDATE search SET answer='$answer' answered=1 WHERE id'=$id'");
echo ('Question answered<P>');
?>

<?PHP // Second part of script time script ?>
<?PHP include("../includes/exetime2.php"); ?>
</body>
</html>
Thanks for any help - Linkjames
User avatar
wmasterj
Forum Commoner
Posts: 40
Joined: Mon Aug 18, 2003 5:52 pm
Location: Stockholm, Sweden

Post by wmasterj »

I'm pretty sure this should work

Code: Select all

<?php
mysql_query("UPDATE search SET(answer, answered) VALUES($answer, $answered)");
?>
but then maybe that whas for INSERT ... ? try it!.... i hope it helps! :) :wink:
User avatar
wmasterj
Forum Commoner
Posts: 40
Joined: Mon Aug 18, 2003 5:52 pm
Location: Stockholm, Sweden

Post by wmasterj »

no....i see the problem.... you forgot a ' , ' between

answer='$answer' AND answered=1
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

Thanks for trying, but thats not it. Currently trying

Code: Select all

<html>
<head>
<title>Question update</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<? // First part of script time script ?>
<?PHP include("../includes/exetime1.php"); ?>
<body>
<?PHP
//Connect to MySql
include("../includes/connect.php");
//Select Database
include("../includes/dbselect.php");
// Set ID variable from the GET URL
$answer = $_POST['answer'];
$id = $_POST['id'];
answered = ('1');
$updatequestion = @mysql_query("UPDATE search SET (answer, answered) VALUES ($answer, $answered) WHERE id = '$id'");
echo ('Question answered<P>');
?>

<?PHP // Second part of script time script ?>
<?PHP include("../includes/exetime2.php"); ?>
</body>
</html>
but it still dosen't work...bugger
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

$updatequestion = @mysql_query("UPDATE search SET answer = '$answer', answered = '$answered' WHERE id = '$id'");
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

Again, thank you, but it still won't play...
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

*Sigh* I am in fact a muppet. I simply forgot to carry the ID variable from the preceeding page. grrrr

Thanks for your help guys, and sorry to have wasted your time.

Linkjames
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Yes that would of course help alot. ;)
User avatar
wmasterj
Forum Commoner
Posts: 40
Joined: Mon Aug 18, 2003 5:52 pm
Location: Stockholm, Sweden

Post by wmasterj »

hehe...always happy to give a hand - :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

For future reference, removing the SQL statement from the mysql_query() call can make debugging easier, as can mysql_error(), e.g. instead of

Code: Select all

$updatequestion = @mysql_query("UPDATE search SET answer='$answer', answered='$answered' WHERE id = '$id'");
try

Code: Select all

$sql = "UPDATE search SET answer='$answer', answered='$answered' WHERE id = '$id'";
@$updatequestion = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
Mac
User avatar
webfinearts
Forum Newbie
Posts: 12
Joined: Tue Sep 30, 2003 12:02 pm
Contact:

Post by webfinearts »

<?php
UPDATE search SET answer='{$answer}', answered= '{$answered}'
WHERE .....
is better for you to include the string values between {}, also if you have a numeric value you will have to use just {} without quote, like
answer={$answer}
?>
Post Reply