Jump to content

Checkbox multiple select


masterwebxz

Recommended Posts

Hello,

 

I have the code and i get error.Can help me?

 

$or = false;

$query = mysql_query("SELECT * FROM unuro_mag.jos_vm_product WHERE ");

if ( ! empty($coliere) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$coliere}' ";

}

 

if ( ! empty($bratari) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$bratari}' ";

}

 

 

if ( ! empty($cercei) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$cercei}' ";

}

 

 

echo $query;

while($row = mysql_fetch_array($query))

{

 

 

echo $row['product_name'];

 

}

 

 

i have a lot of checkboxes and i want to compair 1 field from database with multiple variables.

 

Help.

Link to comment
https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/
Share on other sites

$query = mysql_query("SELECT * FROM unuro_mag.jos_vm_product WHERE ");

 

take the mysql_query out until later.  You want to set:

 

$query = "SELECT * FROM unuro_mag.jos_vm_product WHERE ";

 

And then, instead of echo $query,

$result=mysql_query($query);

And then,

while($row = mysql_fetch_array($query))

becomes

while($row = mysql_fetch_array($result))

 

 

Savvy?

 

Error again...

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/unuro/public_html/cautare.php on line 626

 

 

$or = false;

$query = "SELECT * FROM unuro_mag.jos_vm_product WHERE ";

if ( ! empty($coliere) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$coliere}' ";

}

 

if ( ! empty($bratari) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$bratari}' ";

}

 

 

if ( ! empty($cercei) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$cercei}' ";

}

 

$result=mysql_query($query);

while($row = mysql_fetch_array($result))

{

 

 

echo $row['product_name'];

 

}

$or = false;

$query = "SELECT * FROM unuro_mag.jos_vm_product WHERE 1=1";

if ( ! empty($coliere) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$coliere}' ";

}

 

if ( ! empty($bratari) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$bratari}' ";

}

 

 

if ( ! empty($cercei) )

{

  if ( $or)

  {

      $query .= " OR ";

      $or = true;

  }

  $query .= " categorie = '{$cercei}' ";

}

 

echo $query;

$result=mysql_query($query);

while($row = mysql_fetch_array($result))

{

 

 

echo $row['product_name'];

 

}

 

 

like this? I get error again...

 

 

SELECT * FROM unuro_mag.jos_vm_product WHERE 1=1 categorie = 'Coliere' categorie = 'Bratari'

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/unuro/public_html/cautare.php on line 627

 

Take out the 1=1 and change all three instances of $or=true to:

 

}else{

$or=true;

 

So:

 

$or = false; 
$query = "SELECT * FROM unuro_mag.jos_vm_product WHERE 1=1"; 
if ( ! empty($coliere) ) 
{ 
   if ( $or) 
   { 
      $query .= " OR "; 
}
else
{
      $or = true; 
  }  
   $query .= " categorie = '{$coliere}' "; 
} 

if ( ! empty($bratari) ) 
{ 
   if ( $or) 
   { 
      $query .= " OR "; 
}
else
{    
  $or = true; 
   } 
   $query .= " categorie = '{$bratari}' "; 
} 


if ( ! empty($cercei) ) 
{ 
   if ( $or) 
   { 
      $query .= " OR "; 
}
else
{  
    $or = true; 
   } 
   $query .= " categorie = '{$cercei}' "; 
} 

echo $query; 
$result=mysql_query($query);
while($row = mysql_fetch_array($result))
{


echo $row['product_name'];
   
} 

ok, it works now.But i have 2 more problems.

 

 

If i don`t check nothing and aply the form I have ERROR -

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/unuro/public_html/cautare.php on line 621

 

 

AND if i insert 2 words in categorie field like this: Coliere Bratar with space he gets only the first word and is not god.

 

I want something like AND/OR in the same time.

 

To compair my variable with all the words from the field database

1.

If you want to return all results when all boxes are unselected:

 

$or = false; 
$query = "SELECT * FROM unuro_mag.jos_vm_product"; 
if ( ! empty($coliere) ) 
{ 
   if ( $or) 
   { 
      $query .= " OR "; 
}
else
{
$query.= " WHERE";
      $or = true; 
  }  
   $query .= " categorie = '{$coliere}' "; 
} 

if ( ! empty($bratari) ) 
{ 
   if ( $or) 
   { 
      $query .= " OR "; 
}
else
{    
$query.= " WHERE";
  $or = true; 
   } 
   $query .= " categorie = '{$bratari}' "; 
} 


if ( ! empty($cercei) ) 
{ 
   if ( $or) 
   { 
      $query .= " OR "; 
}
else
{  
$query.= " WHERE";
    $or = true; 
   } 
   $query .= " categorie = '{$cercei}' "; 
} 


$result=mysql_query($query);
while($row = mysql_fetch_array($result))
{


echo $row['product_name'];
   
} 

 

As for the second problem.  I should know that but I don't.  I will let you know what I can dig up, but I haven't had the issue of the query dropping the second word enclosed in quotes.  I'll let you know if I can find out what is going on there.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.