Jump to content

Recommended Posts

Posted

Always wanted to learn getting PHP and SQL talking together, so given it a bit of time this week. Now I'm a little stuck......

 

Can anybody tell me what i am doing wrong with the %$searchq% part on the SELECT statement?

 

Its a simple box, you type in your query into the text box, i.e. Period 4 and it should list all the rows that match Period 4, but its instead returning ALL rows.

 

If i change the code on the select statement from %$searchq% to say Period 4 it returns just the ones matching Period 4.

 

So im not sure whats wrong, why it does not like it being typed in the box.

 

mysql_connect("localhost","root","root") or die ("Could not connect to server!!");
mysql_select_db("testdata") or die ("could not find db");

$output = '';
//collect

if(isset($_POST['search'])) {
$searchq = $_post['search'];
$searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
$query = mysql_query("SELECT * FROM oncallinfo WHERE period LIKE '%$searchq%'") or die ("could not search!");
$count = mysql_num_rows($query);
if($count == 0) {
	$output = 'There was no search results!';
}else{
	while ($row = mysql_fetch_array($query)) {
		$periodname = $row['period'];
		$infoname = $row ['info'];
		$id = $row['infoid'];

		$output .= '' .$periodname. ' '.$infoname.''; 
	}

}

}

?>

</pre><form action="oncallinfo.php" method="post">


</form><br><b

Posted (edited)

I don't think you can use wildcards at the beginning a LIKE statement, try with =

 

Also you don't need the apostrophes, php can use inline variables.

 

Lastly use something like ezSQLJustin Vincent It will make learning so much easier. That code could be a couple of lines instead of all that mysql connect stuff.

 

Oh and if you want to escape it do this ".add_slashes($search)."

Edited by zag
Posted

I have actually done something just like this but for a friends Minecraft server using an input form and taking that data and searching a database.

 

PM if you want some help.

Posted

Hmm got a little further, i added

 

<?php print ("$searchq");?>

 

To the end so i could see on screen what it thinks is in the text box and its always blank, so thats why its return all rows from the table as its searching for "".

 

So its got to be something wrong with this part

 

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

$searchq = $_post['search'];

 

or the Form part at the bottom.

Posted
Just wait until you miss a single semicolon somewhere and it tells you the error is about 30 lines further down...

 

That was all day yesterday :(

 

Problem I'm finding now is everyone has different ways of doing things and I'm having to flip tutorial authors on youtube to achieve different things. Such as one tutorial was using sql and another sqli and i did not notice it until unixman pointed out i should be using sqli .

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...