Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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