在php中导入mysql数据库之前,如何检查csv文件中的每一列数据?
我的列标题是id、姓名、电话。如果我在id列中给出了名称,则数据不会插入,但消息显示为“成功导入”。我想显示类似“数据未正确定义”的消息。如何在导入mysql之前验证代码 我试图检查if和else条件,但没有得到我需要的实际结果在php中导入mysql数据库之前,如何检查csv文件中的每一列数据?,php,mysqli,import-csv,Php,Mysqli,Import Csv,我的列标题是id、姓名、电话。如果我在id列中给出了名称,则数据不会插入,但消息显示为“成功导入”。我想显示类似“数据未正确定义”的消息。如何在导入mysql之前验证代码 我试图检查if和else条件,但没有得到我需要的实际结果 $sql = "INSERT IGNORE INTO `all`(`id`, `name`,`phone`) VALUES ('".$getData[0]."','".$getData[1]."','".$getData[2]."')"; $result = mys
$sql = "INSERT IGNORE INTO `all`(`id`, `name`,`phone`) VALUES ('".$getData[0]."','".$getData[1]."','".$getData[2]."')";
$result = mysqli_query($conn, $sql);
if($result)
{
echo "<script type=\"text/javascript\">alert(\"CSV File has been successfully Imported.\");
}</script>";
else {
echo "<script type=\"text/javascript\"> alert(\"Invalid File Format/ Data Please check file is csv and data as per headings.\");
}</script>";
}
$sql=“将忽略插入到`all`(`id`、`name`、`phone`)值(“$getData[0]”、“$getData[1]”、“$getData[2]”)中;
$result=mysqli\u查询($conn,$sql);
如果($结果)
{
echo“警报(\”CSV文件已成功导入。\”;
}";
否则{
echo“警报(\”无效文件格式/数据,请检查文件是否为csv,数据是否符合标题。\”;
}";
}
我希望警报消息为“不正确的数据”,但得到“成功导入”。您可以使用is_int(),检查值(在您的示例中,$getData[0])是否为整数
在查询中添加值之前
if(!is_int($getData[0])
{
echo "<script type=\"text/javascript\"> alert(\"Improper Data.\");
}</script>";
}
else
{
// insert query code here
}
如果(!is_int($getData[0])
{
回显“警报(\“数据不正确.\”);
}";
}
其他的
{
//在此处插入查询代码
}
您可以使用is_int()检查值(在您的例子中,$getData[0])是否为整数
在查询中添加值之前
if(!is_int($getData[0])
{
echo "<script type=\"text/javascript\"> alert(\"Improper Data.\");
}</script>";
}
else
{
// insert query code here
}
如果(!is_int($getData[0])
{
回显“警报(\“数据不正确.\”);
}";
}
其他的
{
//在此处插入查询代码
}
任何插入前都应进行验证,这样您可以:
<?php
//validation of csv
if (false === validateCsvRow($getData)) {
echo "data is not properly defined";
// or better to throw an exception - uncomment to see difference
// throw new \InvalidArgumentException("data is not properly defined");
die; //end script execution
}
// if validation is okay script continues.
$sql = "INSERT IGNORE INTO `all`(`id`, `name`,`phone`) VALUES ('".$getData[0]."','".$getData[1]."','".$getData[2]."')";
$result = mysqli_query($conn, $sql);
if($result)
{
echo "<script type=\"text/javascript\">alert(\"CSV File has been successfully Imported.\");
}</script>";
else {
echo "<script type=\"text/javascript\"> alert(\"Invalid File Format/ Data Please check file is csv and data as per headings.\");
}</script>";
}
function validateCsvRow(array $data) : bool
{
$result = 1;
//check if id is not set
if (!isset($data[0]) {
$result *= 0;
}
//check if id is not integer
if (!is_int($data[0]) {
$result *= 0;
}
//check if name is not set
if (!isset($data[1]) {
$result *= 0;
}
//check if name is not string
if (!is_string($data[1]) {
$result *= 0;
}
$minimumNameLength = 1; //number of characters
//check if name has at least n characters
if (!strlen($data[1]) < $minimumNameLength) {
$result *= 0;
}
//return 1 => true if all was ok
//or 0 => false if at least one check was not ok.
return (bool) $result;
}
致:
应在任何插入之前进行验证,这样您可以:
<?php
//validation of csv
if (false === validateCsvRow($getData)) {
echo "data is not properly defined";
// or better to throw an exception - uncomment to see difference
// throw new \InvalidArgumentException("data is not properly defined");
die; //end script execution
}
// if validation is okay script continues.
$sql = "INSERT IGNORE INTO `all`(`id`, `name`,`phone`) VALUES ('".$getData[0]."','".$getData[1]."','".$getData[2]."')";
$result = mysqli_query($conn, $sql);
if($result)
{
echo "<script type=\"text/javascript\">alert(\"CSV File has been successfully Imported.\");
}</script>";
else {
echo "<script type=\"text/javascript\"> alert(\"Invalid File Format/ Data Please check file is csv and data as per headings.\");
}</script>";
}
function validateCsvRow(array $data) : bool
{
$result = 1;
//check if id is not set
if (!isset($data[0]) {
$result *= 0;
}
//check if id is not integer
if (!is_int($data[0]) {
$result *= 0;
}
//check if name is not set
if (!isset($data[1]) {
$result *= 0;
}
//check if name is not string
if (!is_string($data[1]) {
$result *= 0;
}
$minimumNameLength = 1; //number of characters
//check if name has at least n characters
if (!strlen($data[1]) < $minimumNameLength) {
$result *= 0;
}
//return 1 => true if all was ok
//or 0 => false if at least one check was not ok.
return (bool) $result;
}
致:
CSV中的字段顺序可能会有所不同,例如,第0列不能总是“id” 以CSV为例:
name, id, phone
'Alice', 1, 1234
'Bob', 2, 5678
该代码:
$sql = "INSERT IGNORE INTO `all`(`id`, `name`,`phone`) VALUES (?, ?, ?)";
$stmt = $db->prepare($sql);
$header = null;
while($row = fgetcsv($handle)) {
if ($header == null) {
$header = $row;
continue;
}
$data = array_combine($header, $row);
// $data is now an assoziative array, eg. you can grab "id" by name
$stmt->execute([ $data['id'], $data['name'], $data['phone'] ]);
}
CSV中的字段顺序可能会有所不同,例如,第0列不能总是“id” 以CSV为例:
name, id, phone
'Alice', 1, 1234
'Bob', 2, 5678
该代码:
$sql = "INSERT IGNORE INTO `all`(`id`, `name`,`phone`) VALUES (?, ?, ?)";
$stmt = $db->prepare($sql);
$header = null;
while($row = fgetcsv($handle)) {
if ($header == null) {
$header = $row;
continue;
}
$data = array_combine($header, $row);
// $data is now an assoziative array, eg. you can grab "id" by name
$stmt->execute([ $data['id'], $data['name'], $data['phone'] ]);
}