PHPorbust Posted October 27, 2010 Share Posted October 27, 2010 Getting an intermittent error...ughh challenging Overview: I am trying to use a drop down menu populated from a mySql database. Once the choice is selected it redirects to a php index page that uses if/else to redirect the page. If it works correctly the redirect is back to the original page but now contains the POST ID associated with the user's choice which goes to another mySQL query and returns the echo'd results. Problem is, sometimes this works and sometimes I get an error. It always works the first time. When it does error and I echo the ID, it is blank. I can reselect the same choice or a different one and sometimes it works, sometimes not. Any help would be appreciated. IE Error:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /html/includes/search.php on line search.php: <?php require("connect.php"); //extract data mySQL $extract = mysql_query("SELECT * FROM business ORDER BY id ASC"); echo "<form action ='index.php?page=search' method='POST'>"; echo "<select name='id'>"; while ($row = mysql_fetch_assoc($extract)) { $id = $row['id']; $grocery = $row[‘grocery']; echo "<option value ='$id'>$grocery</option>"; } echo "</select>"; echo "<input type ='submit' value='submit'></form>"; ?> <?php //extract data mySQL $q2 = "SELECT * FROM business WHERE ID = $_POST[id]"; $extract2 = mysql_query($q2); $result2 = mysql_fetch_array($extract2); $groc2 = $result2[‘grocery']; $fru2 = $result2[‘fruit']; ?> Here is the php index page that directs: ?php include("includes/header.html"); include("includes/navbar.html"); if($_GET['page'] == "stores"){ include("includes/stores.php"); }else if($_GET['page'] == "search"){ include("includes/search.php"); }else if($_GET['page'] == "toys"){ include("includes/toys.html"); }else{ include("includes/home.html"); } include("includes/footer.html"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/ Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2010 Share Posted October 27, 2010 The error indicates a failed query. I'd start by adding some debugging code to the query that's producing the error. $q2 = "SELECT * FROM business WHERE ID = $_POST[id]"; $extract2 = mysql_query($q2) or die( '<br>Query: ' . $q2 . '<br>Produced error: ' mysql_error() . '<br>'); Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126929 Share on other sites More sharing options...
PHPorbust Posted October 27, 2010 Author Share Posted October 27, 2010 $extract2 = mysql_query($q2) or die('<br>Query:'.$q2.'<br>Produced error:'mysql_error()'); = Parse error: syntax error, unexpected T_STRING in /***/search.php on line Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126935 Share on other sites More sharing options...
PHPorbust Posted October 27, 2010 Author Share Posted October 27, 2010 I do get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 when I use: $extract2 = mysql_query($q2) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126937 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2010 Share Posted October 27, 2010 I missed a concatenation "dot" ' . mysql_error() ^ there Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126939 Share on other sites More sharing options...
PHPorbust Posted October 27, 2010 Author Share Posted October 27, 2010 $extract2 = mysql_query($q2) or die('<br>Query:'.$q2.'<br>Produced error:'.mysql_error()'); = Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /***/search.php on line +2 Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126942 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2010 Share Posted October 27, 2010 You've omitted the last <br>, but left an extra ' between the last 2 parentheses. Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126943 Share on other sites More sharing options...
PHPorbust Posted October 27, 2010 Author Share Posted October 27, 2010 Here you go.... Query: SELECT * FROM business WHERE ID = Produced error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126944 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2010 Share Posted October 27, 2010 It's exactly what I expected; I just don't see anything in the code that would cause it to happen. Are you getting any other errors at all? Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126949 Share on other sites More sharing options...
PHPorbust Posted October 27, 2010 Author Share Posted October 27, 2010 What did you expect exactly? No other errors at all that I can see . Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126950 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2010 Share Posted October 27, 2010 I expected to see a blank spot at the end of the query, and that's what was there . . . Hmmmm. There's no pattern at all as to when this occurs, huh? Quote Link to comment https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/#findComment-1126971 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.