如何过滤在Excel中重复的数字发送数据到MySQL,我想在Php中显示警报消息


How to filter the repeated numbers in the Excel to send the data to MySQL and I want to show the alert message in Php?

我尝试的代码仅用于将excel数据导出到MySql数据库。

$source = fopen('email.csv', "r");
                $query = '';
                while (($data = fgetcsv($source, 1000)) !== FALSE) {
                    for ($i = 1; $i < 2; $i++) {
                        $name[$i] = $data[0];
                        $email[$i] = $data[1];
                        $mobile[$i] = $data[2];

                        $query.="INSERT INTO table tablename (`name1`,`email`, mobile) VALUES ('" . $name[$i] . "','" . $email[$i] . "','" . $mobile[$i] . "') ;";
                        //return $query;
                    }
                }
                $connection->createCommand($query)->execute();

我想过滤重复的数字,并显示用于过滤数字的警报消息…这可能吗?

有可能:试试这段代码,只插入唯一的数据。

$source = fopen('email.csv', "r");
$query = '';
while (($data = fgetcsv($source, 1000)) !== FALSE) {
    $duplicate_raw = false;
    $name = $data[0];
    $email = $data[1];
    $mobile = $data[2];
    if( in_array($name , $name_array[] ){
        echo 'Alredy exists: ' . $name . '<br>';}
        $duplicate_raw = true;
    else{
        $name_array[]   = $name;}
    if( in_array($mobile , $mobile_array[] ){
        echo 'Alredy exists: ' . $mobile . '<br>';}
        $duplicate_raw = true;
    else{
        $mobile_array[]   = $mobile;}
    if( in_array($email , $email_array[]  ){
        echo 'Alredy exists: ' . $email . '<br>';}
        $duplicate_raw = true;
    else{
        $email_array[]   = $email;}

    if ($duplicate_raw == false){
        $query.="INSERT INTO table tablename (`name1`,`email`, mobile) VALUES ('" . $name . "','" . $email . "','" . $mobile . "') ;";}//return $query;
}
$connection->createCommand($query)->execute();