使用.csv文件替换值

时间:2015-04-23 03:58:21

标签: php mysql csv

我正在为我们的大学建立一个时间表。我已经完成了上传.csv文件,它还显示了所有数据。我的问题是,有没有什么办法可以替换我上传新的.csv文件之前上传的整个数据?我的意思是删除/替换旧数据..

这是我上传csv文件的PHP代码。

<?php
 $conn = mysql_connect("localhost","root","") or die (mysql_error());
 mysql_select_db("testing",$conn);

 if(isset($_POST['submit']))
 {
    $file = $_FILES['file']['tmp_name'];

    if($file == "")
    {
        ?>
        <script type="text/javascript">
        alert('No File Selected!');
        </script>
        <?php
    }   

    else
    {
    $handle = fopen($file,"r");

    while(($fileop = fgetcsv($handle,1000,",")) !==false)
    {
        $subj_desc = $fileop[0];
        $subj_code = $fileop[1];
        $units = $fileop[2];
        $day = $fileop[3];
        $start_time = $fileop[4];
        $end_time = $fileop[5];
        $soc = $fileop[6];
        $eoc = $fileop[7];
        $max = $fileop[8];
        $room = $fileop[9];
        $term = $fileop[10];
        $lecturer = $fileop[11];

        $sql = mysql_query("INSERT INTO `tbl_schedule_board` (`subj_desc`,`subj_code`,`units`,`day`,`start_time`,`end_time`,`soc`,`eoc`,`max`,`room`, `term`, `lecturer`)
        VALUES (
                '" . mysql_real_escape_string($subj_desc) . "',
                '" . mysql_real_escape_string($subj_code) . "',
                '" . mysql_real_escape_string($units) . "',
                '" . mysql_real_escape_string($day) . "',
                '" . mysql_real_escape_string($start_time) . "',
                '" . mysql_real_escape_string($end_time) . "',
                '" . mysql_real_escape_string($soc) . "',
                '" . mysql_real_escape_string($eoc) . "',
                '" . mysql_real_escape_string($max) . "',
                '" . mysql_real_escape_string($room) . "',
                '" . mysql_real_escape_string($term) . "',
                '" . mysql_real_escape_string($lecturer) . "')");
    }
     if($sql)
     {
        echo '<script> alert("Data Successfully Uploaded."); </script>';
     }
    }
}

&GT;

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

如果您要从表中删除所有旧数据,请使用TRUNCATE命令:

finish()