'PHP preg_match regex send warning if not find the text

There is a table in my database, I want to fetch records in the table and compare with the text which user has entered, if they match, fetch and show the other fields of that row of the table.

this is my code:

$q = "SELECT * FROM Kala2  WHERE 1";
$res = mysqli_query($db, $q);
while ($row = mysqli_fetch_assoc($res)) //For each word
 {
  $t = $row[sharhe_kala];
  $p = $row[mark];

  if (preg_match("/$t/i", $text)) {
       if (preg_match("/$p/i", $text)) {
             *** do something ***
                           }}

it works but when the word which has entered by user ($text) is not in database it sends this warning:

PHP Warning: preg_match(): Unknown modifier '1' in

also doesn't answer when the $row[sharhe_kala] or $row[mark] is numerical

for example if $t="KOYO" and $p="Ballbearing" and user enter $text="koyo ballbearing" it shows the result and it's ok, but if user enters $text="ntn zzz" its sends this warning:

PHP Warning: preg_match(): Unknown modifier '1' in

and if $t="222" and $p="Ballbearing" and user enter $text="222 ballbearing" it doesn't show the result and doesn't show any warning.



Solution 1:[1]

From preg_match:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Solution 2:[2]

You must escape the variables that you send directly in regexp.

$text = "222 ballbearing";
$t = 222;
$p = "Ballbearing";

if (preg_match("/{$t}/i", $text)) {
   if (preg_match("/{$p}/i", $text)) {
       echo "Match detected";
   }
}

Solution 3:[3]

Since the question mentions... ...the other dropdown also automatically chooses the same value and this works both ways...

Try the following, ..

const leadStatusList = document.getElementById('leadstatus');
const leadStatus2List = document.getElementById('leadstatus2');

const setDropdownValue = (event) => {
  leadStatusList.value = event.target.value;
  leadStatus2List.value = event.target.value;
}

leadStatusList.onchange = setDropdownValue;
leadStatus2List.onchange = setDropdownValue;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry
Solution 2 ustmaestro
Solution 3 Nalin Ranjan