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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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