Jump to content

Sessions-related error,


nickholt1972

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?
Link to comment
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.
Link to comment
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]
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.