PHP / MYSQL search

Change this:
$SQL = "SELECT * FROM itemsearch WHERE itemname = '$user_input'";

To this:
$SQL = "SELECT * FROM itemsearch WHERE itemname LIKE \"%$user_input%\"";
 
Thanks, that worked. Might anyone tell me how i could make all variables that match the user input display? So if brown car and blue car are names, and the word car is searched, both display? Thanks
 
I read that, but it seemed to only talk about matching, not selecing all of the matching items. Perhaps i missed it, but i read it two times. Thanks
 
Ok, heh. Lets say I have 2 items, both containing the word "Wheat". If the word wheat was searched, both items would display instead of just one.

Also, i asked earlier, why my Forms wont put information into my database?

If its easy, could you post code? I am not good with PHP yet. I know basics, but i still need to get this up! Lol.. Thanks for the help.
 
About you INSERT: You have 2 errors in the line:
('$name','$url','$picurl','$str','$sta','$dex','$a gi','$wis','$int','$cha','$effone','$efftwo','$eff three')"; // The space

BTW: Are you already using the superglobal arrays!? Like: $_GET, $_POST.... If don´t, so you better you it!
Example (instead of yours...):
Code:
<?php
if (isset($_POST['submit'])) { 

$db = mysql_connect("localhost", "login", "pass"); 
mysql_select_db("dbasdf",$db);

$sql = "INSERT INTO itemsearch (name,url,picurl,str,sta,dex,agi,wis,int,cha,effone,efftwo,effthree) VALUES ('" . $_POST['name'] . "','" . $_POST['url'] . "','" . $_POST['picurl'] . "','" . $_POST['str'] . "','" . $_POST['sta'] . "','" . $_POST['dex'] . "','" . $_POST['agi'] . "','" . $_POST['wis'] . "','" . $_POST['int'] . "','" . $_POST['cha'] . "','" . $_POST['effone'] . "','" . $_POST['efftwo'] . "','" . $_POST['effthree'] . "')"; 

}
?>
 
Ill try that. Could you show me how to display multple items? In my search, it only displays 1 item a search. I want all items per search to display. So if 72054 items contain the word memberlist, all of them would display? Thanks
 
Ugh, i tried that but still didnt place any info into my table? Those mistakes never were, the forum must have messed it up..

The code you posted, is that supposed to put information into a table? Or does it need to be modified yet, besides passwords, logins, etc.
 
Please take the @ off of your functions, ´cause it will supress the that function´s errors!
I suggest you too to use error_reporting(E_ALL);, so you can know about all php errors!

BTW: Your still having errors in your query:
INSERT INTO itemsearch (name,url,picurl,str,sta,dex,agi,wis,int,cha,effon e,efftwo,effthree)
I´ve already fix it in the code i gave you above. It should work just fine!

Tell me what happens after making what i´ve said (above)!


Searching:
Is the next code your search query!?
Code:
$SQL = "SELECT * FROM itemsearch WHERE itemname LIKE \"%$user_input%\"";
 
I dont know why those are there, they are not in my script. The forum must have automatically placed it in... I placed that code (error_reporting(E_ALL);) in my script, i have no idea where it should go, so i placed it at the end..the script runs, says Information Entered, but it doesnt show up in my table...
 
Yes that is my search query..it works too. Except im trying to get it to display multiple items, not just one.
 
Ok. In that query the only item you´re searching for is 'itemname'.
So if you want to search more items, you can do something like:
Code:
$SQL = "SELECT * FROM itemsearch WHERE itemname LIKE '%$user_input%' AND itemname1 LIKE '%$user_input1%' OR itemname1 LIKE '%$user_input1%'";
Or...
Code:
$SQL = "SELECT itemname, itemname1 FROM itemsearch WHERE itemname2 LIKE '%$user_input2%'";
And so on...
You can now use php's mysql_fetch_array() function and show the itemsname related fields.

This are some ideas. I recommend you to take a look at: MySQL Manual!
 
Thanks for the help so far. I dont know if i explained this well enough, so ill try again. Lets say I have a table with the following "items" in it.
A Brown Car
A Blue Car
A Purple Car
A Orage Car
A Green Car
A Red Car

Now lets say I search car. I want all of those names do display. Then if i search brown car, A Brown Car displays (as it does now).

Then for placing info into my DB, those errors you said i had wernt actually there, not in my script. Im not sure why they showed up. Would you would be willing to try to figure out why its still not working. The script connects to the table, it just wont put information into the table.
 
Are you not sure? Or am i just being so stupid, you have decided to give up? Heh...
 
try this.
Code:
$yoursearch = $_POST['fieldname'];
//now break the items separated by " "
$searchlist  = explode (" ", $yoursearch);

now u build the query
foreach ($searchlist as $key => $value) {
   $sqlsearch .= " like \"%$value%\" or";
}

now your query should be something like this....

$sql = "selelct ....  where fieldname $sqlsearch";
//be sure you remove the last OR from the query statement...

//i supose that should suffice your problem....!
 
Hey eq1986 is exactly the same query i´ve posted above!

Nice idea ruturajv!
With this one you don´t need to look at the last 'OR'.

BTW eq1986, if you put your form input names as an array and with the same name as your table fields, it will simplify your work.
Example:
Code:
...
<input name="inputs[effthree]">
...
So you´ll have...
Code:
<?php

// if submit
if(array_key_exists('submit', $_POST) && !empty($_POST['submit'])){  

$inc = 0;
$sqlsearch = '';

// count your array -> form input´s
$count = count($_POST['inputs']);

/* Will create that part of the query with $key(or table field name) in it. E.g: name LIKE 'John' OR ...*/

foreach ($_POST['inputs'] as $key => $value) { 
   $inc ++;
   if($inc == $count){
      $sqlsearch .= $key . " LIKE '%$value%'";
   }else{
      $sqlsearch .= $key . " LIKE '%$value%' OR "; 
   }
}

// Your query
$sql = "SELECT something FROM table WHERE $sqlsearch"; 

}
?>
 
Back
Top