使用php

时间:2015-11-06 14:23:04

标签: php html sql-server sql-server-2008

我正在尝试帮助简化我们的CSV上传,目前我们只是进入SQL,右键单击并导入CSV文件。我想创建一个页面,允许一次将多个文件上传到SQL数据库。我已经有一段时间不习惯PHP,而且对C#更熟悉。这是我到目前为止所得到的,但似乎根本没有工作。我已经将连接细节命名为

<html>
<head>
    <title>Renewal Uploader</title>
</head>
<body>
    <image src="logo.png">
    <h2>Renewal Uploader</h2><p />

    Please select the files you are wanting to upload below:<p />
    <input name="upload[]" type="file" multiple="multiple" />
    <label>
        <form action="test.php" method="post">
            <input type="submit" name="submit" id="submit" value="Submit" />
        </form>
    </label>
    <?php
    //Database Connection Info
    $connectionInfo = array(
        "UID"=>'*********',
        "PWD"=>'*********',
        "Database"=>"TEST_EDIDB"
    );

    /* Connect using SQL Server Authentication. */
    $conn = sqlsrv_connect('**.**.**.**, ****', $connectionInfo);
    if( $conn === false )
        {
        echo "Unable to connect.</br>";
        die( print_r( sqlsrv_errors(), true));
        }
    $polno = $_POST["polno"];

    if((isset($_POST['submit']) && $_POST["polno"] != '')){
        for($i=0; $i<count($_FILES['upload']['name']); $i++) {
        //Get the temp file path
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

        //Make sure we have a filepath
        if ($tmpFilePath != ""){
            //Setup our new file path
            $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

            //Upload the file into the temp dir
                if(move_uploaded_file($tmpFilePath, $newFilePath)) {

                    $file_handle = fopen($newFilePath, "r");

                        while (!feof($file_handle) ) {

                        $line_of_text = fgetcsv($file_handle);

                        $tsql = "INSERT INTO OutboundEDI ([Agent No], [Broker Mailbox], [Message Type], [Envelope Reference], [Policy Number], [Effective Date], Status, [Trans  Date and Time], [Kewill Reference])
                        VALUES ('$line_of_text[0]','$line_of_text[1]', '$line_of_text[2]', '$line_of_text[3]', '$line_of_text[4]', '$line_of_text[5]', '$line_of_text[6]', '$line_of_text[7]', '$line_of_text[8]' )"; 

                        if( sqlsrv_query( $conn, $tsql))
                            {
                            echo "Statement executed.\n";
                            } 
                        else
                            {
                            echo "Error in statement execution.\n";
                            die( print_r( sqlsrv_errors(), true));
                            }

                        } 
                }
            }
        }
    }
?>

</body>

有没有人有任何指示或建议可以帮助我解决这个问题?

0 个答案:

没有答案