SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    php value passing

    hi,
    i am having trouble echoing from posted page, in the url it shows the send id but it gives me error for this query
    Code PHP:
    $_GET['$songs_set["id"]'];
    and i get this error
    Undefined index: $songs_set["id"]
    All those who wander aren't lost.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    What does the url look like?
    My guess is it should be:
    Code:
    $_GET['id'];

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    I'm guessing actually you've gotten your arrays backwards.

    Were you trying to do $songs_set[$_GET['id']] ?

  4. #4
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What does the url look like?
    My guess is it should be:
    Code:
    $_GET['id'];
    single.php?song =1
    All those who wander aren't lost.

  5. #5
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Were you trying to do $songs_set[$_GET['id']] ?
    yes
    All those who wander aren't lost.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    single.php?song =1
    I hope there is no space between song and =

  7. #7
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Code:
    single.php?song =1
    I hope there is no space between song and =
    there is and i was wondering if that is the reason
    edit : i fixed the gap
    All those who wander aren't lost.

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    The undefined index error is because you had things mixed up like StarLion said.
    Change your code like he told you, and try again.

  9. #9
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    The undefined index error is because you had things mixed up like StarLion said.
    Change your code like he told you, and try again.
    i still get the error
    Code PHP:
    $_REQUEST['id'];
    $song = $_REQUEST["id"];
    Notice: Undefined index: id

    Notice: Undefined index: $songs_set["id"]
    All those who wander aren't lost.

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Because the variable name isn't 'id', but 'song'. You called it that way in the query string.

    single.php?key=value

    $_GET['key'] contains value

    single.php?song=1 (get rid of that space)

    $_GET['song'] contains 1

  11. #11
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Because the variable name isn't 'id', but 'song'. You called it that way in the query string.

    single.php?key=value

    $_GET['key'] contains value

    single.php?song=1 (get rid of that space)

    $_GET['song'] contains 1
    i didn't got any of it.
    All those who wander aren't lost.

  12. #12
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Let's add some color:
    Code:
    single.php?key=value
    
    $_GET['key'] contains value
    
    single.php?song=1 
    
    $_GET['song'] contains 1
    I can't explain it any clearer than that.
    Do a print_r($_GET); in your code to see the contents of $_GET.

  13. #13
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i don't know what is wrong. i am reading a tut too it says the same about passing and getting value, this is page 1
    Code PHP:
    <a href=\"single.php?song=key{$songs_set["id"]}\">".$songs_set["title"]."</a>
    this is page 2
    Code PHP:
    $song = $_REQUEST['id'];
    and i m echoing on page 2
    Code PHP:
    echo $song;
    All those who wander aren't lost.

  14. #14
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    here is my code
    Code PHP:
    <form action="single.php" method="post">
                                    <div class="songslist">
                                        <table class="songs" width="1020" border="0" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <th width="281">Artist</th>
                                            <th width="281">Title</th>
                                            <th width="204">Length</th>
                                            <th width="204">Genre</th>
                                            <th width="50">Ratings</th>
                                        </tr>
                                        </table>
                                        <table class="songs" width="1020" border="0" cellpadding="0" cellspacing="0">
                                        <tr>
                                        <?php
                                        $songs = mysql_query("SELECT * FROM songs LIMIT 10");
                                        if($songs){
                                            echo mysql_error();
                                        }
                                        while($songs_set = mysql_fetch_array($songs)){
                                            echo "<tr>
                                                   <td width=\"281\">".$songs_set["artist"]."</td>"
                                                . "<td width=\"281\"><a href=\"single.php?song={$songs_set["id"]}\">".$songs_set["title"]."</a></td>"
                                                . "<td width=\"204\">".$songs_set["length"]."</td>"
                                                . "<td width=\"204\">".$songs_set["genre"]."</td>"
                                                . "<td width=\"50\">".$songs_set["rating"]."</td><tr>";
                                        }
                                        ?>
                                        </table>
                         </div>
                             </form>
    and here is the single page
    Code PHP:
    <?php
    $song = $_REQUEST['id'];
    echo "$song";
    ?>
    now can u guide me where am i going wrong?
    All those who wander aren't lost.

  15. #15
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    single.php?song=1

    $_REQUEST['id'];

    This is where you're going wrong.
    Like I told you, do a print_r($_GET); in your code (or a print_r($_REQUEST); ), and you'll see what exactly is sent to single.php

  16. #16
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    its a empty Array. i still don't understand what i m going wrong
    All those who wander aren't lost.

  17. #17
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Change
    PHP Code:
    song={$songs_set["id"]} 
    in
    PHP Code:
    song={$songs_set['id']} 
    That line should have given you an error though.

  18. #18
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    u told me to remove double quotes. but it didn't solve the undefined index error on next page
    All those who wander aren't lost.

  19. #19
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Did you change id to song?

  20. #20
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=guido2004;4881072]Did you change id to song?[/QUOTE} still undefined index error
    All those who wander aren't lost.

  21. #21
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    and the print_r of $_GET still gives an empty error?

    Can you check the html code of the first page in your browser and post the link code here?

  22. #22
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    well i get undefined index for print_r for songs.i did posted the page code above,here i'll do it again
    Code HTML4Strict:
    <form action="single.php" method="post">
                                    <div class="songslist">
                                        <table class="songs" width="1020" border="0" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <th width="281">Artist</th>
                                            <th width="281">Title</th>
                                            <th width="204">Length</th>
                                            <th width="204">Genre</th>
                                            <th width="50">Ratings</th>
                                        </tr>
                                        </table>
                                        <table class="songs" width="1020" border="0" cellpadding="0" cellspacing="0">
                                        <tr>
                                        <?php
                                        $songs = mysql_query("SELECT * FROM songs LIMIT 10");
                                        if($songs){
                                            echo mysql_error();
                                        }
                                        while($songs_set = mysql_fetch_array($songs)){
                                            echo "<tr>
                                                   <td width=\"281\">".$songs_set["artist"]."</td>"
                                                . "<td width=\"281\"><a href=\"single.php?song={$songs_set['id']}\">".$songs_set["title"]."</a></td>"
                                                . "<td width=\"204\">".$songs_set["length"]."</td>"
                                                . "<td width=\"204\">".$songs_set["genre"]."</td>"
                                                . "<td width=\"50\">".$songs_set["rating"]."</td><tr>";
                                        }
                                        ?>
                                        </table>
                         </div>
                             </form>
    and second page
    Code PHP:
    print_r ($_POST['song']);
    All those who wander aren't lost.

  23. #23
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Why don't you just do what I ask you?
    The first time I asked you about $_GET, you changed things to $_REQUEST.
    Now, you change things to $_POST.

    Of course things don't work. You send the data in the query string of the link, so $_POST is empty. You must use $_GET.

  24. #24
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Why don't you just do what I ask you?
    The first time I asked you about $_GET, you changed things to $_REQUEST.
    Now, you change things to $_POST.

    Of course things don't work. You send the data in the query string of the link, so $_POST is empty. You must use $_GET.
    my bad.. i m just lost. it worked so now can i use this id to put it in query to get information from the table?
    p.s - thanks
    All those who wander aren't lost.

  25. #25
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nofel View Post
    my bad.. i m just lost.
    Yes, I understand the feeling And it's a good thing you're not just waiting for answers, but also trying yourself. It's just hard to help when the feedback says my suggestions didn't work, while in reality they did work, but other changes created a new error (or even worse, the same error).
    it worked
    good
    so now can i use this id to put it in query to get information from the table?
    First you'll have to sanitize it. If it's supposed to be a number, cast it with (int). If it's a string, put it through mysql_real_escape_string. And then you can use it in a query.


Bookmarks

Posting Permissions

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