Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php mysql $id problem

    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:

    PHP Code:
    $query="SELECT * FROM home WHERE id='$id'"
    on my edit page, the page doesn't display; however, if I do:

    PHP Code:
    $query="SELECT * FROM home WHERE id='1'"
    It works. The page I'm working on http://www.katherinewagnerdesigns.co...ford/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.co...lford/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
    Last edited by mlmorg; 07-17-2007 at 09:51 PM.

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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

    HTH
    Dan
    Last edited by whizard; 07-17-2007 at 09:58 PM. Reason: I seemed to mistype a word in such a fashion as to confuse the filter
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,077
    Thanks
    2
    Thanked 320 Times in 312 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Regular Coder
    Join Date
    Mar 2007
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"

  • #5
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    $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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #6
    Regular Coder
    Join Date
    Mar 2007
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *edit*
    Nevermind, figured it all out. Thanks for the help in pushing me forward guys, it's greatly appreciated.

    Thanks, Matt
    Last edited by mlmorg; 07-18-2007 at 01:08 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •