h4r00n Posted August 24, 2009 Share Posted August 24, 2009 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> "; ) } ?> Quote Link to comment Share on other sites More sharing options...
Maq Posted August 24, 2009 Share Posted August 24, 2009 1) What's the error? 2) Use tags. Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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%'"; Quote Link to comment Share on other sites More sharing options...
Maq Posted August 24, 2009 Share Posted August 24, 2009 Your foreach should be using curly braces '{' and '}'. Not parentheses. For the second time, use code tags. Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 Hi. Thanks for the help, but I still get an error with $x++; (just that line) its a parse error Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 24, 2009 Share Posted August 24, 2009 There is no error in that line, the php wiill give the error because you have parse error in the line BEFORE this line. Which is the foreach that was already mentioned. And the foreach is incorrect unless you modified as mentioned above. Quote Link to comment Share on other sites More sharing options...
Maq Posted August 24, 2009 Share Posted August 24, 2009 Hi. Thanks for the help, but I still get an error with $x++; (just that line) its a parse error Please post the whole foreach code block. $x++; is perfectly fine. Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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%'"; ) Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 24, 2009 Share Posted August 24, 2009 You don't use foreach which parenthesis like that... but with curly brackets as already mentioned .. <?php foreach ($something as $some) { .... } Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 ok thanks , I have fixed that, I am now getting an error a few lines below that. $construct .= "Keywords" LIKE '%$search_each%'"; Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 24, 2009 Share Posted August 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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%'"; } Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 24, 2009 Share Posted August 24, 2009 The code above is not like I wrote it, look closely. You are breaking the string with one extra quotation mark. Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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> "; ) } ?> Quote Link to comment Share on other sites More sharing options...
Maq Posted August 24, 2009 Share Posted August 24, 2009 Hmm, you're not the type to learn from your mistakes, are you? You should be using curly braces, '{' and '}' for if/else blocks, foreach, etc... For the third time, use tags... Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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)} Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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> "; ) } ?> Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 24, 2009 Share Posted August 24, 2009 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.. } Quote Link to comment Share on other sites More sharing options...
Maq Posted August 24, 2009 Share Posted August 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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> "; ) } ?> . Quote Link to comment Share on other sites More sharing options...
Maq Posted August 24, 2009 Share Posted August 24, 2009 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 "; } } } } ?> Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
Maq Posted August 24, 2009 Share Posted August 24, 2009 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"; Quote Link to comment Share on other sites More sharing options...
h4r00n Posted August 24, 2009 Author Share Posted August 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
Maq Posted August 24, 2009 Share Posted August 24, 2009 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"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.