PHP正则表达式和CSV验证


PHP Regular Expressions and CSV Validation

我正在尝试创建一个CSV检查器,它将检查的数据插入数据库,并将任何不成功的数据添加到.txt文件中。我正试图使用正则表达式来验证我插入的数据,没有任何验证的while循环可以正常工作并插入,但一旦使用正则表达式,它就不起作用。

<?php
    include_once('connection.php');
    error_reporting(E_ALL);
    date_default_timezone_set('Europe/London');
    $date = date('d/m/y h:i:s a', time());
    $filetxt = "./errors.txt";
    $errors = array();
    $var1 = 5;
    $var2 = 1000;
    $var3 = 10;
    $sql = '';
    if(isset($_POST["Import"]))
    {
        echo $filename=$_FILES["file"]["tmp_name"]; 
        if($_FILES["file"]["size"] > 0)
        {
            $file = fopen($filename, "r");
            while(($emapData = fgetcsv($file, 10000, ",")) !==FALSE)
            {
                if(isset($_GET['strProductCode']))
                {
                    $emapData[0] =  $conn->real_escape_string(trim($_POST['strProductCode']));
                    if (!preg_match("^[a-zA-Z0-9]+$^", $_POST['strProductCode']))
                    {
                        $errors['strProductCode'];
                    }
                }
                if(isset($_GET['strProductName']))
                {
                    $emapData[1] =  $conn->real_escape_string(trim($_GET['strProductName']));
                    if (!preg_match("^[a-zA-Z0-9]+$^", $_POST['strProductName']))
                    {
                        $errors['strProductName'];
                    }
                }
                if(isset($_GET['strProductDesc']))
                {
                    $emapData[2] =  $conn->real_escape_string(trim($_GET['strProductDesc']));
                    if (!preg_match("^[a-zA-Z0-9]+$^", $_POST['strProductDesc']))
                    {
                        $errors['strProductDesc'];
                    }
                }
                if(isset($_GET['intStock']))
                {           
                    if (!preg_match("^[0-9]", $_POST['intStock']))
                    {
                        $errors['intStock'];
                    }
                }
                if(isset($_GET['intPrice']))
                {
                    if (!preg_match("[0-9]", $_POST['intPrice']))
                    {
                        $errors['intPrice'];
                    }
                }
                if(isset($_GET['dtmDiscontinued'])){
                    if($emapData[6] == preg_match("[a-zA-Z]", $_POST['dtmDiscontinued']))
                    {
                        $emapData[6] = $date;
                        echo $date;
                    }else{
                            $emapData[6] = Null;
                        }
                }
                if(count($errors > 0))
                {
                    // errors 
                    $write = "$emapData[0], $emapData[1], $emapData[2], $emapData[3], $emapData[4], $emapData[5], $emapData[6]'r'n";     
                    file_put_contents($filetxt , $write , FILE_APPEND);
                }else{
                    // insert into Database
                        $sql = "INSERT INTO tblproductdata(strProductCode, strProductName, strProductDesc, intStock, intPrice, dtmAdded, dtmDiscontinued) VALUES('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$date','$emapData[6]')";
                    $res=$conn->query($sql);
                    }
            }
            fclose($file);
            echo "CSV File has successfully been Imported";
            echo "<br>";
            echo "Any errors within the CVS Database are reported here.";
            echo "<br>";
            $fh = fopen($filetxt, 'r');
            $theData = fread($fh, filesize($filetxt));
            fclose($fh);
            echo $theData;
        }else{
            echo "Invalid File: Please Upload a Valid CSV File";    
        }
            header("Location: index.php");
    }
?>

我的PHP知识不是很好,但这是我最好的尝试。如有任何帮助,我们将不胜感激。

您的代码中存在几个问题。让我们从正则表达式和错误检查开始:

  1. 您的某些表达式无效。请注意,每个表达式的开头和结尾都需要一个分隔字符。在某些表达式(如^[0-9](中,缺少这些分隔符。还请注意,使用^作为正则表达式的分隔符不是一个好的选择,因为^字符在正则表达式中也有特殊含义。

    这实际上应该导致PHP警告。我看到您启用了error_reporting;您还应该查看您的display_errors设置。

  2. 正如我在评论中提到的,您没有为$errors数组分配任何值。语句$errors['strProductName']本身不会更改数组;这意味着CCD_ 8将总是空的。你可能想做这样的事情:

    $errors['strProductName'] = TRUE;
    
  3. 实际上,您正在检查count($errors > 0),而应该检查count($errors > 0)。CCD_ 11转换为CCD_ 12或CCD_。

其他注意事项:

  1. 有时,您会检查$_GET['strProductCode'],但随后会使用$_POST['strProductCode']
  2. 您不会为每次迭代重置$errors数组。这意味着,对于您读取的每一行,$errors变量仍将包含上一次迭代中的错误。因此,第一个无效行也将导致后面的所有行被识别为无效
  3. 当其中一个参数的格式无效时,您会注册错误,但当其中某个参数根本没有设置时(即isset($_POST[...])FALSE时(则不会注册错误。它们中的每一个都可能是这样的东西:

    if (isset($_POST['strProductCode'])) {
        $emapData[0] = $conn->real_escape_string(trim($_POST['strProductCode']));
        if (!preg_match("^[a-zA-Z0-9]+$^", $_POST['strProductCode'])) {
            $errors['strProductCode'] = TRUE;
        }
    } else {
        $errors['strProductCode'] = TRUE;
    }