Page 1 of 2
query help
Posted: Thu Jan 20, 2005 7:18 pm
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
Posted: Fri Jan 21, 2005 10:57 am
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
Posted: Fri Jan 21, 2005 11:57 am
by patrikG
and the SQL statement would be handy as well to see how it all fits together.
Posted: Mon Jan 24, 2005 2:02 pm
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ї'sur_id'];
if ($_SERVERї'REQUEST_METHOD'] == "POST")
{
$q_text = $_POSTї"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 />";
}
else
{
echo "<br /><br /> <strong>not added</strong>";
}
?>
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!
Posted: Mon Jan 24, 2005 2:13 pm
by feyd
what's wrong with using auto_increment?
Posted: Mon Jan 24, 2005 2:27 pm
by C_Calav
i guess i can, but will it auto_increment from 1 again when there is a new s_id?
Posted: Mon Jan 24, 2005 2:58 pm
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.
Posted: Mon Jan 24, 2005 3:34 pm
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.
Posted: Mon Jan 24, 2005 3:39 pm
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?
Posted: Mon Jan 24, 2005 4:28 pm
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
Posted: Mon Jan 24, 2005 4:31 pm
by feyd
a standard select query would be one.

Posted: Mon Jan 24, 2005 4:33 pm
by C_Calav
how do i select the last value where the s_id = $s_id?
thanx
Posted: Mon Jan 24, 2005 4:35 pm
by feyd
Code: Select all
SELECT g_id FROM table WHERE s_id = $s_id ORDER BY g_id DESC LIMIT 1
Posted: Mon Jan 24, 2005 4:38 pm
by C_Calav
thanx feyd,
whats the g_id
and whats
ORDER BY g_id DESC LIMIT 1
this for?
thanx again
Posted: Mon Jan 24, 2005 4:47 pm
by feyd
sorry.. q_no
that makes sure to get only the highest value one.