Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Simple Search from Database Fails.. HELP HELP!!

Codes here !

Moderators: macek, egami, gesf

Simple Search from Database Fails.. HELP HELP!!

Postby aragorn911 » Wed May 16, 2012 9:06 am

Hello,
im trying to search some song lyrics from my database and display in the webpage. But somehow it fails..

Code: Select all
<form metod="POST" action="test.php">
      
       
        <label> Select Language:
        <select name="Language">
            <option value="English">English</option>
            <option value="Sinhala">Sinhala</option>
            </select>
        </label>
           
       
        <label>Search Song: <input type="text" name="criteria" /> </label>
        <input type="submit" value="Search" />
       
        </form>
        <br />

       
        <?php
      
      if(isset($_POST['submitted'])){
      
      $Language = $_POST['Language'];
      $criteria = $_POST['criteria'];
      $query = "SELECT * FROM songs WHERE category ='$criteria'";
      $result = mysql_query($query) or die();
      
      echo "<table>";
      echo "<tr> <th>Title</th> <th>Lyrics</th> </tr>";
            
      while($row = mysql_fetch_array($result)){
      
      echo "<tr><td>";
      echo $row['Title'];
      echo "</td><td>";
      echo $row['Lyrics'];
      echo "</td></tr>";
      }      
            
      echo "</table>";   


      }
      
      
      ?>



PLEASE tell me what I did wrong.. for some reason it never goes into the if statement.. :(
aragorn911
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed May 16, 2012 9:01 am

Re: Simple Search from Database Fails.. HELP HELP!!

Postby macek » Wed May 16, 2012 9:12 am

Code: Select all
$query = "SELECT * FROM songs WHERE category ='" . mysql_real_escape_string($criteria) . "'";


because, if you enter: " it's nice " that the final query is:

SELECT * FROM songs WHERE category = 'it's nice'

but better could be
WHERE category LIKE '" . mysql_real_escape_string($criteria) . "'
User avatar
macek
php-forum Active User
php-forum Active User
 
Posts: 277
Joined: Wed Aug 25, 2010 10:42 am

Re: Simple Search from Database Fails.. HELP HELP!!

Postby aragorn911 » Wed May 16, 2012 10:16 am

My problem is not returning search results.. its just that the POST thing doesn't work.. i think..
if i put an else{} for the if and echo something, the page will always display it... even if i press the submit button..

plss Help!!
aragorn911
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed May 16, 2012 9:01 am

Re: Simple Search from Database Fails.. HELP HELP!!

Postby macek » Wed May 16, 2012 11:48 am

Code: Select all
<input type="submit" value="Search" name="someVeryNiceName />
...
if(isset($_POST['someVeryNiceName'])) {

}
User avatar
macek
php-forum Active User
php-forum Active User
 
Posts: 277
Joined: Wed Aug 25, 2010 10:42 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.