0

i am taking textarea data which is the "firstname" one below each other and trying to search for those names in mysql database and if the name found in database then display the full information.

This is the code so far, but only works for 1 line data

<?php
session_start();
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
?>
<html>
<head>
</head>
<body>
          <form method="post">
          <textarea name="search" type="search" list="searchkey" placeholder="search"  class="search"></textarea>
          <input type="submit" name="submit" id="click" value="search"  />
<?php
      $db = new mysqli('localhost', 'root', 'root', 'skfstapel');
          if(isset($_POST['submit']))
           {
              $textarea = trim($_POST['search']);
              $search = implode("|",explode("\r\n", $textarea));
              $_SESSION['firstname']= $textarea;
            }
                if(($_SESSION['firstname']!=""))
                {
                  $view = $db->query("SELECT * FROM contacts WHERE firstname REGEXP '$search'");
                  $check = $view;
                  if($check!="")
                {
                  while($output = mysqli_fetch_array($view))
                {
              ?>
          <div class="reslt">
            <h3 id="resuil-title">
              <?php echo $output['firstname']; ?>
            </h3>
          <p class="Description">
          <hr>
          </div>
              <?php } } else { ?>
            <p class="error">
              <?php echo $view; ?>
            <p>
              <?php } }  ?>
        </form>
</body>
</html>

I tried using " $search = explode('\n', $_POST['search']); " but somehow could not get it to work or used it wrong. Could any one tell me what am i doing wrong or missing?

robroy111
  • 27
  • 8

1 Answers1

1

Try something like this

$textarea = trim($_POST['search']);
$search = implode("|",preg_replace("/[^a-zA-Z 0-9]+/", "",explode("\n", $textarea)));

and the MySQL query looks like

if(($_SESSION['firstname']!=""))
{
  $view = $db->query("SELECT * FROM contacts WHERE firstname REGEXP '$search'");
  $check = mysqli_num_rows($view);
}
Vigikaran
  • 725
  • 1
  • 10
  • 27