Jump to content

PHP/mySQL POST problem


PHPorbust

Recommended Posts

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  :confused:

 

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);

:confused: $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");

?>

Link to comment
https://forums.phpfreaks.com/topic/216952-phpmysql-post-problem/
Share on other sites

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>');

Archived

This topic is now archived and is closed to further replies.

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