简体   繁体   English

上载CSV文件,PHP / MySQL查询

[英]Uploading CSV File, PHP/MySQL Query

i have a .csv file that looks like this... 我有一个看起来像这样的.csv文件...

     Name            AC-No.      Time                State        Exception   Operation

Johnny Starks Depp   1220    4/12/2013 12:45:18 AM  Check In        OK   
Johnny Starks Depp   1220    4/12/2013 5:46:58 AM   Out             Out 
Johnny Starks Depp   1220    4/12/2013 6:22:41 AM   Out Back        Out  
Johnny Starks Depp   1220    4/12/2013 10:42:17 AM  Check Out       Repeat   
Johnny Starks Depp   1220    4/12/2013 10:42:19 AM  Check Out       OK

i can already upload this to my database. 我已经可以将此上传到我的数据库了。 my problem is that, the result it shows in the table. 我的问题是,结果显示在表格中。 In mysql database I have a TABLE with columns ( Name, ACNo. , Date, CheckIn, Breakout, Breakin, CheckOut ). 在mysql数据库中,我有一个带有列( 名称,ACNo。,日期,CheckIn,Breakout,Breakin,CheckOut )的表。 What I want to happen that i cannot do right is.. the record/s in the .csv file with the same DATE ( which is shown in the TIME Column ) will be in just one same row along with the Check In, BreakOut, BreakIn, and CheckOut in the TABLE and wont result a multiple record. 我想做的是我无法正确处理的..具有相同DATE (在TIME Column中显示)的.csv文件中的记录将与Check In,BreakOut,在表中的BreakIn和CheckOut不会导致多条记录。 like this.. 像这样..

     Name         |ACNo |     Date  | CheckIn     |  Breakout  |   Breakin  | Checkout
                  |     |           |             |            |            |
Johnny Starks Depp|1220 | 4/12/2013 | 12:45:18 AM | 5:46:58 AM | 6:22:41 AM | 10:42:19 AM

Now.. i already did the above example of what i want to happen in the table but... when i check the table, yes it has different dates per row but the time in (CheckIn, Breakout, Breakin, Checkout) are wrong. 现在..我已经做了上面要在表中发生的上述示例,但是...当我检查表时,是的,每行有不同的日期,但是(CheckIn,Breakout,Brekin,Checkout)中的时间是错误的。 Wrong, because the time for example in the DATE 4/12/2013 is recorded in the DATE 4/13/2013. 错误,因为例如DATE 4/12/2013中的时间记录在DATE 4/13/2013中。 What is wrong with the codes i used? 我使用的代码有什么问题? Or is there any way else i can do this? 还是我还有其他方法可以做到这一点? these are my codes: 这些是我的代码:

<?php

$conn = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("dtrlogs",$conn);

$datehere ="";

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)
{
    $Name = $fileop[0];
    $ACNo = $fileop[1];
    $Time = $fileop[2];
    $State = $fileop[3];
    $NewState = $fileop[4];
    $Exception = $fileop[5];
    $Operation = $fileop[6];

    //i separated time/date in the Column TIME in the .csv file
    $date = date('m/d/Y', strtotime($Time));
    $hours = date('H:i:s A', strtotime($Time));


    //this is what i used to prevent multiple dates
    if($datehere != $date) {

        $datehere = $date;
    }
    else{

        $date = "";
    }







    //this is to insert the name and acno to the other table. 
    //(this is for the lists of employee)

    $sql=mysql_query("SELECT * FROM employee Where ACNo = '$ACNo'");
    $row=mysql_fetch_array($sql);
    if($row == 0) {

       $sql1 = mysql_query("INSERT INTO employee(ACNo, Name) VALUES('$ACNo','$Name')");
    }



    //Inserts record if Date is not yet recorded and if already has just updates 
    //the row's column (Checkin, breakout, breakin and Checkout) 
    //with its correct time according to the date

    $query = mysql_query("SELECT * FROM dtrs Where ACNo = '$ACNo' AND Date = '$date'");
    $rows = mysql_fetch_array($query);
    if($rows == 0) {

    $sql2 = mysql_query("INSERT INTO dtrs(Name, ACNo, Date, CheckIn, BreakOut, 
    BreakIn,CheckOut) 
    VALUES('$Name','$ACNo','$date','$CheckIn','$Breakout','$Breakin','$Checkout')");

    }
    else{

    $sql2 = mysql_query("Update dtrs Set CheckIn = '$CheckIn', BreakOut = 
    '$Breakout', BreakIn = '$Breakin', CheckOut = '$Checkout'");
    }







    //this is my conditions to identify whether the TIME/HOUR is 
    //Stated as CheckIn, Breakout, Breakin, or Checkout

    if($NewState == "Check In" || $State == 'Check In' && $Exception == "OK") {

        if($Exception == "Invalid" || $Exception == "Repeat")
        {}
        else {
            $CheckIn = $hours;
        }
    }
    if($State == 'Out' && $Exception == "Out") {

        if($Exception == "Invalid" || $Exception == "Repeat")
        {}
        else {
            $Breakout = $hours;
        }
    }
    if($State == 'Out Back' && $Exception == "Out") {

        if($Exception == "Invalid" || $Exception == "Repeat")
        {}
        else {
            $Breakin = $hours;
        }
    }
    if($NewState == "Check Out" || $State == "Check Out" && $Exception == "OK") {

        if($NewState == "Check In")
        {}
        else {
            $Checkout = $hours;
        }
     }


}


if($sql2)
{
if($sql2)
{


?>
    <script type="text/javascript">
    alert('File Upload Successful!');
    </script>
<?php
}
}
}

}

?>

I would suggest possibly loading to a transient table, and from that extracting the details to use to populate your real table. 我建议可能加载到临时表中,然后从中提取详细信息以用于填充实际表。

For example, if you loaded you CSV file to a table called LoadTable (not a temp table, as you can only use a temp table once in a piece of SQL), you could extract the distinct name and account number, then join that against subselects to get the max event times for each of the relevant events. 例如,如果将CSV文件加载到名为LoadTable的表(不是临时表,因为在一个SQL中只能使用一个临时表),则可以提取唯一的名称和帐号,然后将其与子选择可获取每个相关事件的最大事件时间。

Something like this:- 像这样的东西:

SELECT DerivMain.Name, 
    DerivMain.ACNo, 
    DerivMain.EventDate, 
    DATE_FORMAT(DerivCheckIn.EventDateTime, '%l:%i:%s %p'), 
    DATE_FORMAT(DerivBreakout.EventDateTime, '%l:%i:%s %p'),  
    DATE_FORMAT(DerivBreakin.EventDateTime, '%l:%i:%s %p'), 
    DATE_FORMAT(DerivCheckout.EventDateTime, '%l:%i:%s %p')
FROM (SELECT DISTINCT Name, ACNo, DATE(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p')) As EventDate FROM LoadTable) DerivMain
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Check In' GROUP BY Name, ACNo) DerivCheckIn
ON DerivMain.Name = DerivCheckIn Name AND DerivMain.ACNo = DerivCheckIn.ACNo 
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Out' GROUP BY Name, ACNo) DerivBreakout
ON DerivMain.Name = DerivBreakout Name AND DerivMain.ACNo = DerivBreakout.ACNo 
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Out Back' GROUP BY Name, ACNo) DerivBreakin
ON DerivMain.Name = DerivBreakin Name AND DerivMain.ACNo = DerivBreakin.ACNo 
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Check Out' GROUP BY Name, ACNo) DerivCheckout
ON DerivMain.Name = DerivCheckout Name AND DerivMain.ACNo = DerivCheckout.ACNo 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM