使用Select和其他参数插入stmt

时间:2014-11-18 16:45:03

标签: php mysql sql

是否可以使用"混合" SQL插入如下? 我希望能够从另一个表中获取一个值(需要一个参数),然后输入另外两个参数。

    $sql = "INSERT INTO tblquestions (userID, questionText, questionAnswer) VALUES (
    Select userID FROM tblusers WHERE userEmail = (?),?,?)";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt, 'sss', $userEmail, $question, $answer);
    $result = mysqli_stmt_execute($stmt);
    if (!$result) {
        throw new Exception($conn->error);
    }   

1 个答案:

答案 0 :(得分:1)

没必要。只需使用insert . . . select

INSERT INTO tblquestions(userID, questionText, questionAnswer)
    Select userID, ?, ?
    FROM tblusers
    WHERE userEmail = (?);