Page 1 of 2

UPDATE statement for mysql

Posted: Wed May 30, 2007 4:15 am
by otd
Please can someone help with this statement as im a bit of a newbie to php

Code: Select all

$question_id=$_GET['question_id']; 

$query = "UPDATE forum_question SET reply=reply-1 WHERE id='$question_id'";
basically i have got the question_id being passed within the url from the previous page, and i would like this statement to simply minus one off the figure that is in the reply column where the id column within the table equals the question_id being passed.

At the moment the code isnt adjusting anything with in mysql.

Any help would be greatly appreciated.

Many Thanks In Advance

Chris

Posted: Wed May 30, 2007 4:19 am
by volka
Where do you call mysql_query() ?
echo $query and check the statement.

Posted: Wed May 30, 2007 4:22 am
by djot
-

Code: Select all

$question_id=$_GET['question_id'];

$query = "UPDATE forum_question SET reply=reply-1 WHERE id='".$question_id."'";
Missing quotes (") around the variable?

djot
-

Posted: Wed May 30, 2007 4:23 am
by otd
When i enter that i get;

UPDATE forum_question SET reply=reply-1 WHERE id=''

Posted: Wed May 30, 2007 4:25 am
by djot
-
Well,

take away the single and double quotes around the variable or do it like I posted (with both sinlge and double quotes around).

Make sure the GET is set and sanitized (never trust user input).

djot
-

Posted: Wed May 30, 2007 4:26 am
by otd
i have entered that from djot but the reply column still isnt updating.....any ideas???

Posted: Wed May 30, 2007 4:28 am
by djot
-
Well, try it manually then ... and use a database manager to catch the error...

Code: Select all

UPDATE forum_question SET reply=4 WHERE id=100
What type is reply? INT?

djot
-

Posted: Wed May 30, 2007 4:30 am
by otd
yeah reply is int(4)

Re: UPDATE statement for mysql

Posted: Wed May 30, 2007 4:37 am
by volka
try

Code: Select all

<?php
if ( !isset($_GET['question_id']) ) {
	echo '<pre>_GET: '; print_r($_GET); echo "</pre>\n";
	die('missing parameter');
}

$question_id=(int)$_GET['question_id']; 
$query = "UPDATE forum_question SET reply=reply-1 WHERE id=$question_id";

Posted: Wed May 30, 2007 4:44 am
by otd
Hello Volka, thank you for you help so far,

i replaced my code with what you stated and got
GET: Array
(
[a_id] => 2question_id=27
)

missing parameter
any ideas?

Posted: Wed May 30, 2007 4:50 am
by volka
You have an error in your html form code.

Posted: Wed May 30, 2007 5:05 am
by otd
dont suppose you know where this error could be??

can you give me any direction?

many thanks

Chris

Posted: Wed May 30, 2007 5:37 am
by volka
Your html form should look like

Code: Select all

<form action="..." method="...">
	<input name="a_id" ... />
	<input name="question_id" ... />
but somehow you've managed to build the form in a way that the browser interprets 2question_id=27 as the value of the field named a_id. How you did that escapes me.

Posted: Wed May 30, 2007 5:50 am
by otd
ok ive had a look but cant find anything like this, the page previous to this is passing 2 ids in the url, will this make any difference??

heres the code for it

Code: Select all

<td colspan="3" bgcolor="#F8F7F1"><a href="../forumgeneral/delete_answer.php?a_id=<? echo $rows['a_id']; ?>question_id=<? echo $rows['question_id']; ?>" class="style8"><? echo Yes; ?></a></td>
Many thanks so far

Chris

Posted: Wed May 30, 2007 5:55 am
by volka
ah ok, it's a link not a form. The link must look like script.php?param1=value1&param2=value2
You need to add an ampersand that separates the two name=value pairs.