1

What I'm trying to do currently is have a search function on my site that will look into a local MySQL db, in 1 column. The issue is the end-user could paste 1 item or they could paste 50 for example. And, without creating 50 textbox's I was wondering if it would be possible to make say a large textbox, that would search for a new field based on a space between them, or linebreak. Currently I have two search boxes, one where data is pasted, and the second null(I have null because when I left it blank it search for all(*).

Here is my page.

form action="" method="get">  
Paste your ZZZs here: <br>
<input type="text" name="item" id="textbox" maxlength="10" size="10"><br>
<input type="text" name="item2" id="textbox2" value="null" maxlength="10" size="10"><script>document.getElementById('textbox').focus()</script><br />  
<input type="submit" value="Submit"/>  
</form>  
<?php
if (!empty($_REQUEST['item'])) {

    $item = mysql_real_escape_string($_REQUEST['item']);
    $item2 = mysql_real_escape_string($_REQUEST['item2']);     

    $sql = "select fcsku, fnsku from oak3_zzz_to_boo WHERE fcsku like '%".$item."%' or fcsku like '%".$item2."%'"; 
    $r_query = mysql_query($sql); 

    echo "<table border=1>";
    echo "<tr><td><b>FcSKU</td><td><b>FnSKU</td></tr>";
    while ($row = mysql_fetch_array($r_query)) { 
        echo "<tr><td>".$row['fcsku']."</td><td>".$row['fnsku']."</td></tr>"; 
    }  
}
echo "</table>";
mysql_close();

My Table structure only has 2 columns, FnSKU, and FcSKU

Thanks to the contributors here is the working code!

<form action="" method="get">  
Paste your ZZZs here: <br>
<textarea name="item" id="textbox"></textarea>
<script>document.getElementById('textbox').focus()</script><br />  
<input type="submit" value="Submit"/>  
</form>  
<?php
if (!empty($_REQUEST['item'])) {

    $search = "\"" . strip_tags(preg_replace("/[\n\r ]/","\", \"",$_REQUEST["item"])) . "\""; // for space as delimiter 
    ##$search = "\"" . strip_tags(str_replace(" ","\", \"",$_REQUEST["item"])) . "\""; // for space as delimiter 

    $query = "SELECT fcsku, fnsku FROM oak3_zzz_to_boo WHERE fcsku IN ($search)";
    $r_query = mysql_query($query); 

    echo "<table border=1>";
    echo "<tr><td><b>FcSKU</td><td><b>FnSKU</td></tr>";
    while ($row = mysql_fetch_array($r_query)){ 
        echo "<tr><td>".$row['fcsku']."</td><td>".$row['fnsku']."</td></tr>"; 
    }  
}
echo "</table>";
mysql_close();
2
  • 1
    you could explode a string (input) into an array then loop through your array searching on each term. Commented Jan 2, 2014 at 20:34
  • I was thinking the same implode the string at each space and include each item in an array then get the size of the array which would allow you t use a for loop to look through the array and search using each value. Commented Jan 2, 2014 at 20:36

1 Answer 1

1

no need to explode, just go:

$search = "\"" . strip_tags(str_replace(" ","\", \"",$_REQUEST["item"])) . "\""; // for space as delimiter

$query = "SELECT * FROM something WHERE something IN ($search)";

//rest of mysql stuffs here
Sign up to request clarification or add additional context in comments.

6 Comments

Awesome! I didn't even test it.
It works when they are on the same line. although it does not if they are on separate line (ex. pasted values from excel)
Hmm, that didn't seem to do anything, but now not execute the query. Also it appears to work when values are typed in, but when pasted and identical it does not.
You might want to change str_replace with preg_replace: preg_replace("/[\n\r ]/","\", \"",$element); this should replace space and newline
Remove the line breaks. stackoverflow.com/questions/5258543/…. You should also Google "sql injection" while you're at it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.