Simple search form problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

Simple search form problem

Post by tua1 »

Hello!

I have a problem with my searching script.

My html form:

Code: Select all

    <form id="frmPokaz" action="pokaz.php" method="post" >
      <fieldset id="Pokaz"><legend>Wybierz swoja kategorie</legend><table id="tblPokaz" >
       <tr>
        <td class="underline">Dyscyplina:</td>
        <td>
         <select name="dyscyplina" id="select">
         <option selected value="0"><strong>Wszystkie</strong></option>
         <option value="1">Koszykówka</option>
         <option value="2">Pilka Nozna</option>
         <option value="3">Sztuki walki</option>
         <option value="4">Lekkoatletyka</option>
         <option value="5">Terefere</option>
         <option value="6">Biegi</option>
         <option value="7">Suplementy</option>
         </select>     </td>
       <td class="underline"> Kategoria:       </td>
       <td>Odziez    </td>
       <td>
        <input name="kategoria" type="checkbox"  class="checkbox" value="1" >      </td>  
       <td>Sprzet</td>
       <td><input name="kategoria" type="checkbox" class="checkbox" value="2">     </td>
       <td>Inne</td>
       <td><input name="kategoria" type="checkbox"  class="checkbox" value="3"  >      </td>
       <td><input type="submit" class="submit" name="submit" id="pokaz" value="Pokaz"></td>
       </tr>
       
       </table></fieldset>
      </form>

My php script:

Code: Select all

<?php
require_once ("include/polaczenie.php");
 
//$_POST['kategoria']= array();
 
foreach ($_POST['kategoria'] as $pt) { 
 $in1 .= "'" . $pt . "',"; 
 
 }
$in1 = substr($in1,0,strlen($pt)-1); 
 
$sql = "select ID_prod, ID_kat, ID_dyscyplina, nazwa_prod, cena, dostepny, opis_prod from produkty where ID_kat IN (" . $in1  . ") and ID_dyscyplina=" . $_POST['dyscyplina'] . ";";
//echo $sql;
 
$query = mysql_query($sql);
 
while ($row = mysql_fetch_array($query)) { 
 echo '<p class="p_account_php">',$row['nazwa_prod'],'</p> ',$row['nazwa_prod']; 
}
 
?>



The error is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\WebServ\httpd\Runners2\pokaz.php on line 241

Could anyone help?

Regards
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Simple search form problem

Post by Rovas »

The sql interogation should be:

Code: Select all

 
SELECT ID_prod, ID_kat, ID_dyscyplina, nazwa_prod, cena, dostepny, opis_prod 
  FROM produkty WHERE (ID_kat IN " . $in1  . ") AND (ID_dyscyplina=" . $_POST['dyscyplina'] . ");";
 
 

Also you could only select from the table the column you what. You should put in a different table the categories for products.
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

Re: Simple search form problem

Post by tua1 »

Rovas wrote:The sql interogation should be:
Also you could only select from the table the column you what. You should put in a different table the categories for products.
ID_kat and ID_dyscyplina are foreign keys in table produkty. Maybe I try to explain it.

I have to do a internet shop site to my school homework

In my database I have table called produkty (products), table kategoria (categories) in that table I have 3 different categories of products, and table dyscyplina (sport disciplines).
So I have 3 tables needed for my searching query:
  • categories (ID_kat pk, categorie name),
  • dyscyplines (ID_dyscyplina pk, discipline name)
  • and products(items) (ID_prod pk, ID_kat fk, ID_dyscyplina fk, item name,...)
I want to create 2 search engines (one above).

In my first search engine I want to show products with specified categories or/and
dyscypline witout specify any names of products (for this I have another search engine). For the categories are 3 checkboxes name 'kategoria', and for sport dyscyplines I have selection list (name="dyscyplina").

If the selection list is unset, and checkboxes are unchecked I want to show all products from all dyscyplines and all categories. If only selection list is set I want to show products from all categories from specified discipline. If only checkboxes are checked I want to show products from all discipline from checked categories, ofcourse you can checked 1,2 or 3 categories, etc.

This is a script, I want to have every product in seperate table.

Code: Select all

     <?php
      
     require_once ("include/polaczenie.php");
 
//$_POST['kategoria']= array();
 
foreach ($_POST['kategoria'] as $pt) { 
 $in1 .= "'" . $pt . "',"; 
 
 }
$in1 = substr($in1,0,strlen($pt)-1); 
 
$sql = "SELECT ID_prod, ID_kat, ID_dyscyplina, nazwa_prod, cena, dostepny, opis_prod
   FROM produkty WHERE (ID_kat IN " . $in1  . ") AND (ID_dyscyplina=" . $_POST['dyscyplina'] . ");";
//echo $sql;
 
$query = mysql_query($sql);
 
 
 
while ($row = mysql_fetch_array($query)) 
 { 
 echo '<p class="p_szukaj_php">',$row['nazwa_prod'],'</p> 
        <table class="szukaj_php" cellpadding="5">
          <tr>
            <td rowspan="2" valign="top" align="left" cellpadding="5"><img src="images/img_male/',$row['ID_prod'],'.jpg"/>                                  </td>
            <td  colspan="4" valign="top" width="400" align="left">',$row['opis_prod'],'</td>
        </tr>
        <tr>
            <td align="left"  valign="top">Cena: ',$row['cena'],'</td>
            
         
             <td align="left" valign="top">Dostepny: ',$row['dostepny'],'</td>
             <td align="left"  valign="top"><a href="opinie.php"><input type="submit" class="submit"  value="Opinie"></a></a></td>
             <td align="left"  valign="top" align="right"><a href=""><input type="submit" class="submit"  value="Do Koszyka"></a></td>
         </tr>
        </table>
        <p class="p_szukaj_php"></p>';  
            
}
 
?>
Error: Warning: Invalid argument supplied for foreach() in C:\WebServ\httpd\Runners2\pokaz.php on line 225

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\WebServ\httpd\Runners2\pokaz.php on line 239



Maybe calling this search engine in not good, this is a "select engine".

I hope anybody understand me:)


Any advices??
Could anybody help me?
Regards
Post Reply