Help adding multiple values from textarea to sql query

I am trying to figure out the best way to include the values in a multiline textarea webform into a sql select statement that is processed when the submit button is pressed. The values in the textarea will be single servernames, one per line. When combined it will need to be put in a format like this (‘servername1’, ‘servername2’, ‘servername3’) and so forth. The sql statement would be like this - select * from table where servername in (combined values from textarea);

Below is what I currently am using that works for a single value in the textarea box. Any help on how to accomplish this would be very much appreciated.

[php]

<?php if(!isset($_POST)) die("Bad behavior"); echo "Results...

"; $strServerName = $_POST['ServerName']; mysql_connect("localhost", "user", "") or die (mysql_error()); mysql_select_db("ServerList") or die(mysql_error()); $strSQL = "Select * from table where servername = '" . $strServerName . "'"; $results = mysql_query($strSQL) or die("
$strServerName
".mysql_error()); echo ""; while($arr = mysql_fetch_array($results)) { echo ""; echo ""; echo ""; echo ""; echo ""; } mysql_close(); ?>
[/php]
" . $arr['hostid'] . "" . $arr['servername'] . "" . $arr['console'] . "

Are you saying you want a separate query generated for each server name?

No just a single select statement. I need help with joining/conctenating the values in the textarea form, along with the quotes and spacing into the actual sql query.

So if the values on the textarea where like this:
Server1
Server2
Server3

The select statement would that would be run would be:
Select * from table1 where servername in (‘Server1’, ‘Server2’, Server3’);

Hope that makes more sense, thanks for your quick reply.

So server 1,2 and 3 are column names in your table? If so, you have a bad database design and need to fix that first.

No server1 server2 are just example records for the servername column… The select statement will return all rows that had server1, server2, server3 as values in the servername column

In that case you just need an OR in your WHERE clause.

WHERE servername=‘server1’ OR servername=‘server2’ OR servername =‘server3’

If you’re going to use a text area, you’ll have to parse that input on the new lines. You would also have to trust that the user is going to enter the data correctly which is always a bad idea . If there are only a few server name options, you may just want to use a separate text field for each one. Best to make it as a idiot proof as possible.

More importantly, your MySQL code is deprecated. You need to use PDO or mysqli with prepared statements. There is a link in my signature to a PDO bump start database to get you going.

Yes I’m aware how to format the sql query and was mainly needing help with parsing through the textarea. The user input could be 300 or more servers but it wil always be pasted from a single column spreadsheet

I was aware my formatting was outdated so I’ll look over the link you provided. Thanks again for your suggestions.

Okay, finally we are on the same page. This should help you

Yes thats exactly what I needed. Thanks for the link and all the other information. I read over the PDO guide and plan to make the switch to it as well.

Sponsor our Newsletter | Privacy Policy | Terms of Service