Jump to content

[SOLVED] search form coding help


h4r00n

Recommended Posts

Hi, I am trying to construct a search form in dreamweaver to search my database in WAMP. I have created the following code and I get an error where I have made it bold. Any suggestions?

 

<?php

 

//get data

$button = $_GET['submit'];

$search = $_GET['search'];

 

if (!$button)

echo "You didn't submit a keyword.";

else

{

if (strlen($search)<=2)

echo "Search term too short.";

else

{

echo "You searched for <b>$search</b><hr size='1'>";

 

//connect to our database

mysql_connect("localhost","root","");

mysql_select_db("jobjar");

 

 

//explode our search term

$search_exploded = explode(" ",$search);

 

foreach($search_exploded as $search_each)

(

 

//construct query

$x++;

if ($x==1)

$construct .= "Keywords" LIKE '%$search_each%'";

else

$construct .= " OR Keywords" LIKE '%$search_each%'";

 

 

)

 

//echo out construct

 

$construct = "SELECT * FROM jobs WHERE $construct";

$run = mysql_query($construct);

 

$foundnum = mysql_num_rows($run);

 

if ($foundnum==0)

echo "No results found.";

else

(

echo "$foundnum results found!<p>";

 

while ($runrows = mysql_fetch_assoc($run))

(

//get data

$Title = $runrows['Title '];

$Location = $runrows['Location'];

$Salary = $runrows['Salary'];

$Sector = $runrows['Sector'];

$Job Type = $runrows['Job Type'];

$Duration = $runrows['Duration'];

$Job Ref = $runrows['Job Ref'];

$Description = $runrows['Description'];

 

echo "

<b>$Title</b><br>

$Location<br>

$Salary<br>

$Sector<br>

$Job Type<br>

$Duration<br>

$Job Ref<br>

$Description<p>

";

 

 

 

)

 

}

 

?>

Link to comment
Share on other sites

I get the following error:

 

Parse error: parse error in C:\Documents and Settings\haroon\My Documents\JobJar\wamp\www\wamp\www\Pages\search.php on line 29

 

and line 29 is:

 

//construct query

            $x++;

            if ($x==1)

              $construct .= "Keywords" LIKE '%$search_each%'";

            else

              $construct .= " OR Keywords" LIKE '%$search_each%'";

Link to comment
Share on other sites

sorry...

 

//explode our search term

$search_exploded = explode(" ",$search);

 

foreach($search_exploded as $search_each)

(

 

//construct query

$x++;

if ($x==1)

$construct .= "Keywords" LIKE '%$search_each%'";

else

$construct .= " OR Keywords" LIKE '%$search_each%'";

 

 

)

 

Link to comment
Share on other sites

It should probably be

 

 

<?php
$construct .= "Keywords LIKE '%$search_each%'";

 

..assuming that the $construct variable is declared before concencating it.

 

Edit: btw. in future it would be also good to provide the error you are getting instead of just saying I got an error.

Link to comment
Share on other sites

Hi, it still doesnt work. It is a parse error:

 

//explode our search term

$search_exploded = explode(" ",$search);

 

foreach($search_exploded as $search_each)

{

 

//construct query

$x++;

if ($x==1)

$construct .= "Keywords" LIKE '%$search_each%'";

else

$construct .= " OR Keywords" LIKE '%$search_each%'";

 

 

}

 

Link to comment
Share on other sites

Thanks, sorry my bad. I took the " out at the end of both KEYWORDS and now its worked. However, I now have another new parse error: I have changed the font to bold and underline to illustrate which line contains the error, thanks.

 

//echo out construct

 

$construct = "SELECT * FROM jobs WHERE $construct";

$run = mysql_query($construct);

 

$foundnum = mysql_num_rows($run);

 

if ($foundnum==0)

echo "No results found.";

else

(

echo "$foundnum results found!<p>";

 

while ($runrows = mysql_fetch_assoc($run))

(

//get data

$Title = $runrows['Title '];

$Location = $runrows['Location'];

$Salary = $runrows['Salary'];

$Sector = $runrows['Sector'];

$Job Type = $runrows['Job Type'];

$Duration = $runrows['Duration'];

$Job Ref = $runrows['Job Ref'];

$Description = $runrows['Description'];

 

echo "

<b>$Title</b><br>

$Location<br>

$Salary<br>

$Sector<br>

$Job Type<br>

$Duration<br>

$Job Ref<br>

$Description<p>

";

 

 

 

)

 

}

 

?>

Link to comment
Share on other sites

Hi, thanks for the reply. I have used { and } thanks. I am new to PHP and used an online tutorial to get this far. I am not sure what you mean by code tags. I sound a bit stupid now I know. Also, I am getting a parse error message :

//echo out construct

 

$construct = "SELECT * FROM jobs WHERE $construct";

$run = mysql_query($construct);

 

$foundnum = mysql_num_rows($run);

 

if ($foundnum==0)

echo "No results found.";

else

{

echo "$foundnum results found!<p>";

 

while {$runrows = mysql_fetch_assoc($run)}

Link to comment
Share on other sites

Hi. I think I have fixed it. I have changed it to:

//echo out construct

 

$construct = "SELECT * FROM jobs WHERE $construct";

$run = mysql_query($construct);

 

$foundnum = mysql_num_rows($run);

 

if ($foundnum==0)

echo "No results found.";

else

{

echo "$foundnum results found!<p>";

 

while ($runrows = mysql_fetch_assoc($run)

 

Just getting a few more new errors now, hopefully I will be nearly done!

 

(

//get data

$Title = $runrows['Title '];

$Location = $runrows['Location'];

$Salary = $runrows['Salary'];

$Sector = $runrows['Sector'];

$Job Type = $runrows['Job Type'];

$Duration = $runrows['Duration'];

$Job Ref = $runrows['Job Ref'];

$Description = $runrows['Description'];

 

echo "

<b>$Title</b><br>

$Location<br>

$Salary<br>

$Sector<br>

$Job Type<br>

$Duration<br>

$Job Ref<br>

$Description<p>

";

 

 

 

)

 

}

 

?>

 

Link to comment
Share on other sites

I suggest you read some basic tutorials about php. Now you are opening a curly bracket in else but not closing it. You have to open and close even amount of brackets and in right order.

 

Edit: and yet again you are using parenthesis in while loop which is not right. You should use curly brackets in while loop also. And your code is pretty much messed up. Seriously give some time and pay attention and read some basics.

 

<?php
// if else statement
if (condition)
{
   // something is true
}
else
{
  // Something is false
}


// while loop
while (condition)
{
   // action..
}

Link to comment
Share on other sites

I am not sure what you mean by code tags.

I mean put your code in tags like this

... code here ...

.  It will increase readability.

 

As far as your code, you're changing it so much and the format is so awful I can't even follow it.  Please post your entire current code in code tags.  If you have errors copy and paste exactly what it says.

Link to comment
Share on other sites

This is my entire code. I have underlined where I am currently getting parse errors. I apologise if my coding is all over the place, it's my first time. I am trying to create a simple keyword search for my recruitment agency.

 

<?php

//get data
$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
echo "You didn't submit a keyword.";
else
{
if (strlen($search)<=2)
	echo "Search term too short.";
else
{
	echo "You searched for <b>$search</b><hr size='1'>";

	//connect to our database
	mysql_connect("localhost","root","");
	mysql_select_db("jobjar");


		//explode our search term
		$search_exploded = explode(" ",$search);

		foreach($search_exploded as $search_each)
		{

			//construct query
			$x++;
			if ($x==1)				
				$construct .= "Keywords LIKE '%$search_each%'";
			else
				$construct .= " OR Keywords LIKE '%$search_each%'";


		}

	//echo out construct

	$construct = "SELECT * FROM jobs WHERE $construct";
	$run = mysql_query($construct);

	$foundnum = mysql_num_rows($run);

	if ($foundnum==0)
		echo "No results found.";
	else
	{
	echo "$foundnum results found!<p>";

	while ($runrows = mysql_fetch_assoc($run)
	[u][b]([/b][/u]
	//get data
	[b][u]$Title = $runrows['Title'][/u][/b];
	$Location = $runrows['Location'];
	$Salary = $runrows['Salary'];
	$Sector = $runrows['Sector'];
	$Job Type = $runrows['Job Type'];
	$Duration = $runrows['Duration'];
	$Job Ref = $runrows['Job Ref'];
	$Description = $runrows['Description'];

	echo "
	<b>$Title</b><br>
	$Location<br>
	$Salary<br>
	$Sector<br>
	$Job Type<br>
	$Duration<br>
	$Job Ref<br>
	$Description<p>
	";



	)

}

?>

.

Link to comment
Share on other sites

This is my entire code. I have underlined where I am currently getting parse errors. I apologise if my coding is all over the place, it's my first time. I am trying to create a simple keyword search for my recruitment agency.

 

I understand you're new, but I told you to list the exact error messages.  The other bbcodes don't work inside the code tag block.  I reformatted your code and fixed all the errors that I could see.  Try this:

 

//get data
$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
{
   echo "You didn't submit a keyword.";
}
else
{
   if (strlen($search)   {
      echo "Search term too short.";
   }
   else
   {
      echo "You searched for $search";
      //connect to our database
      mysql_connect("localhost","root","");
      mysql_select_db("jobjar");
      //explode our search term
      $search_exploded = explode(" ",$search);

      foreach($search_exploded as $search_each)
      {
         //construct query
         $x++;
         if ($x==1)            
         {
            $construct .= "Keywords LIKE '%$search_each%'";
         }
         else
         {
            $construct .= " OR Keywords LIKE '%$search_each%'";
         }
      }
   
      //echo out construct
      $construct = "SELECT * FROM jobs WHERE $construct";
      $run = mysql_query($construct);
      $foundnum = mysql_num_rows($run);

      if ($foundnum==0)
      {
         echo "No results found.";
      }
      else
      {
         echo "$foundnum results found!
";
   
         while ($runrows = mysql_fetch_assoc($run))
         {
            //get data
            $Title = $runrows['Title'];
            $Location = $runrows['Location'];
            $Salary = $runrows['Salary'];
            $Sector = $runrows['Sector'];
            $Job Type = $runrows['Job Type'];
            $Duration = $runrows['Duration'];
            $Job Ref = $runrows['Job Ref'];
            $Description = $runrows['Description'];
      
            echo "$Title

            $Location

            $Salary

            $Sector

            $Job Type

            $Duration

            $Job Ref

            $Description
";
         }
      }
   }
}

?>

Link to comment
Share on other sites

Thanks so much I think it should work now. I also changed the

$JobType = $runrows['Job Type'];    &

$JobRef = $runrows['Job Ref'];

 

because I don't think it allowed a space between JOB and TYPE and REF.

 

I now get the following errors:

Notice: Undefined index: submit in C:\Documents and Settings\haroon\My Documents\JobJar\wamp\www\wamp\www\Pages\search.php on line 4

 

Notice: Undefined index: search in C:\Documents and Settings\haroon\My Documents\JobJar\wamp\www\wamp\www\Pages\search.php on line 5

You didn't submit a keyword.

Link to comment
Share on other sites

Those 2 errors are referring to these lines:

 

$button = $_GET['submit'];
$search = $_GET['search'];

 

The GET method is when variables are passed via HTTP.  For example:

 

www.yoursite.com?submit=var1&search=var2

 

How are you getting to this page?  Does the URL resemble the above?

 

One solution would be to check if these are set.  If they are set, give $search and $button their values, if not, then you can assign them default values.  Something like this:

 

$button = (isset($_GET['submit']) ? $_GET['submit'] : "default_value";
$search = (isset($_GET['search']) ? $_GET['search'] : "default_value";

Link to comment
Share on other sites

I have two php files. One is search.php and the other is index.php. I have the text field and search button on the index.php page, so when I click search, the search.php should load up with the result. I tried the modified coding you've provided but it doesn't work.

Link to comment
Share on other sites

I have two php files. One is search.php and the other is index.php. I have the text field and search button on the index.php page, so when I click search, the search.php should load up with the result. I tried the modified coding you've provided but it doesn't work.

 

I forgot to close one of my parentheses.  Try this:

 

$button = (isset($_GET['submit'])) ? $_GET['submit'] : "default_value";
$search = (isset($_GET['search'])) ? $_GET['search'] : "default_value";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.