

#Php if else statement with post data code#
If it’s false, the first set of code is skipped, and the second set of code (after the else) is executed instead.įor example, you could ask if a user is logged in, and then provide a different greeting depending on the result. If the statement is found to be true, the first set of code is executed. The code begins by checking to see if a statement is true or false. Conditional Tags usually work with PHP if / else Conditional Statements. They tell WordPress what code to display under specific conditions. A Single Page, Single Post or AttachmentĬonditional Tags can be used in your Template Files in classic themes to alter the display of content depending on the conditions that the current page matches.Trouble is that when there are no matching titles the code should echo “there are no results!” but it doesn’t.Ĭan you tell me where i’m going wrong again please. I am just messing around making a mini search engine for my site. I now have this code thanks to the help of you guys here… //GET DATA FROM DATABASEīasically users input a word in a search box and then the php looks in the database for a matching title. Sorry to sound like a complete noob but what does the sanitizing bit do? Let’s throw in some user data sanitizing as well This is what I have been trying to include… SELECT * FROM items WHERE title LIKE '%$title_query%'
#Php if else statement with post data how to#
I can’t figure out how to add the like statement into my code now though. Before it was showing contents of all fields as called for: code, title, maindesc, itemimg.Īlso I added the ‘sanitizer’ you mentioned, thanks for that. What you told me worked, but, now only the contents of “code” is being output to the browser. I have kind of sorted the ‘else’ problem. $result = mysql_query("SELECT * FROM items

Now I have this… //GET DATA FROM DATABASE Thanks for the explanation, I’m learning slowly. You can do this using: SELECT * FROM items WHERE title LIKE '%$title_query%' Since you’re building a search engine, you might want to match titles that are similar to what was entered, rather than having to be an identical match. So rather than checking the amount of rows each time the loop runs, you should check beforehand: if (mysql_num_rows($result) != 0) However, if there are no rows then the loop will never run, causing the text to never be displayed. The else problem is caused because you’ve placed the check inside the loop. The sanitizing filters the data to prevent people from injecting SQL their own SQL code which could manipulate your database. The only way I can get a result is with an exact matching query. I’ve tried… (while($row = mysql_fetch_array($result)))Īnd Ĭan anyone explain the LIKE bit for me please? As it doesn’t seem to work.

You mentioned putting brackets round the ‘while’ clause. $title_query = mysql_real_escape_string($_POST) So, now I have this… //GET DATA FROM DATABASE With regards to the query, what errors are you getting? The code should be like: $title_query = mysql_real_escape_string($_POST)

If you don’t use brackets, only the first line after the clause is executed. The reason that only ‘code’ is showing is because you haven’t put brackets around the while clause.
