differ8 Posted July 10, 2008 Share Posted July 10, 2008 hey Guys this has been driving me nuts for days, im hoping someone can give me a hand with this code.....whats wrong with it...its obviously something with my If(condition){and} is there a better way to do this?? code is as follows: <?php $tileCode = $_POST["tileCode"]; $tileCategory = $_POST["tileCategory"]; $tileFactory = $_POST["tileFactory"]; $tileFinish = $_POST["tileFinish"]; $tileColour = $_POST["tileColour"]; $tileSize = $_POST["tileSize"]; $mysqli = new mysqli("localhost", "username", "password", "database"); $Tiles = "select * From tiles where tileCode = tileCode if ($tileCode == "") {and tileCode like '%$tileCode%';} if ($tileCategory == "") {and tileCategory = '$tileCategory';} "; ?> Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/ Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 Theres all sorts of things wrong here. Are those condiftions meant to be within your sql statement? They should'nt be. You never actually execute any query either. Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586281 Share on other sites More sharing options...
differ8 Posted July 10, 2008 Author Share Posted July 10, 2008 Well basically i have a form with Select boxes , when the form is submitted it produces this page. Now the problem i have is that my Select boxes have an "any" position and if the any is selected i want it to ignore the "and" part of the sql but if its not i want it to do it? do u know what i mean?? if you have messenger could you please add me so i can speak about this a bit better and send u the files EDIT: messenger details removed. Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586295 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 if you have messenger could you please add me so i can speak about this a bit better and send u the files Your missing the point of a public forum. I'm not your psersonal assistant. Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586329 Share on other sites More sharing options...
differ8 Posted July 10, 2008 Author Share Posted July 10, 2008 well guess what buddy, ur not real usefull on here either...so wake up to urself hey and dont be so rude, i was just asking for help Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586337 Share on other sites More sharing options...
slushpuppie Posted July 10, 2008 Share Posted July 10, 2008 you are not making a legitimate sql query... your sql is literally: "select * From tiles where tileCode = tileCode if ($tileCode == "") {and tileCode like '%$tileCode%';} if ($tileCategory == "") {and tileCategory = '$tileCategory';} " what you need is something like: <?php $tileCode = "select * From tiles where tileCode = tileCode "; ## ADDING AND CONDITIONS TO THE QUERY if ($tileCode == "") {$tileCode .= " and tileCode like '%$tileCode%'";} if ($tileCategory == "") {$tileCode .= " and tileCategory = '$tileCategory'";} echo $tileCode; ?> that should output a better query... then after you verify it outputs properly just execure it: mysql_query($tileCode) Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586340 Share on other sites More sharing options...
differ8 Posted July 10, 2008 Author Share Posted July 10, 2008 thank you very much, i think thats what ive been after. Ill have a look at it now Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586342 Share on other sites More sharing options...
slushpuppie Posted July 10, 2008 Share Posted July 10, 2008 yup - basically use your if's to BUILD your query, not to RUN it. just append stuff to your $tileCode variable using .= within your if's and then execute it after all your conditionals. you can't intermingle the if's the way you did within a string, your code was actually being put INTO the string i believe. and as always, just echo your $tileCode variable before you start executing it to see if it looks like a valid SQL statement. Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586345 Share on other sites More sharing options...
BillyBoB Posted July 10, 2008 Share Posted July 10, 2008 differ8: He isn't being rude this is a public forum for people to work php problems out on if you want your problem solved then use the forum. Also use the forums correctly put your code inside [ code] brackets. thorpe is right you have your syntax all messed up. <?php $tileCode = $_POST["tileCode"]; $tileCategory = $_POST["tileCategory"]; $tileFactory = $_POST["tileFactory"]; $tileFinish = $_POST["tileFinish"]; $tileColour = $_POST["tileColour"]; $tileSize = $_POST["tileSize"]; $mysqli = new mysqli("localhost", "username", "password", "database"); $Tiles = "SELECT * FROM `tiles` WHERE `tileCode` = '$tileCode' "; if ($tileCode == "") {$Tiles .= "AND `tileCode` LIKE '%$tileCode%'";} if ($tileCategory == "") {$Tiles .= "AND `tileCategory` = '$tileCategory'";} ?> Dang got to it first... lol. But I want to get my first point across also you should use complete SQL syntax Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586346 Share on other sites More sharing options...
discomatt Posted July 10, 2008 Share Posted July 10, 2008 well guess what buddy, ur not real usefull on here either...so wake up to urself hey and dont be so rude, i was just asking for help No, you were asking for personal 1 on 1 live support. If it's offered... nice, but don't ask that of volunteers who come here to help you out of their own free time, and then call them a jerk for saying no. slushpuppie <?php $tileCode = "select * From tiles where tileCode = tileCode "; # Below will never get executed, due to it being defined above if ($tileCode == "") {$tileCode .= " and tileCode like '%$tileCode%'";} ?> differ8 You should look up MySQL syntax... it differs quite a bit from PHP. Usually the if/thens are best left to PHP. Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586350 Share on other sites More sharing options...
differ8 Posted July 10, 2008 Author Share Posted July 10, 2008 that outputs select * From tiles where tileCode = tileCode and tileCode like '%select * From tiles where tileCode = tileCode %' and tileCategory = 'Any' this is all of my code: <?php $tileCode = $_POST["tileCode"]; $tileCategory = $_POST["tileCategory"]; $tileFactory = $_POST["tileFactory"]; $tileFinish = $_POST["tileFinish"]; $tileColour = $_POST["tileColour"]; $tileSize = $_POST["tileSize"]; $categoryCondition = 'if($tileCategory != "")and tileCategory = $tileCategory;'; $mysqli = new mysqli("localhost", "bradytiles", "oo7bro", "bradytiles"); $tileCode = "select * From tiles where tileCode = tileCode "; ## ADDING AND CONDITIONS TO THE QUERY if ($tileCode != "") {$tileCode .= " and tileCode like '%$tileCode%'";} if ($tileCategory != "") {$tileCode .= " and tileCategory = '$tileCategory'";} echo $tileCode; ?> <html> <head> <title>Personal INFO</title> </head> <body> <table border ="1"> <?php if ($result = $mysqli->query($Tiles)) { while ($row = $result->fetch_row()) { echo "<tr><td>".$row[0]."</td>"."<td>".$row[1]."</td>"."<td>".$row[2]."</td>"."<td>".$row[3]."</td>"."<td>".$row[4]."</td>"."<td>".$row[5]."</td>"."<td>".$row[6]."</td></tr>";} if (mysqli_num_rows($result) == 0) { echo "Sorry your search did not display and results!<br>Please click <a href='searchform.php'>Here</a> to return to the search page!"; } } ?> </table> Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586353 Share on other sites More sharing options...
slushpuppie Posted July 10, 2008 Share Posted July 10, 2008 discomatt... $sql = "hello"; $sql .= " discomatt"; will output: hello discomatt i was appending the AND's onto the SQL using .= Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586357 Share on other sites More sharing options...
BillyBoB Posted July 10, 2008 Share Posted July 10, 2008 Use my code you have an error in the beginning of your SQL query where it says tileCode = tileCode should be something like tileCode = '$tileCode' Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586359 Share on other sites More sharing options...
slushpuppie Posted July 10, 2008 Share Posted July 10, 2008 discomatt... nevermind - i see what you were saying. i named the SQL variable wrong. it should be $Tiles.................................. must be too early... maybe i should stop posting for a while Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586361 Share on other sites More sharing options...
discomatt Posted July 10, 2008 Share Posted July 10, 2008 Differ8... check your variable names and where you're using them... if you can't figure out what's wrong, you should be practicing the basics, and leaving database transactions alone until you're more comfortable with the core language. Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586366 Share on other sites More sharing options...
differ8 Posted July 10, 2008 Author Share Posted July 10, 2008 lol wish i could, i got thrown in the deep end on this one!! wrote a script in coldFusion (which im comfortable with) and the client got hsoting with php support not coldFusion...so i gotta grudge it out and get it done! thanks for the help ill give it all a go! Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586372 Share on other sites More sharing options...
discomatt Posted July 10, 2008 Share Posted July 10, 2008 Why not just move hosts? If your client is going to be frugal, then he should consult before making any decisions regarding your script. If it was mis-communication on your part, good luck Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586383 Share on other sites More sharing options...
differ8 Posted July 10, 2008 Author Share Posted July 10, 2008 im not dealing directly with the client, im just the sucker that has to fix the problem!!! but anyway its a good chance to learn php! im doin ok i think for only 2 days into it! i got the solution for anyone who is interested, the final code is posted below, Sorry i snapped im just frustrated and wasnt getting anywhere! thanks for eveyrones help. <?php $tileCode = $_POST["tileCode"]; $tileCategory = $_POST["tileCategory"]; $tileFactory = $_POST["tileFactory"]; $tileFinish = $_POST["tileFinish"]; $tileColour = $_POST["tileColour"]; $tileSize = $_POST["tileSize"]; $mysqli = new mysqli("localhost", "username", "password", "database"); $Tiles = "SELECT * FROM tiles WHERE tileCode = tileCode "; if ($tileCode != "") {$Tiles .= "AND tileCode LIKE '%$tileCode%'";} if ($tileCategory != "Any") {$Tiles .= "AND tileCategory = '$tileCategory'";} if ($tileFactory != "Any") {$Tiles .= "AND tileFactory = '$tileFactory'";} if ($tileFinish != "Any") {$Tiles .= "AND tileFinish = '$tileFinish'";} if ($tileColour != "Any") {$Tiles .= "AND tileColour = '$tileColour'";} if ($tileSize != "Any") {$Tiles .= "AND tileSize = '$tileSize'";} ## echo $Tiles; ?> <html> <head> <title>Personal INFO</title> </head> <body> <table border ="1"> <?php if ($result = $mysqli->query($Tiles)) { while ($row = $result->fetch_row()) { echo "<tr><td>".$row[0]."</td>"."<td>".$row[1]."</td>"."<td>".$row[2]."</td>"."<td>".$row[3]."</td>"."<td>".$row[4]."</td>"."<td>".$row[5]."</td>"."<td>".$row[6]."</td></tr>";} if (mysqli_num_rows($result) == 0) { echo "Sorry your search did not display and results!<br>Please click <a href='searchform.php'>Here</a> to return to the search page!"; } } ?> </table> Link to comment https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/#findComment-586394 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.