View Full Version : php mysql $id problem

07-17-2007, 10:26 PM
Hi all,
I'm very new to php mysql but have been learning from http://www.freewebmasterhelp.com/tutorials/phpmysql/1

I have set up a web page and my home page (the one that displays what is in my database) works nicely; however, my edit page does not work unless I specify the exact id number I want to edit. Even though there's currently only one row in my database, if I have:

$query="SELECT * FROM home WHERE id='$id'";

on my edit page, the page doesn't display; however, if I do:

$query="SELECT * FROM home WHERE id='1'";

It works. The page I'm working on http://www.katherinewagnerdesigns.com/test/guilford/index.php is set up with two "posts". One titled "Movies in the Park," the other "Annual Flea Market and Crafts Fair." Since I just started learning php mysql, only the first post uses php (the second one html for now). The edit page, http://www.katherinewagnerdesigns.com/test/guilford/edit.php right now is set up with id=1 but I would like to have it so the edit page has every post on it, with the ability to add new posts and edit/delete old ones. So, it seems that this problem I'm having will stop me from being able to do this unless I can get it to work and auto-increment down the page each successive id post.

I hope this makes sense to everyone...sorry I'm still learning this. Thanks for any help anyone can give me. Matt

07-17-2007, 10:46 PM
I think what you might want to do is have a couple different pages: one to add a new post, one to view all the current posts, and one to edit posts.

So, your page to show all the post could have a link at the bottom along the lines of "Click here to add new post", which would point to the new post script. Then, the page would also list all the current posts with a link next to them that says "Click here to edit post", and point to edit.php?id=ID where ID is equal to that post's id in the database. Then you could use the more dynamic query you already have set up in edit.php to edit the requested article


07-17-2007, 10:55 PM
Actually, this is php problem because the query is only doing what you tell it to. I suspect that the $id variable is empty.

Echo out your $query variable after you assign the string to it to see if it contains the expected contents (any debugging involves finding at what point the data is the expected or when it is not and tracing back through the code/data to find out why it is not as expected.)

If you find that the $id variable is empty or contains something other than the record number you are trying to display, back track through the program execution path until you find out why it does not contain the expected value.

07-17-2007, 11:06 PM
CF it looks like you were right...I checked in phpmyadmin and the id was set to 1...But I assume $id starts at 0...so I changed it to 0 and it seemed to work. Thanks very much!

Now is there a way to set up the edit page so that it displays however many posts there are in that database table? Maybe I would use some sort of if statement..."If there is another row in this table, then display those values here"

07-17-2007, 11:09 PM
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
$row['title']."&nbsp;<br />";

Something like that should work

07-17-2007, 11:26 PM
Nevermind, figured it all out. Thanks for the help in pushing me forward guys, it's greatly appreciated.

Thanks, Matt