I'm reading data from my DB and displaying it in a table like this

while($row = mysql_fetch_row($result)){
          $_SESSION['edit'] = $row[1];//it says undefined index 'edit' 
           echo '</tr>';
 echo ' <td class="product"><a href="manage-products-2.php?prod_id ='.$row[0].'">'.$row[1].'</a></td>';//it says undefined index 'prod_id' 
          echo'<td class="quantity">'.$row[5].'</td>';
          echo '<td class="item_price">'.$row[4].'</td>';
          echo '<td class="item_total">'.$row[6].'</td>';
        echo '<td class="item_unsold"><a href = "manage-products.php?prod ='.$row[0].'" style="color:red" onclick="return confirm("Are you sure you want to delete this product ?")">Delete</a></td>';//to delete an item
        echo '</tr>';
}

On a separate script called manage-products2.php, i'm retrieving these values like this:

$edit = $_SESSION['edit'];
$prodid = $_GET['prod_id'];

But then i get the errors: undefined index 'edit' and undefined index 'prod_id' when i try to display these values like this for example <?php echo '<div class="question2 ha">'.$prodid.'</div>';?>. Please where is my mistake?. Thank You.

Recommended Answers

All 9 Replies

Did you:

  1. Check if you started a session in both files, using session_start()?
  2. Validate that each $row actually contains the values you expect it to contain? You can use var_dump($row), for example, to output a row (or print_r($row)).
  3. On your second page - manage-products2.php - you could check if $_SESSION and $_GET contain any values, by using those same var_dump() and/or print_r() functions.

Could you tell us the results of these tests? :)

i started a session on both files using session_start().
Also var_dump($row) contains the correct values.
var_dump($_SESSION) displays correct values but it seems $_SESSION['edit'] will always contains the same value.
However when i did var_dump($_GET) on manageproducts2.php i got. array(0){}.; an empty array.

Any idea why?

Well if your $_GET is empty, it is usually because there is something wrong with the URL that was used to load your page. Does your URL contain all the key=value pairs you need? (E.g. prod_id=1&name=dhani etc.)

sorry but i don't understand what you mean when you say; "(E.g. prod_id=1&name=dhani etc.)". The URL is like this
echo ' <td class="product"><a href="manage-products-2.php?prod_id ='.$row[0].'">'.$row[1].'</a></td>';
I'm supposed to include the key=value pairs in the URL as well. I'm confused. Thanks

<a href="manage-products-2.php?prod_id ='.$row[0].'">'.$row[1].'</a>

Above code have space after prod_id. remove space "//it says undefined index 'prod_id'" gets eliminated

For undefined index 'edit' add

session_start()

On both the pages

But $_SESSION['edit'] will always give you last value which it encounters in while loop
Hence will always give you last row[1] value.

@code_rum i removed the space after prod_id like this:
echo ' <td class="product"><a href="manage-products-2.php?prod_id='.$row[0].'">'.$row[1].'</a></td>';
but the undefined index error is still there and var_dump($_GET) still returns an empty array.

So when you inspect your URL on page, what does it look like? I mean this url:

"manage-products-2.php?prod_id='.$row[0].'"

Does it look like, for example, manage-products-2.php?prod_id=5 ? In other words: is the prod_id really given?

What is strange though is that your whole $_GET is empty, because even if your prod_id would be empty, you should still get "prod_id = null" when you var_dump() your $_GET. Does the URL on your manage-products-2.php page look as it should? I mean, when you load the page in your browser, does the link include the "prod_id=x" part?

It just began to work now. Magically :). i restarted the server, refreshed the page and the output was there. I dont know how because i didn't change the code. Thanks for your contribution.

Glad to hear a miracle has happened :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.