如何选择然后在phpmyadmin中插入?

时间:2016-01-29 12:37:34

标签: php mysql

如何正确选择然后将结果插入$ result?

$result = mysql_query("SELECT AVG(answervalue) 
                        FROM `tblanswer` 
                        WHERE questiontype = 'Methods' 
                         AND studentid = '$username' 
                         AND subjectname = '$server_subject' 
                         AND professorname = '$server_name', $connect);

$Query = "INSERT INTO tblevaluationgrade (result) VALUES ('$result')";

2 个答案:

答案 0 :(得分:5)

您可以使用INSERT ... SELECT语法在一个查询中执行此操作:

INSERT INTO tblevaluationgrade (result)
    SELECT AVG(answervalue) FROM `tblanswer` ...

你应该切换到PDO或mysqli和准备好的语句,因为mysql_*函数已被弃用并且已从php 7中删除,并且你的查询看起来很容易被sql注入。

答案 1 :(得分:4)

您的查询应该是:

INSERT INTO tblevaluationgrade(result_column) 
   (SELECT AVG(answervalue) FROM `tblanswer`   
     WHERE questiontype = 'Methods' AND studentid = '$username'   
       AND subjectname = '$server_subject' AND professorname = '$server_name')