My columns headings are id, name, phone. If i gave name in id column the data is not inserting but message is showing as "successful import". I want to show the message like the "data is not properly defined". How to validate code before importing into mysql?
I tried to check with the if and else conditions but didn't get the actual result which i need.
$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>";
}
I expect the alert message as "improper data" but getting "successful import".
you can use is_int(), to check wether the value (in your case $getData[0]) is integer or not.
before adding value in query
if(!is_int($getData[0])
{
echo "<script type=\"text/javascript\"> alert(\"Improper Data.\");
}</script>";
}
else
{
// insert query code here
}
Validation should be done before any INSERT, this way you can do it:
<?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;
}
the code above uses typing, if you are using an old php version change
function validateCsvRow(array $data) : bool
to:
function validateCsvRow($data)
Field order in CSV can vary, eg. column 0 must not always be "id".
Consider this CSV:
name, id, phone
'Alice', 1, 1234
'Bob', 2, 5678
and this code:
$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'] ]);
}