我从select查询得到结果,但是当我尝试增加$ slot的值然后在我的表中插入2行时,我想只添加1行,增加的插槽值。
这是我的代码
try{
$slot_query="Select max(slot) AS maxslot from mdl_quiz_slots where quizid=$quiid";
$queryResult=db_query($slot_query);
foreach ($queryResult as $row) {
$slot=$row->maxslot;
$slot++;
$question_query ="INSERT INTO mdl_quiz_slots ".
"(slot,quizid,page,requireprevious,questionid,maxmark)".
"VALUES ".
"('$slot','$quiid','$page','$requireprevious','$questid','$maxmark')";
$queryResult=db_query($question_query);
}
答案 0 :(得分:1)
我并不认真地提倡这个解决方案,但无论如何你都可以玩这个......
CREATE TABLE my_table (i INT NOT NULL UNIQUE);
INSERT INTO my_table SELECT COALESCE(MAX(i)+1,1) FROM (SELECT 1) x, my_table LIMIT 1;
SELECT * FROM my_table;
+---+
| i |
+---+
| 1 |
+---+
INSERT INTO my_table SELECT COALESCE(MAX(i)+1,1) FROM (SELECT 1) x, my_table LIMIT 1;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
SELECT * FROM my_table;
+---+
| i |
+---+
| 1 |
| 2 |
+---+
2 rows in set (0.00 sec)
答案 1 :(得分:0)
我不知道这里是否有足够的信息可以解决,因为我感觉你曾经两次调用整个试用片段。
因为您正在调用select max查询,所以我只期望一个结果,但不确定您的自定义数据库包装类中发生了什么。因此,您可能会尝试将insert语句移出循环
try{
$slot_query="Select max(slot) AS maxslot from mdl_quiz_slots where quizid=$quiid";
$queryResult=db_query($slot_query);
foreach ($queryResult as $row) {
$slot=$row->maxslot;
$slot++;
}
$question_query ="
INSERT INTO mdl_quiz_slots
(slot,quizid,page,requireprevious,questionid,maxmark)
VALUES
('$slot','$quiid','$page','$requireprevious','$questid','$maxmark');
";
$queryResult=db_query($question_query);
}