【发布时间】:2015-03-20 03:44:21
【问题描述】:
我的考试网页是用 php 代码开发的。
我想知道如何从链接中自动检索 WHERE 值。
例如我的网页链接是:
http://localhost/Exam/OES/OES/oes/admin/rsltmng.php?testid=4
当我点击其他试卷的另一个结果时,链接将变为 http://localhost/Exam/OES/OES/oes/admin/rsltmng.php?testid=3
我想知道如何将 testid 值自动传递到我的 sql 代码中。
目前我的 sql 代码将如下所示: //您问题的代码格式。
`$result3=executeQuery("SELECT x.stdid, x.testid, x.questionsInTest, x.questionsCorrect, ROUND( 100.0 * x.questionsCorrect / x.questionsInTest ) AS percentScore FROM ( SELECT s.stdid, q.testid, COUNT(q.qnid) AS questionsInTest, SUM( IF(s.answered='answered' AND s.stdanswer=q.correctanswer, 1, 0 ) ) AS questionsCorrect FROM question AS q INNER JOIN studentquestion AS s ON q.testid = s.testid AND q.qnid = s.qnid WHERE q.testid=".$_REQUEST['testid']." GROUP BY s.stdid, q.testid ) AS X ORDER BY x.stdid, x.testid;"); `
当我输入这段代码时,我的页面上没有显示结果。
==== 如下是所涉及的表格。
问题表:
CREATE TABLE IF NOT EXISTS `question` (
`testid` bigint(20) NOT NULL DEFAULT '0',
`qnid` int(11) NOT NULL DEFAULT '0',
`question` varchar(500) DEFAULT NULL,
`optiona` varchar(100) DEFAULT NULL,
`optionb` varchar(100) DEFAULT NULL,
`optionc` varchar(100) DEFAULT NULL,
`optiond` varchar(100) DEFAULT NULL,
`correctanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
`marks` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `question`
--
INSERT INTO `question` (`testid`, `qnid`, `question`, `optiona`, `optionb`, `optionc`, `optiond`, `correctanswer`, `marks`) VALUES
(1, 1, 'question for test 1', 'ans1', 'ans 2', 'ans 3', 'ans 4', 'optiona', 1),
(2, 1, 'question test', 'ans1', 'ans2', 'ans3', 'ans4', 'optiona', 1);
学生题表:
CREATE TABLE IF NOT EXISTS `studentquestion` (
`stdid` bigint(20) NOT NULL DEFAULT '0',
`testid` bigint(20) NOT NULL DEFAULT '0',
`qnid` int(11) NOT NULL DEFAULT '0',
`answered` enum('answered','unanswered','review') DEFAULT NULL,
`stdanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `studentquestion`
--
INSERT INTO `studentquestion` (`stdid`, `testid`, `qnid`, `answered`, `stdanswer`) VALUES
(1, 1, 1, 'answered', 'optiona'),
(1, 2, 1, 'answered', 'optiona');
【问题讨论】:
-
你需要一步一步来。听起来你根本不知道发生了什么。你所说的和期望的有太多错误。
-
您正在使用 $_REQUEST 却一无所获?你在期待什么?能否请您回显 SQL 语句以查看 $_REQUEST 存储了什么?
-
它将存储以下详细信息
-
好的,你能告诉我们表格结构和涉及的那些php代码吗?
-
$SQL = "SELECT x.stdid, x.testid, x.questionsInTest, x.questionsCorrect, ROUND( 100.0 * x.questionsCorrect / x.questionsInTest ) AS percentScore FROM ( SELECT s.stdid, q.testid, COUNT(q.qnid) AS questionsInTest, SUM( IF(s.answered='answered' AND s.stdanswer=q.correctanswer, 1, 0 ) ) AS questionsCorrect FROM question AS q INNER JOIN studentquestion AS s ON q.testid = s.testid AND q.qnid = s.qnid WHERE q.testid=".$_REQUEST['testid']." GROUP BY s.stdid, q.testid ) AS X ORDER BY x.stdid, x .testid;”;回声 $SQL;告诉我你得到了什么。