从值插入到MySQL表并选择语句

时间:2018-07-04 21:58:34

标签: php pdo

我想通过从另一个表中选择字段将值插入一个表中,并添加日期和login_id之类的信息(这些都不来自我所选择的表)。

$sql = "INSERT INTO questions_to_answer (login_id, question_id, Question_Category, QuestionType, Question, Meaning) VALUES (?,?,?,?,?,?)";
$stmt= $DB_con->prepare($sql);

$stmt->execute($test_user_id, 
    "SELECT question_id, 
            Question_Category, 
            QuestionType, 
            Question, 
            Meaning all_questions 
      WHERE personality_profile_questions.Question_Category = 1");

1 个答案:

答案 0 :(得分:0)

您需要在var a = req.body.lab[1] 中包含SELECT,在执行中不能包含SQL命令。如果在执行中,它将被视为文字字符串,并用引号引起来并转义。

尝试:

prepare

假设$sql = "INSERT INTO questions_to_answer (login_id, question_id, Question_Category, QuestionType, Question, Meaning) SELECT ?, question_id, Question_Category, QuestionType, Question, Meaning WHERE personality_profile_questions.Question_Category = 1"; $stmt= $DB_con->prepare($sql); $stmt->execute(array($test_user_id)); = $test_user_id。我不清楚login_id是什么。