Heim  >  Fragen und Antworten  >  Hauptteil

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>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" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/> <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粉529581199P粉529581199419 Tage vor504

Antworte allen(1)Ich werde antworten

  • P粉817354783

    P粉8173547832023-08-29 00:13:45

    首先,您需要阅读并丢弃CSV中的标题行。然后,使用适当的、准备好的和参数化的查询,您可以正确地更新数据库。由于.csv文件中的日期格式正确,因此不需要进行任何操作,但是对于其他CSV文件可能不是这种情况,通常需要在将日期正确存储到表中之前对其进行重新格式化。

    <?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");
                // 读取并忽略标题行
                $data = fgetcsv($handle, 1000, ",");
    
                // 准备查询一次
                $query = "UPDATE my_table 
                                SET date = ?, 
                                    births = ?
                              WHERE data_id = ?";
                $stmt = $connect->prepare($query);
    
                // 循环遍历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 = '';
        }
    }

    注意:我假设所有3列都是文本类型的。

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

    如果date_id是整数类型,您可以将其更改为'ssi',尽管3个s通常也能正常工作。

    参考资料:

    fgetcsv
    mysqli_prepare
    mysqli_bind_param

    Antwort
    0
  • StornierenAntwort