I have been working on an old app mostly dealing with upgrading from mysql
to mysqli
and removing depreciated functions. While debugging the mess I would occasionally have an error with in line hyperlinks to edit or delete products. Didn’t pay much attention at first as was finding other errors -- missing parameter or i syntax ect. But then discovered that items in a category could have 2 different identifiers ‘ds’ or ‘bc’ or both and the links to edit or delete these items could have one or the other or both. The problem boils down to the original code that did not account for one or the other I think originally there was just ‘ds’ and ‘bc’ was added after. Here is the original code.
// Original code
if ($_GET['act'] == 'del') {
$cat_id = $_GET['cat_id'];
$ds = $_GET['ds'];
$bc = $_GET['bc'];
if($cat_id == '' && $ds == '' || $bc == '') {
echo 'Error Cannot identify item for action!';
}
else {
//$db = mysqli_connect($db_host, $db_login, $db_pwd, $db_name);
// Do some SQL Stuff in cat_id that match ds or bc
}
I tried dozens of variations of the original if($cat_id == '' && $ds == '' || $bc == '')
using ||
&&
equal to or not equal to and one way or another could never get one to be correct in all cases. cat_id is necessary and is numeric ds and bc are mixed and item has at least one or both identifiers. Most of the examples I found on Stack dealt with all conditions true or false so I thought I would give value to a possible missing value and this is what I came up with and it works but there has to be a better way!
// From GET -- Just to fix if($cat_id == '' && $ds == '' || $bc == '') {
//isset may be better way to avoid undefined
$cat_id = 3;
$ds = 'bbb';
$bc = '';
// define some vars
$c = 'NO'; $d = 0; $b = 0; $t = 0; $s = 0;
if (is_numeric($cat_id)) {
$c = 'OK'; }
if ($ds != '') {
$d = 1;
}
if ($bc != '') {
$b = 1;
}
$t = $d + $b;
if ( $s == $t || $c != 'OK') {
echo 'Error Cannot identify item for action! ';
echo $d .' '. $b .' '. $c . ' '.$t;
}
else {
echo ' OK two out three aint bad as long as one is cat ';
echo $d .' '. $b .' '. $c . ' '.$t;
This is a working sample of what I came up with Thanks for looking any help would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire