Hi
I'm new to this forum and quite new to PHP. I'm trying to insert records into a table and I want to determine if this is the first record (question_id=NULL) or there are more. If this is the first record then I want the value for question_id to become 1, else I want it to become the maximum question_id value plus 1. Here is the code :
$query_for_max = "SELECT MAX(question_id) FROM questions";
if (mysql_query($query_for_max) == NULL)
{
$query_insert = "INSERT INTO questions (question_id) VALUES (1)";
mysql_query($query_insert);
$question_id = 1;
}
else
{
$question_id = (mysql_query($query_for_max) + 1);
$query_insert = "INSERT INTO questions (question_id) VALUES ($question_id)";
I've tried both cases by altering the table data, but I haven't accomplished anything yet. Any ideas appreciated
?>
Db prob
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Code: Select all
$query = "SELECT MAX(question_id) FROM questions";
$result = mysql_query($query);
$value = mysql_fetch_array($result);
echo $value[0];