在Mysql Excel上传期间,使用PHP如何验证手机号码列


During Mysql Excel upload using PHP how to validate mobile numbers column?

在使用PHP上传excel期间,我想验证excel文件列中的每个手机号码,我必须上传有时我的代码在 MySQL 中上传空白行?还有如何让我的代码上传 CSV 文件?

<?php
ini_set("display_errors", 1);
require_once "include/connect.php";
require_once 'Excel/reader.php';
$school_id = $_SESSION['SCHOOL_ID'];
$class      = explode("$", $_REQUEST['student_class']);
$class_id   = $class[0];
$class_name = $class[1];
if (isset($_POST['submit'])) {
    $allowedExts = array(
    "xls",
    "xlsx"
);
$temp        = explode(".", $_FILES["file"]["name"]);
$extension   = end($temp);
if (($_FILES["file"]["type"] == "application/vnd.ms-excel") && ($_FILES["file"]["size"] < 524288) && in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Stored in: " . $_FILES["file"]["tmp_name"];

        $data = new Spreadsheet_Excel_Reader();
        $data->setOutputEncoding('CP1251');
        $data->read($_FILES["file"]["tmp_name"]);
        $html = "<table border='1'>";
        if (count($data->sheets[0][cells]) > 0) // checking sheet not empty
            {
            echo "<br /><br />Total no. of Students added:" . count($data->sheets[0][cells]) . "<br />";
            for ($j = 2; $j <= count($data->sheets[0][cells]); $j++) // loop used to get each row of the sheet
                {
                $html .= "<tr>";
                for ($k = 1; $k <= count($data->sheets[0][cells][$j]); $k++) // This loop is created to get data in a table format.
                    {
                    $html .= "<td>";
                    $html .= $data->sheets[0][cells][$j][$k];
                    $html .= "</td>";
                }
                $roll_no      = $data->sheets[0][cells][$j][1];
                $admission_no = $data->sheets[0][cells][$j][2];
                $st_name      = $data->sheets[0][cells][$j][3];
                $father_name  = $data->sheets[0][cells][$j][4];
                $mother_name  = $data->sheets[0][cells][$j][5];
                $st_address   = $data->sheets[0][cells][$j][6];
                $st_mobile    = $data->sheets[0][cells][$j][7];
                $st_dob       = $data->sheets[0][cells][$j][8];
                $query = "insert into student_info(roll_no,admission_no,st_name,father_name,mother_name,st_address,st_mobile,st_dob,school_id,st_class_name,st_class) values('" . $roll_no . "','" . $admission_no . "','" . $st_name . "','" . $father_name . "','" . $mother_name . "','" . $st_address . "','" . $st_mobile . "','" . $st_dob . "','" . $school_id . "','" . $class_name . "','" . $class_id . "')";
                mysql_query($query);
                $html .= "</tr>";
            }
        }
        $html .= "</table>";
        echo $html;
        echo "<br>";
        echo '<p style="color: green; text-align: left">Data sucessfully inserted in your database</p>';
    }
} else {
    echo '<p style="color: red; text-align: left">Invalid File- Check file size(size<500kb) / Check extension</p>';
}
}

我想从我的代码中得到什么-

  • 我不想上传错误的手机号码(st_mobile)
  • 手机号码列也可以留空。
  • 我的代码也可以上传csv文件
  • 有时我的代码在Mysql数据库中上传空白行,如何处理?

为了防止空白导入,您应该在执行 SQL 查询之前检查每个变量是否为空。显然,您可以根据需要从 if 语句中删除可选变量。

if($roll_no and $admission_no and $st_name and $father_name and $mother_name and $st_address and $st_mobile and $st_dob)
{
    mysql_query($query);
}

至于验证手机号码,您可以使用preg_match

$st_mobile = preg_match("/^(+440?|0)7[0-9]{9}$/", trim($st_mobile)) ? $st_mobile : "";