I'm trying to take lines of text in a textarea of a form, and turn them into a query from a database. But for some reason, the variables are not showing up in the query I've constructed.

Here's what I have so far:

<html>
<body>
<?php

$table_variable = $_POST['chosen_table']; 
 $chosen_insert = $_POST['chosen_insert'];
$textfield = $_POST['textarea'];


echo "texted: ".$textfield."<br/>";  


if(isset($_POST['textarea'])) { 

          $query = "SELECT DISTINCT item_id FROM table WHERE ";


          $text = explode("\n", $_POST['textarea']); 
     foreach ($text as $line) {

          $insertLine = explode("\t", $line); 
          $insertLine = explode(",", $line); 

          for($i = 0 ; $i <= 2 ; $i++){
           $insertLine[$i] = mysql_real_escape_string(trim($insertLine[$i]));
           $query .= "taxonomic_genus = '{$insertLine[0]}' AND scientific_name = '{$insertLine[1]}' AND infraspecific_epithet = '{$insertLine[1]}' OR";
      }

     }

     $query = substr($query, 0, -2);
     $result = mysql_query($query);
     echo $query;   
}  


?>


<form name="myform" method="POST">
      <textarea name="textarea" id="textarea" style="height:300px;width:300px;"></textarea><br/>
      <input type="hidden" name="chosen_table" value="table">
      <input type="hidden" name="chosen_insert" value="412">
      <input type="submit">
</form>
</body>
</html>
Member Avatar for diafol

I'm assuming that there are 3 values on each line of the textarea, separated by a ','

In this case:

$query = "SELECT DISTINCT item_id FROM table";
if($_POST[...])
{
    $subClause = array();
    $crudeLines = explode("\n", $_POST['textarea']);
    $lines = array_map("trim", $crudeLines);
    foreach($lines as $line)
    {
        $crudeElements = explode(",", $line);
        $elements = array_map("trim", $crudeElements);
        $subClause[] = "taxonomic_genus = '{$elements[0]}' AND scientific_name = '{$elements[1]}' AND infraspecific_epithet = '{$elements[2]}'";
    }
    $whereClause = " WHERE " . implode(' OR ', $subClause);
    $query .= $whereClause;
}

For brevity, there's no error checking and this code is not tested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.