我在php中的INSERT INTO SELECT FROM语句是否正确?

时间:2018-05-28 01:40:58

标签: php mysql

我在PHP中有一个MySQL语句将一行移动到另一个表但它仍然显示:

*您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“1”附近使用正确的语法 *

这是我的代码:

$sql = mysqli_query($conn,"
INSERT INTO userdocs 
SELECT transaction_no
     , document_type
     , date_received
     , application_no
     , hei
     , school_name
     , from_co
     , other_govt
     , contact_person
     , comment
     , program
     , year_level
     , academic_year 
  FROM records 
 WHERE transaction_no = '$transaction_no'
");

if(!mysqli_query($conn, $sql))


            {  
                echo (mysqli_error($conn));
            }  

else
{
    echo "Document Sent! Going back to Home Page...";
    header("refresh:3; url=director.php");
}

1 个答案:

答案 0 :(得分:1)

可能包含$transaction_no会导致格式错误。尝试检查SQL语句。通常,在处理动态参数时应使用预处理语句:

$stmt = $db->prepare('insert into userdocs select transaction_no, document_type, date_received, application_no, hei, school_name, from_co, other_govt, contact_person, comment, program, year_level, academic_year from records where transaction_no = ?');

$stmt->bind_param('s', $transaction_no);

$result = $stmt->execute();

或使用PDO