1

每次用户创建考试时,我想插入一个连续的项目编号。每次他创建考试时,它都会重新开始为 1。我非常需要你的帮助,人们。更多的权力。:)

这是我的代码:

$examid = $_SESSION['examid'];
$itemnum = 1;

$sql = mysql_query("INSERT INTO exam_questions (question_description, question_type, question_exam_id) VALUES ('$question', '$type', '$examid')")or die(mysql_error());
$lastinsertid = mysql_insert_id();
mysql_query("UPDATE exam_questions SET question_set_id='<??????????>' WHERE question_id='$lastinsertid' LIMIT 1")or die(mysql_error());
4

2 回答 2

1

尝试通过加入来执行此操作:

UPDATE exam_questions eq cross join
       (select max(question_set_id) as qsi
        from exam_questions
        where question_id='$lastinsertid'
       ) as const
    SET eq.question_set_id = const.qsi + 1
    WHERE question_id='$lastinsertid';
于 2013-08-17T12:01:40.633 回答
0

你可以尝试这样的事情:

SELECT COUNT(`id`) INTO @tmpvar FROM `exam_questions` WHERE `question_exam_id`='$examid';
INSERT INTO `exam_questions`
    (`question_description`,`question_type`,`question_exam_id`,`question_id`)
    VALUES ('$question','$type','$examid',@tmpvar+1);

请注意,这些必须作为单独的mysql_query调用发送,但它应该可以很好地工作。我还假设您已经正确地转义了参数。

于 2013-08-17T12:03:20.770 回答