Undefined variable error in array

I have a collection of check boxes all with the name c but different values. From php8 the code below works but gives me an 'Undefined variable $sql_c’ warning (perhaps this error was just suppressed in earlier versions?). I was given the code years back and have now updated it with numerous ‘if isset()’ functions, but if I do that to the $sql_c function, I get no results. Any help would be appreciated.

if (isset($_POST['submit']) && isset($_POST['c'])) {

if(isset($_POST['c']) && $_POST['c']=="") {$c = array(); }

foreach ($_POST['c'] as $cID) {

$sql_c .= ",".$cID;  // "Undefined variable $sql_c" warning

if (isset($numCat)) {$numCat = $numCat + 1;} 


$sql = $sql . $sql_c;         

$sql="SELECT"." ".$sql." "."FROM records ORDER BY fname";

$result=@mysqli_query($dbcnx, $sql);

Using .= you are joining to an existing string. But that string has yet to be defined.
You could start by creating the variable as an empty string before the loop: $sql_c = "";
But maybe, instead of the foreach, you should look at implode() to make the array into a string.

And $numCat I guess is some attempt at a counter. Instead just use count().

Also, seriously consider validating the inputs. Putting the variable, made up of user input, directly into the query is very dangerous.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.