PHP& SQL Server:如何将数据插入2个表并插入多行

时间:2017-03-07 20:03:01

标签: php sql-server post sqlsrv

我正在尝试使用php将数据从html输入到MSSQL数据库中。我无法在2个不同的表中插入记录而无法在表格中插入多个记录,我有以下代码

<?php
$name = $_POST["name"];

$email = $_POST["email"];

$company = $_POST["company"];

$contact = (int)$_POST["contact"];

$worktitle = $_POST["worktitle"];

$industry = $_POST["industry"];

$V101 = $_POST["part2q1"];    
$V102 = $_POST["part2q2"];    
$V103 = $_POST["part2q3"];    
$V104 = $_POST["part2q4"];    
$V105 = $_POST["part2q5"];    
$V106 = $_POST["part2q6"];    
$V107 = $_POST["part3q1"];    
$V108 = $_POST["part3q2"];    
$V109 = $_POST["part3q3"];    
$V110 = $_POST["part3q4"];    
$V111 = $_POST["part3q5"];    
$V112 = $_POST["part3q6"];    
$V113 = $_POST["part4q1"];    
$V114 = $_POST["part4q2"];    
$V115 = $_POST["part4q3"];    
$V116 = $_POST["part4q4"];    
$V117 = $_POST["part4q5"];    
$V118 = $_POST["part4q6"];    
$V119 = $_POST["part5q1"];    
$V120 = $_POST["part5q2"];    
$V121 = $_POST["part5q3"];    
$V122 = $_POST["part5q4"];    
$V123 = $_POST["part5q5"];    
$V124 = $_POST["part5q6"];    
$V125 = $_POST["part6q1"];    
$V126 = $_POST["part6q2"];    
$V127 = $_POST["part6q3"];    
$V128 = $_POST["part6q4"];    
$V129 = $_POST["part6q5"];    
$V130 = $_POST["part6q6"];    
$V131 = $_POST["part7q1"];    
$V132 = $_POST["part7q2"];    
$V133 = $_POST["part7q3"];    
$V134 = $_POST["part7q4"];    
$V135 = $_POST["part7q5"];    
$V136 = $_POST["part7q6"];    
$V137 = $_POST["part7q7"];    
$V138 = $_POST["part7q8"];    
$V139 = $_POST["part8q1"];    
$V140 = $_POST["part8q2"];    
$V141 = $_POST["part8q3"];    
$V142 = $_POST["part8q4"];    
$V143 = $_POST["part8q5"];    
$V144 = $_POST["part8q6"];    
$currenttime = date("Ymd h:m:sa");    
$server = "***";    
$connOptions = array("Database"=>"**", "UID"=>"**", "PWD"=>"**!");    
$conn = sqlsrv_connect($server, $connOptions);    

if($conn){    
    $query="INSERT INTO dbo.profile (    
        name,    
        email,    
        company,    
        telephone,    
        worktitle,    
        industry,    
        createdate    
        )    
VALUES (?, ?, ?, ?, ?, ?,getdate())";    
    $params = array(    
        $name,    
        $email,    
        $company,    
        $contact,    
        $worktitle,    
        $industry,      
        $currenttime    
        );    
    if(sqlsrv_query($conn, $query, $params)){    
        echo "<h4>Thank you</h4><p>You have completed the survey and your     answers have been received.</p>";    
    } else {    
        echo "<p>We're sorry but there has been and error receiving your     answers.</p>";    
    }    
} else {    
    echo "<p>We're sorry but there has been and error receiving your answers.    </p>";    
}    

我试图将记录插入另一个表格,如上一行继续:

if($conn){    
$query1="INSERT INTO dbo.SurveyResponse (    
        profileid,    
        Value,    
        CreatedOn    
        )    
VALUES ('2', ?, ?, ?, ?, ?,getdate())";    
    $params=array($V101,$currenttime);    

$query1="INSERT INTO dbo.SurveyResponse (    
        profileid,    
        Value,    
        CreatedOn    
        )    
VALUES ('2', ?, ?, ?, ?, ?,getdate())";    
    $params=array($V102,$currenttime);    

    $query1="INSERT INTO dbo.SurveyResponse (    
        profileid,    
        Value,    
        CreatedOn    
        )    
VALUES ('2', ?, ?, ?, ?, ?,getdate())";    
    $params=array($V103,$currenttime);    

.    .    .    .    .    
if(sqlsrv_query($conn, $query1, $params))        
{    
        echo "<h4>Thank you</h4><p>You have completed the survey and your     answers have been received.</p>";    
    } else {    
        echo "<p>We're sorry but there has been and error receiving your     answers.</p>";    
    }    
} else {    
    echo "<p>We're sorry but there has been and error receiving your answers.    </p>";    
}    

?>

我一直在尝试这个,插入适用于第一个表但不适用于第二个表,任何人都可以帮忙吗

1 个答案:

答案 0 :(得分:0)

以下工作方式让我输入多个记录到第二个表。感谢Miken32

if($conn){
    $query1="INSERT INTO dbo.SurveyResponse (
            profileid,
            Qid,
            Value,
            CreatedOn
            )
    VALUES (?, ?, ?,getdate())";
        $params1=array(2,101,$V101,$currenttime);

    if(sqlsrv_query($conn, $query1, $params1))
    {
            echo "";
        } 
        else { echo"<p>We're sorry but there has been and error receiving your answers.</p>" ; }
        }


    if($conn){
    $query2="INSERT INTO dbo.SurveyResponse (
            profileid,
            Qid,
            Value,
            CreatedOn
            )
    VALUES (?, ?, ?,getdate())";
        $params2=array(2,102,$V102,$currenttime);

    if(sqlsrv_query($conn, $query2, $params2))
    {
            echo "";
        } 
        else { echo"<p>We're sorry but there has been and error receiving your answers.</p>" ; }
        }