首页  >  问答  >  正文

如何解决无法更新“日期列”的问题:更新MySQL数据库表时如何正确上传CSV文件

<p>我的代码中的主要问题是我无法使用php将csv文件中的“date”更新到mysql数据库表中。代码行$ date = mysqli_real_escape_string($ connect,$ data [1]);是这里的主要问题。我正在寻找这个特定代码行的任何替代查询。</p> <p>这是csv文件:https://drive.google.com/file/d/1EdMKo-XH7VOXS5HqUh8-m0uWfomcYL5T/view?usp=sharing</p> <p>这是完整的代码:</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 = explode(“。”,$ _FILES ['产品文件'] ['名称']); if(end($文件名)==“csv”){ $handle = fopen($ _FILES ['product_file'] ['tmp_name'],“r”); while($数据 = fgetcsv($句柄)){ $data_id = mysqli_real_escape_string($连接,$data[0]); $date = mysqli_real_escape_string($connect,$data[1]); //我的问题 $births = mysqli_real_escape_string($连接,$数据[2]); $query =“更新my_table SET日期='$日期', 出生数='$出生数', WHERE data_id ='$data_id'”; mysqli_query($连接,$查询); } fclose($句柄); header(“位置:index.php?updation = 1”); } else { $message ='<label class =“text-danger”>请仅选择CSV文件</label>'; } } else { $message ='<label class =“text-danger”>请选择文件</label>'; } } if(isset($ _GET [“updation”])){ $message ='<label class =“text-success”>产品更新完成</label>'; } $query =“SELECT * FROM my_table”; $result = mysqli_query($ connect,$ query); ?> <!DOCTYPE html> <html> <head> <title>通过使用PHP上传CSV文件更新Mysql数据库</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> <body> <br /> <div class =“container”> <h2 align =“center”>通过使用PHP上传CSV文件更新Mysql数据库</a></h2> <br /> <form method =“post” enctype ='multipart/form-data'> <p><label>请选择文件(仅限CSV格式)</label> <input type =“file” name =“product_file”/></p> <br /> <input type =“submit” name =“upload” class =“btn btn-info” value =“上传”/> </form> <br /> <?php echo $message; ?> <h3 align =“center”>Birthss</h3> <br /> <div class =“table-responsive”> <table class =“table table-bordered table-striped”> <tr> <th>日期</th> <th>出生</th> </tr> <?php while($ row = mysqli_fetch_array($ result)) { echo ' <tr> <td>'.$row [“date”].'</td> <td>'.$row [“births”].'</td> </tr> '; } ?> </table> </div> </div> </body> </html></pre></p>
P粉529581199P粉529581199419 天前503

全部回复(1)我来回复

  • 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

    回复
    0
  • 取消回复