masterwebxz Posted April 27, 2010 Share Posted April 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/ Share on other sites More sharing options...
andrewgauger Posted April 27, 2010 Share Posted April 27, 2010 $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? Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049418 Share on other sites More sharing options...
masterwebxz Posted April 27, 2010 Author Share Posted April 27, 2010 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049423 Share on other sites More sharing options...
andrewgauger Posted April 27, 2010 Share Posted April 27, 2010 Just add 1=1 at the end of $query= line If you get another error, do an echo $query before mysql_query and post the query Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049426 Share on other sites More sharing options...
masterwebxz Posted April 27, 2010 Author Share Posted April 27, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049429 Share on other sites More sharing options...
masterwebxz Posted April 27, 2010 Author Share Posted April 27, 2010 What is wrong...I don`t understand... Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049437 Share on other sites More sharing options...
andrewgauger Posted April 27, 2010 Share Posted April 27, 2010 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049449 Share on other sites More sharing options...
masterwebxz Posted April 27, 2010 Author Share Posted April 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049466 Share on other sites More sharing options...
masterwebxz Posted April 27, 2010 Author Share Posted April 27, 2010 can help me? Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049484 Share on other sites More sharing options...
andrewgauger Posted April 27, 2010 Share Posted April 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199938-checkbox-multiple-select/#findComment-1049554 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.