Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用PHP上传Mysql Excel时,如何验证手机号码列?_Php_Html_Mysql - Fatal编程技术网

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

在使用PHP上传Mysql Excel时,如何验证手机号码列?,php,html,mysql,Php,Html,Mysql,在使用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("$",

在使用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>';
}
}

为了防止空白导入,您应该在执行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\u match

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