Greetings!
I have an app that my users rely to update customer data in our local database. I want to add a "control" whereby a check for customer number is done to make sure it exists.
I started with this simple script
<?php
//variable declaration
$customer_id = '257';
// DEBUG - get type => string
echo gettype($customer_id);
//db settings
$dbconf = include '../config/dbConfig.php';
//storage array declaration
$arr = array();
//declaring result as TRUE: we assume all values entered are found in array
$result = TRUE;
//we connect to db host
$con = mysql_connect($dbconf["host"], $dbconf["user"],$dbconf["password"]) ;
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//we connect to database
$db_selected = mysql_select_db($dbconf["db"], $con) or die ("Couldn't select the database.");
if (!$db_selected) {
die ('Could not use '.$dbconf["db"].' : ' . mysql_error());
}
//we get a list of customer ids.
$result=mysql_query("select distinct customer_id from cases where status!='Closed'");
while($row=mysql_fetch_array($result)) {
$arr[] = $row['customer_id'];
}
// DEBUG - outputs contents => works! Approx. 300+ customer ids
print_r($arr);
// DEBUG - get type => array!
echo gettype($arr);
//we check if input matches any value in array.
(bool)$result = in_array($customer_id, $arr);
//we close databse connection and return boolean.
mysql_close($con);
if ((bool)$result == FALSE) {
echo 'not in array';
} else {
echo 'in array';
}
?>
It works perfect. However, I added the same logic to the body of code for my app...
...
$customer_id = $_POST['customer_id'];
$case_status = $_POST['status_change'];
/*
* Check customerid validity.
*/
$arr = array();
// DEBUG - get type => string
echo gettype($customer_id);
//we get a list of customerids.
$result=mysql_query("select distinct customer_id from cases where status!='Closed'");
while($row=mysql_fetch_array($result)) {
$arr[] = $row['customer_id'];
}
// DEBUG - outputs contents => empty!
print_r($arr);
// DEBUG - get type => array
echo gettype($arr);
//we check if input matches any value in array.
(bool)$result = in_array($customer_id, $arr);
//did we return FALSE?.
if ((bool)$result == FALSE) {
echo 'not in array' . (bool)$result;
} else {
echo 'in array' . (bool)$result;
}
...and it does NOT work. The array is always empty and in_array() always returns FALSE!
What am I missing? Can someone please tell me! I think I am going nuts.
Thank you.