query help

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

User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

query help

Post by C_Calav »

hi guys,

i have this query where i insert:

q_id - auto number (done)
s_id - fk (done
q_no -
q_text - (done)


now i got everything insert correctly except for the q_no.

now every time there is a new s_id the q_no will insert at 1 and increment up. example, 1, 2, 3, 4 ....

and if there is not a new s_id, i want it to carry on from where left off, for example, 4,5,6,7....

can anyone help me or give me a way on how i can achieve this?

thanx
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

Could you post some of your code for the insert? I'm assumming that you are using mySQL? You've not really given enough info for any of us to help you!

Obviously you know what you want to do, but there's a bug in your code. Post your code.

Paul
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

and the SQL statement would be handy as well to see how it all fits together.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey guys,

sorry about that heres some more info

Code: Select all

<?php
     session_start(); 
     ob_start();
     
	include("conection.php"); 

	$sur_id = $_GET&#1111;'sur_id'];	     

	if ($_SERVER&#1111;'REQUEST_METHOD'] == "POST") 
        &#123;
		$q_text = $_POST&#1111;"txt_question"]; 

		$sql  = "INSERT INTO tbl_question (s_id,q_text) VALUES ('$sur_id','$q_text')";
		$result = mysql_query($sql) or die ("Execution failed: ".mysql_error());

		echo "sur_id: $sur_id"; 

		header("Location: /survey/questions.php?sur_id=$sur_id"); 

		echo "<br /><br /> <strong>new question added</strong> <br /><br />";

           
	&#125; 
	else 
	&#123; 
      		echo "<br /><br /> <strong>not added</strong>"; 
	&#125; 

?>
now i want to add in the q_no, 1 2 3 4 5...

but now if i have a diff s_id (survey id) i want to carry on from where the q_no left off.

for example..

if the s_id is 3 and the q_no is 4, when im in the questions page and i insert another question i want that one to insert at no 5.

by the way its a online survey system might make it easier to understand. thanx!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's wrong with using auto_increment?
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

i guess i can, but will it auto_increment from 1 again when there is a new s_id?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no. your last post made it sound like you wanted the number to always be unique..

in order to determine the next value, you'll have to perform a lookup.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

feyd wrote:no. your last post made it sound like you wanted the number to always be unique..

in order to determine the next value, you'll have to perform a lookup.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

ok i see,

here is another example..

s_id = 1
q_no = 6 next value 7

s_id = 2
q_no = 3 next value 4

s_id = 3
q_no = 10 next value 11

so ill use a lookup? and what would my insert be still a auto_increment?
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

ps feyd:
in order to determine the next value, you'll have to perform a lookup.
what shall i search to get any info on a lookup?

i am unable to find anything good, thanx
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a standard select query would be one. :)
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

how do i select the last value where the s_id = $s_id?

thanx
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT g_id FROM table WHERE s_id = $s_id ORDER BY g_id DESC LIMIT 1
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx feyd,

whats the g_id

and whats
ORDER BY g_id DESC LIMIT 1
this for?

thanx again
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sorry.. q_no

that makes sure to get only the highest value one.
Post Reply