Assume the field data type is varchar
When i add a integer in it, it will post and add it as string,
and assume the field data type is int
When i add a varchar in it, it will post and add it as “0” in the database,
So are there anyway to check whether the datatype is equal to what is in the database?
All i want to check is varchar, integer and boolean
Thank you
//get column name
try {
$q = $conn->prepare("DESCRIBE subscriber");
$q->execute();
$tableField = $q->fetchAll(PDO::FETCH_COLUMN);
}
catch(PDOException $e)
{
die ($e->getMessage()."<a href='view.php' onClick='window.location.reload()'> Back</a>");
}
foreach ($tableField as $set)
{$counter=0;
//if ($set!='Email')
$subCol[]=$set; //column name
$noOfColumn[]=$counter; //how much column
}
//get column datatype
foreach ($noOfColumn as $set)
{
try{
$sql ='SELECT s.*
FROM subscriber s,list_sub ls
where ls.ListID = ?
AND s.SubID=ls.SubID
';
$select = $DB->query($sql);
$meta = $select->getColumnMeta($set);
$dataType[]=$meta[$subCol['$set']]; //an arrary of datatype
}
catch(PDOException $e)
{
die ($e->getMessage().'<a href="viewAll.php"> Back</a>');
}
}
//assume input1='int' , input2='varchar' ,input3='bool'
//column name in database = input box name
foreach ($dataType as $compare)
{if($compare[$subCol['FieldName']]==gettype(mixed $input1))
echo "ok, same type";
else
echo "no, not same type";
}
I see no point in such automated verifications.
Especially the former one.
You have to implement application-level verifications, checking if value meets certain conditions.
BTW, Mysql doesn’t have boolean field type.