title: PHP MySQL PDO TextArea Where clause with condition checks
body: <p>I have a page as shown in the screenshot below. The idea is to enter the bus number and the list of all stops on a particular route, one per line.</p>
  
  <p><a href="https://i.stack.imgur.com/u3mDM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u3mDM.png" alt="My Add Route Page"></a></p>
  
  <p>The stops are already stored in a database table called 'stops' I need the ID of each stop from the textarea. My current code only gets the ID of the last stop in the textarea. I feel like I am missing something. 'busnumber' is my textfield and 'busroute' is my textarea. I would appreciate if anyone can point me out on what I need to change in order to get the ID of each stop entered in the textarea as an array. Thanks for your time in advance.</p>
  
  <pre><code>try {
              $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
              if(isset($_POST["busnumber"]) &amp;&amp; isset($_POST["busroute"])){
  
              $stops = explode(PHP_EOL, $_POST["busroute"]);
              $stopsArray = '"' . implode('","', $stops) . '"';
              $sql = "SELECT * FROM stops WHERE stop_name IN ($stopsArray)";
              echo $sql."&lt;/br&gt;";
              $query = $conn-&gt;prepare($sql);
              $query-&gt;execute();
              $query-&gt;setFetchMode(PDO::FETCH_ASSOC);
              $results = $query-&gt;fetchAll();
              foreach($results as $result){
              echo $result['stop_id'].' '.$result['stop_name'].'&lt;/br&gt;';
              }
              }
  
  } catch (PDOException $pe) {
  die("Could not connect to the database $dbname :" . $pe-&gt;getMessage());
  </code></pre>
  
  <p>}</p>
  
  <p><strong>UPDATE 1</strong>
  I changed the code as follows</p>
  
  <pre><code>$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
          if(isset($_POST["busnumber"], $_POST["busroute"])){
  
      $stops = explode(PHP_EOL, $_POST["busroute"]);
  
      foreach($stops as $stop){
          $sql = "SELECT * FROM stops WHERE stop_name = '".$stop."'";
          $statement = $conn-&gt;query($sql);
          echo $sql.'&lt;/br&gt;';
          $statement-&gt;setFetchMode(PDO::FETCH_ASSOC);
          $results = $statement-&gt;fetchAll();
          foreach($results as $result){
              echo $result['stop_id'].' '.$result['stop_name'];
          }
          $statement = null;
  
      }
          }
  </code></pre>
  
  <p>and still get the same output, it only gives me the ID of the last item inside the textarea
  <a href="https://i.stack.imgur.com/io3uz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/io3uz.png" alt="The error is still here"></a></p>