国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

So l?sen Sie das Problem, dass die ?Datumsspalte' nicht aktualisiert werden kann: So laden Sie eine CSV-Datei beim Aktualisieren einer MySQL-Datenbanktabelle korrekt hoch
P粉529581199
P粉529581199 2023-08-28 13:46:51
0
1
648
<p>Das Hauptproblem in meinem Code besteht darin, dass ich das ?Datum“ nicht mit PHP aus der CSV-Datei in die MySQL-Datenbanktabelle aktualisieren kann. Die Codezeile $date = mysqli_real_escape_string($connect, $data[1]); ist hier das Hauptproblem. Ich suche nach einer alternativen Abfrage für diese bestimmte Codezeile. </p> <p>Dies ist die CSV-Datei: https://drive.google.com/file/d/1EdMKo-XH7VOXS5HqUh8-m0uWfomcYL5T/view?usp=sharing</p> <p>Dies ist der vollst?ndige Code: </p> <pre class="brush:php;toolbar:false;"><?php //index.php $connect = mysqli_connect("localhost","root","1234","ml_database"); $message =''; if(isset($_POST["upload"])){ if($_FILES['product_file']['name']){ $filename = explosion(".”,$ _FILES ['product_file'] ['name']); if(end($ filename)==“csv“){ $handle = fopen($ _FILES ['product_file'] ['tmp_name'],“r“); while($ data = fgetcsv($ handle)){ $data_id = mysqli_real_escape_string($ connect,$ data [0]); $date = mysqli_real_escape_string($ connect,$ data [1]); //我的問題 $births = mysqli_real_escape_string($ connect,$ data [2]); $query = ?UPDATE my_table SET date ='$ date', Geburten ='$ Geburten', WHERE data_id ='$ data_id'“; mysqli_query($ connect,$ query); } fclose($ handle); Header(?location:index.php?update = 1“); } anders { $message ='<label class="text-danger">Bitte w?hlen Sie nur CSV-Dateien aus</label>'; } } anders { $message ='<label class="text-danger">Bitte Datei ausw?hlen</label>'; } } if(isset($_GET["updation"])){ $message ='<label class="text-success">Produktaktualisierung abgeschlossen</label>'; } $query = "SELECT * FROM my_table"; $result = mysqli_query($connect, $query); ?> <!DOCTYPE html> <html> <Kopf> <title>Aktualisieren Sie die MySQL-Datenbank durch Hochladen einer CSV-Datei mit PHP</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <link rel="stylesheet" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <K?rper> <br /> <div class="container"> <h2 align="center">Aktualisieren Sie die MySQL-Datenbank durch Hochladen einer CSV-Datei mit PHP</a></h2> <br /> <form method="post" enctype='multipart/form-data'> <p><label>Bitte w?hlen Sie eine Datei aus (nur CSV-Format)</label> <input type = ?file“ name = ?product_file“/></p> <br /> <input type = ?submit“ name = ?upload“ class = ?btn btn-info“ value = ?upload“/> </form> <br /> <?php echo $message ?> <h3 align="center">Birthss</h3> <br /> <div class="table-responsive"> <table class="table table-bordered table-striped"> <tr> <th>Datum</th> <th>Geburt</th> </tr> <?php while($row = mysqli_fetch_array($result)) { Echo ' <tr> <td>'.$row ["Datum"].'</td> <td>'.$row ["births"].'</td> </tr> '; } ?> </table> </div> </div> </body> </html></pre></p>
P粉529581199
P粉529581199

Antworte allen(1)
P粉817354783

首先,您需要閱讀并丟棄CSV中的標(biāo)題行。然后,使用適當(dāng)?shù)摹?zhǔn)備好的和參數(shù)化的查詢,您可以正確地更新數(shù)據(jù)庫。由于.csv文件中的日期格式正確,因此不需要進(jìn)行任何操作,但是對于其他CSV文件可能不是這種情況,通常需要在將日期正確存儲到表中之前對其進(jìn)行重新格式化。

<?php
//index.php
$connect = mysqli_connect("localhost", "root", "1234", "ml_database");
$message = '';

if(isset($_POST["upload"])) {
    if($_FILES['product_file']['name']) {
        $filename = explode(".", $_FILES['product_file']['name']);

        if(end($filename) == "csv") {
            $handle = fopen($_FILES['product_file']['tmp_name'], "r");
            // 讀取并忽略標(biāo)題行
            $data = fgetcsv($handle, 1000, ",");

            // 準(zhǔn)備查詢一次
            $query = "UPDATE my_table 
                            SET date = ?, 
                                births = ?
                          WHERE data_id = ?";
            $stmt = $connect->prepare($query);

            // 循環(huán)遍歷csv剩余的行
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $stmt->bind_param('sss', $data[0],$data[1],$data[2]);
                $stmt->execute();
            }
            fclose($handle);
            header("location: index.php?updation=1");
            exit;   // 重定向后一定要使用exit
        } else {
            $message = '';
        }
    } else {
        $message = '';
    }
}

注意:我假設(shè)所有3列都是文本類型的。

$stmt->bind_param('sss', $data[0],$data[1],$data[2]);
                   ^^^

如果date_id是整數(shù)類型,您可以將其更改為'ssi',盡管3個s通常也能正常工作。

參考資料:

fgetcsv
mysqli_prepare
mysqli_bind_param

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage