一个查询中有多个SQL插入

时间:2010-12-03 03:41:07

标签: php mysql

HI全部,我担心,我有一个从服务器进来的阵列。然后将数组转换为变量。如:

$fpage = $_SESSION['scores'];

$score1 = $fpage['0'];
$score2 - $fpage['1'];
//Start of Queries


 // This is what I would like to do, but Its not working: 
$sql ="INSERT INTO Score (ScoreID,ResponseID,AssessorID,CriteriaID,Score,StudentID)"
       . "VALUES ('$rand','$rand2','$adminname','1-22','$score1', '$student')"; 

           $new = mysql_query($sql, $db);
$sql2 ="INSERT INTO Score (ScoreID,ResponseID,AssessorID,CriteriaID,Score,StudentID)"
       . "VALUES ('$rand','$rand2','$adminname','1-21','$score2', '$student')"; 

           $new1 = mysql_query($sql3, $db);
$sql3 ="INSERT INTO Score (ScoreID,ResponseID,AssessorID,CriteriaID,Score,StudentID)"
       . "VALUES ('$rand','$rand2','$adminname','1-21','$score3', '$student')"; 

           $new2 = mysql_query($sql3, $db);

我的问题是,这样做的最佳方法是什么。我试图循环查询,但我无法让它工作。实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:4)

嗯,在mysql_query的中间通话中有一个拼写错误 - 你传递了$sql3而不是$sql2

另外,你可以这样做:

INSERT INTO Score (ScoreID, ResponseID, AssessorID, CriteriaID, Score, StudentID)
    VALUES
        (score1, response1, assessor1, criteria1, etc.),
        (score2, response2, assessor2, criteria2, etc.),
        (score3, response3, assessor3, criteria3, etc.),
        etc.

只会为所有插入命中一次数据库,而不是每次插入一次。

最后,您的代码似乎容易受到SQL injection的攻击。<​​/ p>