簡體   English   中英

如何通過上傳 CSV 文件來使用 php 更新數據庫?

[英]How to update a database using php by uploading a CSV file?

我這里有一些代碼可以根據 CSV 中的兩列更新我的數據庫。 CSV 文件如下所示:

身份響應

1 你好1

2 你好2

3 你好3

在我的數據庫中,我有一個表,其中還包含一個 id 列和一個與響應匹配的額外列。 這個想法是這個 CSV 文件已上傳,並將填充與 ID 號匹配的響應。

基本上是這樣的:“ UPDATE tbl_data SET response = {$response} WHERE id = {$id}

執行此操作的表單如下所示:

<form method="post" name="uploadCSV" enctype="multipart/form-data">
    <label>Choose CSV File</label>
    <input type="file" name="csv_file" id="file" accept=".csv" />
    <button type="submit" name="import" class="read-more smaller">Upload</button>
</form>

但是,我不認為我已經理解如何正確執行此操作,因為我收到 SQL 錯誤,或者表格只是坐在那里,好像什么都沒發生一樣。 請參閱下面的代碼。

if (isset($_POST["import"])) {

    if($_FILES["csv_file"]["name"]){

        $filename = explode(".", $_FILES["csv_file"]["name"]);

        if(end($filename) == "csv"){

            $handle = fopen($_FILES["csv_file"]["tmp_name"], "r");

            while ($data = fgetcsv($handle)){

                $id = $data[0];
                $response = $data[1];

                $query ="UPDATE tbl_data SET response = {$response} WHERE id = {$id}";
                $update_data = mysqli_query($connection,$query);

                if (!$update_data) {
                    $message = "There was a problem updating your CSV file. If this problem reoccurs, please contact admin";
                    die (mysqli_error($connection));
                }

            }

            fclose($handle);

            header("Location: upload.php?uploaded=1");

        } else {
            $message = "You can only upload a CSV file.";
        }

    } else {
        $message = "Please select a CSV file.";
    }

}

我有 $message 來顯示消息。 但它沒有顯示任何消息,並且數據庫中的更新似乎也沒有發生。

我的代碼中是否有任何我可能忽略的錯誤? 還是有更好的方法來做到這一點?

通過使用以下方法使其工作

if(isset($_POST["importcsv"])){

        $file = $_FILES["csv_file"]["tmp_name"];
        $handle = fopen($file,"r");

        while ($row = fgetcsv($handle)) {

            $id = $row[0];
            $response = $row[1];

            $sql = "UPDATE table SET response = ? WHERE id = ?";
            $update_data_stmt = mysqli_stmt_init($connection);

            if (!mysqli_stmt_prepare($update_data_stmt, $sql)){
                die("Something went wrong with the upload. " . mysqli_error($connection));
            } else {
                mysqli_stmt_bind_param($update_data_stmt, "ss", $response, $id);
                mysqli_stmt_execute($update_data_stmt);
                if ($id == "ID" && $response == "Response"){
                    echo "";
                } else {
                    echo "Lead <b>{$id}</b>'s response was updated to <b>{$response}</b>.</p>";
                }
            }

        }

    }

我懷疑你的數據庫出了問題,因為你說你的$message在這一切之后被回顯但是是空的。 假設您的$data包含正確的信息(我建議var_dump()確保),我只能想象該語句沒有按應有的方式執行。

我不太精通mysqli ,但是您可能需要考慮使用PDO來查看它是否有效。 它還可以保護您免受來自 csv 文件的潛在 SQL 注入攻擊:

$conn = new PDO(db_host, db_user, db_pw)
while ($data = fgetcsv($handle)) {
    $id = $data[0];
    $response = $data[1];

    $sql = "UPDATE tbl_data SET response = :response WHERE id = :id";
    $st = $conn->prepare($sql)
    $st->bindValue(":response", $response, PDO::PARAM_STR)
    $st->bindValue(":id", $id, PDO::PARAM_INT)
    $st->execute();
    //Output
    print_r($st->errorInfo());
}
$conn = null;

試試這個, errorInfo()應該會給你足夠的日志來看看有什么問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM