yet another unassigned variable

Posted 11 September 2013 - 04:23 AM

I do have error reporting on and I did learn from before and have print_r on:) I'm revising my show_comment(); function. ANd testing it. I do have the variable declared in variables.php file. It's actually the primary key in my database so the number will autoincrement. There is now 1 record in the database for testing purposes so technically it is set. (well, in my mind anyways:) PHP does not think so though:)

If the primary key is greater than 0, show all the comments. If it's 0 (meaning no comments), then display "there are no comments" message. Right now, since it does not know what $id is, it just gives me undefined index and displays that there are o comments text.

Here is where I have the variable declared in variables.php

<?php
$id = $_POST['id'];
?>

I have tried using instead of $id, use plain id (since the others don't take marks around the fieldnames in the mysql statement), and then the error tells me it assumes I meant 'id'. Yes, I did so I put in 'id' and then it also gives me undefined index.
How can I get this id to point to what the primary key is in the database and read it as 0 or greater as intended?
Thanks

Replies To: yet another unassigned variable

Re: yet another unassigned variable

Posted 11 September 2013 - 04:29 AM

I was thinking about all the tihings I tried as I was typing. ANd thinking about the logic and what I was trying to do. (Remember yesterday? haha) I tried everything except putting id as a fieldname in the select statement and then 'id' in the if statement. No error this time.

Awesome.

Except one problem.

Now it only shows the text message there is no comments.

It does not show my test comment that I inserted directly on server for it to show up in the select statement:(

Re: yet another unassigned variable

The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.

back to topic. of course $id is not defined. PHPís scope does not extend beyond the functionís boundary. hence you need to set $id inside.

or even better, donít use it at all. use the original.

function showcomments($conn) {
if ((int) $_POST['id'] > 0) {
// ...

on the other hand side, you could throw an exception (makes for cleaner code)

First prepare (with the databsse connection ($conn). Then execute (and it will get whatever page the user is on), and then show.
1. Not sure if my indention sucks here or not.
2. Yes, the computer is always right. I know I did something wrong so I submit:)

Re: yet another unassigned variable

Posted 11 September 2013 - 10:27 AM

I do agree. You guys happen to see me at my worst :-) I did solve some things thanks to php.net though, one this morning, a timezone issue. Didnt solve it dűe to server restrictions but found a workaround (new php function) and with error detection turned off it wont show up anyways. Its mostly when I have code written that I'm thinking "I thought things were right but I'll ask". An error you can always google. For some reason, when nothing happens, I feel more blind.
Anyways now I got 2 interviews tomorrow so I wont get to this til after. Because I'll be preparing:-) (No its not for PHP lol)