User_not_loser Posted January 13, 2011 Share Posted January 13, 2011 Hello, Experts! Need you advice. Here is the code: <?php if ($var == "") { echo "text"; } if ($var == "*") { echo "text"; } else if ($var != "") { $query = "SELECT * FROM bd WHERE ....."; $data = mysql_query($query) or die(mysql_error()); $anymatches=mysql_num_rows($data); if ($anymatches != 0) { echo "text"; while($row = mysql_fetch_array($data)) { echo "text"; } } if ($anymatches == 0) { $query2 = "SELECT * FROM bd WHERE....."; $data2 = mysql_query($query2) or die(mysql_error()); $anymatches2=mysql_num_rows($data2); if ($anymatches2 != 0) { echo "text" else { mysql_query("INSERT INTO notfound (notfound) VALUES ('$var')") or die(mysql_error()); } } } ?> The last line adds two identical lines to my table. Do I run it twice? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/ Share on other sites More sharing options...
ttocskcaj Posted January 13, 2011 Share Posted January 13, 2011 Try this $query = "SELECT * FROM bd WHERE ....."; $data = mysql_query($query) or die(mysql_error()); $anymatches=mysql_num_rows($data); if ($anymatches != 0) { echo "text"; while($row = mysql_fetch_array($data)) { echo "text"; } } if ($anymatches == 0) { $query2 = "SELECT * FROM bd WHERE....."; $data2 = mysql_query($query2) or die(mysql_error()); $anymatches2=mysql_num_rows($data2); if ($anymatches2 != 0) { echo "text"; } else { mysql_query("INSERT INTO notfound (notfound) VALUES ('$var')") or die(mysql_error()); } } Looked like your {}'s where messed up. Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/#findComment-1158811 Share on other sites More sharing options...
ttocskcaj Posted January 13, 2011 Share Posted January 13, 2011 It may just be on this site, but try and keep your indentation structured, it helps you read your code, and close braces properly. Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/#findComment-1158816 Share on other sites More sharing options...
User_not_loser Posted January 13, 2011 Author Share Posted January 13, 2011 Try this Sorry, but I can't see the difference... except making it shorter... Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/#findComment-1158820 Share on other sites More sharing options...
QuickOldCar Posted January 13, 2011 Share Posted January 13, 2011 More information would be needed if wanted help. The code you posted is incomplete and just a bunch of echoed "text" and partial select statements. Can't even tell what or where $var would be anywhere in there. Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/#findComment-1158822 Share on other sites More sharing options...
User_not_loser Posted January 13, 2011 Author Share Posted January 13, 2011 Here is the whole thing: <?php if ($var == "") { echo "<center>Empty</center>"; } else if ($var != "") { $query = "SELECT * FROM bd1 WHERE word LIKE '$var'"; $data = mysql_query($query) or die(mysql_error()); $anymatches=mysql_num_rows($data); if ($anymatches != 0) { while($row = mysql_fetch_array($data)) { echo "{$row['word']}"; } } if ($anymatches == 0) { $query2 = "SELECT * FROM bd2 WHERE word LIKE '$var'"; $data2 = mysql_query($query2) or die(mysql_error()); $anymatches2=mysql_num_rows($data2); if ($anymatches2 != 0) { while($row = mysql_fetch_array($data2)) { echo "{$row['word']}"; } } else { mysql_query("INSERT INTO newwords (notfound) VALUES ('$var')") or die(mysql_error()); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/#findComment-1158832 Share on other sites More sharing options...
ttocskcaj Posted January 13, 2011 Share Posted January 13, 2011 Instead of using else if ($var != "") use else if(isset($var)) Apart from that, I'm not too sure what's wrong. Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/#findComment-1158836 Share on other sites More sharing options...
User_not_loser Posted January 13, 2011 Author Share Posted January 13, 2011 ttocskcaj, how can it be connected with INSERT in the last line? Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/#findComment-1158844 Share on other sites More sharing options...
ttocskcaj Posted January 14, 2011 Share Posted January 14, 2011 Ok, I've gone over it again And came up with this <?php if (empty($var)) echo "<center>Empty</center>"; else { //Search bd1 for var. $query = "SELECT * FROM bd1 WHERE `word`='$var'"; $data = mysql_query($query) or die(mysql_error()); if (1>mysql_num_rows($data)) { //If there's no results from bd1 search bd2 $query = "SELECT * FROM bd2 WHERE `word`='$var'"; $data = mysql_query($query) or die(mysql_error()); } if (0<mysql_num_rows($data)) { //If there where results found in either bd1 or bd2, print them out. while($row = mysql_fetch_array($data2)) echo "{$row['word']}"; } //Else insert a log into the new words table. else mysql_query("INSERT INTO `newwords (notfound)` VALUES ('$var')") or die(mysql_error()); //Not sure if your table is called 'newwords (notfound)' or just 'newwords' Maybe just use this? // else mysql_query("INSERT INTO `newwords` VALUES ('$var')") or die(mysql_error()); } ?> I condensed it a bit to get rid of braces, Assuming you have a html form to search for a word ($var) if you want $var to match what's in the database EXACTLY you should be using WHERE `word`='$var' in your querys. If you want to "search" for var. for example you enter "ttocs" in the form it will match the row with "ttocskcaj" use WHERE `word`LIKE '%$var%' Note the % wildcards. Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/#findComment-1159191 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.