UPDATE statement for mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

UPDATE statement for mysql

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Where do you call mysql_query() ?
echo $query and check the statement.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

Post by otd »

When i enter that i get;

UPDATE forum_question SET reply=reply-1 WHERE id=''
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
Last edited by djot on Wed May 30, 2007 4:26 am, edited 1 time in total.
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

Post by otd »

i have entered that from djot but the reply column still isnt updating.....any ideas???
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

Post by otd »

yeah reply is int(4)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: UPDATE statement for mysql

Post 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";
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You have an error in your html form code.
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

Post by otd »

dont suppose you know where this error could be??

can you give me any direction?

many thanks

Chris
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply