Jump to content

Archived

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

nickholt1972

Sessions-related error,

Recommended Posts

I had a shopping page which was working fine only I didn't like it. So in trying to fix what wasn't broken ... guess what? I broke it.

I introduced sessions and shortly afterwards made the short trip to errormessagesville

My products.php page starts with the following code

[code]<?php
session_start();
if (!isset($_SESSION['cart'])) {
  $_SESSION['cart'] = array();
}
if (isset($_GET['buy'])) {
  // Add item to the end of the $_SESSION['cart'] array
  $_SESSION['cart'][] = $_GET['buy'];
  header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
  exit();
}
?>[/code]
then it does some other SELECT FROM related shenanigans, before giving it some of this.. (and its this line which brings about the error)

[code]while ($row = mysql_fetch_array($result)) {[/code]

within my while statement, I display my product details and include the following link, which is SUPPOSED to add the product id to my array of purchased products.

[code]<a href="' . $_SERVER['PHP_SELF'] .'?buy=' . $row['id'] . '">Buy</a>[/code]

When I run the page, the number of items in my cart goes up, which is fine. However, I get the following message:

[i]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/.sites/123/site117/web/display2.inc.php on line 3[/i]
(line 3 is [b]while ($row = mysql_fetch_array($result)) {[/b])

What I think i happening is the [b]while ($row...[/b] line is losing track of the preceding SELECT line so there's effectively no data but this only happened since I started piddling about with these pesky sessions.

can any one suggest a solution?

Share this post


Link to post
Share on other sites
Further to my original post, I think I might not have explained myself too well so i'm posting the complete script (more or less, slightly simplified so you've less to trawl through)


[code]<?php
session_start();
if (!isset($_SESSION['cart'])) {
  $_SESSION['cart'] = array();
}
if (isset($_GET['buy'])) {
  // Add item to the end of the $_SESSION['cart'] array
  $_SESSION['cart'][] = $_GET['buy'];
  header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
  exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>apple tree nappies online shop</title>
</head>
<body>

<?php

include_once 'dbconnect.inc.php';

if ($choice == "allnappies")
{$result = @mysql_query('SELECT * FROM stocks WHERE alphatype = "NAPPY" AND display = "yes" ORDER BY id ASC'); }

elseif ($choice == "onesize")
{$result = @mysql_query('SELECT * FROM stocks WHERE sizetype = "One Size" AND display = "yes" ORDER BY id ASC');}

elseif ($choice == "multisize")
{$result = @mysql_query('SELECT * FROM stocks WHERE sizetype = "Multi Size" AND display = "yes" ORDER BY id ASC');}



  // Display the title of each entry with a 'buy' link in a table

// THIS FOLLOWING LINE IS THE ONE GIVING ME AN ERROR //
while ($row = mysql_fetch_array($result)) {

<table class="products">
<tr>
<td>' . $row['alphaname'] . '</td></td>
<td>
<a href="' . $_SERVER['PHP_SELF'] .'?buy=' . $row['id'] . '">Buy</a>
</td>
</tr>
</table>';
  }

?>
<?php echo count($_SESSION['cart']); ?>
<p><a href="cart.php">View Cart</a></p>


</body>
</html>[/code]

I'd really appreciate your input.

Thanks.

Share this post


Link to post
Share on other sites
$choice does not appear to get a value from anywhere, effectively meaning non of these:

[code]
if ($choice == "allnappies")
{$result = @mysql_query('SELECT * FROM stocks WHERE alphatype = "NAPPY" AND display = "yes" ORDER BY id ASC'); }

elseif ($choice == "onesize")
{$result = @mysql_query('SELECT * FROM stocks WHERE sizetype = "One Size" AND display = "yes" ORDER BY id ASC');}

elseif ($choice == "multisize")
{$result = @mysql_query('SELECT * FROM stocks WHERE sizetype = "Multi Size" AND display = "yes" ORDER BY id ASC');}
[/code]
will get executed and meaning that $result, which you expect to be the result of a query, will not get set,resulting in the error you've gotten here. it's worth noting that in many cases, using the error suppressor (@) can make debugging pretty tricky. as the error coming back from mysql_query will only be stuff like not being able to connect to the database, or invalid SQL, you'd be better doing a proper check elsewhere - i.e, let the mysql_query's throw errors if they have to, so you can fix them. something like:

[code]
<?php
// first make sure we actually have a choice
if ($choice)
{
  $query = '';

  // using switch to simplify all the ifs/elses etc
  switch ($choice)
  {
      case 'allnappies':
        $query = 'SELECT * FROM stocks WHERE alphatype = "NAPPY" AND display = "yes" ORDER BY id ASC';
        break;

      case 'onesize':
        $query = 'SELECT * FROM stocks WHERE sizetype = "One Size" AND display = "yes" ORDER BY id ASC';
        break;

      case 'multisize':
        $query = 'SELECT * FROM stocks WHERE sizetype = "Multi Size" AND display = "yes" ORDER BY id ASC';
        break;
  }

  // if we have a query, run it.
  if ($query)
  {
      $result = mysql_query($query) or die('Problem with '.$query);

      // rest of code here...
  }
  else
  {
      echo 'Invalid choice';
      die;
  }
}
else
{
  echo 'No choice specified';
  die;
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.