1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Php and sql question...long shot tho.

Discussion in 'PHP' started by micromark, Oct 11, 2007.

  1. #1
    Hi,

    I need to make a script so members of my can post 'gifts' to other people's profiles. Like facebook does.

    I am kind of stuck on how to do this. I was going to use php and sql.

    Maybe have the images in the database and when one type og image is selected in will be posted ito the members profile.

    Has any1 got a clue on how to do this. ?
    SEMrush
     
    micromark, Oct 11, 2007 IP
    SEMrush
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Make two tables.

    One called gitfs, with 3 fields.
    - Giftid
    - Name
    - Image

    And another one called giftlog or something like that, with 4 fields.
    - Logid
    - Giftid
    - Recipientid
    - Fromid


    Then to give someone a gift.

    
    INSERT INTO giftlog
        (giftid, recipientid, fromid)
    VALUES
        (" . $giftid . ", " . $recipientid . ", " . $userid . ")
    
    Code (sql):

    Get gifts for a specific user.

    
    SELECT
        giftlog.name,
        girtlog.image
    FROM
        giftlog
    LEFT JOIN
        gifs
    USING (giftid)
    WHERE
        giftlog.recipientid = " . $userid
    
    Code (sql):

    Untested but you get the idea.
     
    nico_swd, Oct 11, 2007 IP
  3. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Have a folder in images [gifts]

    Have a database for gifts [db_gifts] , with a unique id, gift name, and image loacation (refer to > [gifts])

    When a user sends a gift it picks from a list (each with an id corresponding to [db_gifts] id's). This will bring up the correct image.


    EDIT: Bah, i was too late! lol :)
     
    Noddegamra, Oct 11, 2007 IP
  4. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    eeek. OK im not sure about how to implement this. So in short im making the table in SQL - what code do i put on the page to let the user select and send ?

    Big thanks so far.
     
    micromark, Oct 11, 2007 IP
  5. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    nico_swd : I have made those table now i need to put the code onto the page.

    Do i just copy the sql code on ? Im really not sure of this one ? Any help good ta.
     
    micromark, Oct 11, 2007 IP
  6. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    How do a write a html table to submit this ?




     
    micromark, Oct 11, 2007 IP
  7. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #7
    As you are a beginner with MySQL and PHP I recommend you try starting on a smaller project.

    http://www.tizag.com/ have some great PHP tutorials :)
     
    Noddegamra, Oct 11, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    I don't want to sound cocky, but I can't code the entire script for you. But I'll point you in the right direction.

    First, you have to add gifts to the table.

    Then you have to make a page which loops though this table and displays the photos along with a link which passes the current ID of the gift to another page (or to itself)

    Info regarding this here: www.php.net/mysql_fetch_array

    Users have to be logged in at this point.

    Once the user clicks a link, the gift ID is passed to this page, and adds a row to the database (query in first post). The gift ID, the recipients user ID and the current user ID will be inserted.

    Running queries: www.php.net/mysql_query

    You might not need this if you do it with links alone, however, if you're interedted:

    Dealing with forms: http://www.php.net/tutorial.forms
     
    nico_swd, Oct 11, 2007 IP
  9. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hmm kinda understand...I will have a play and see what happens. I really dont know mucho about MySQL.....Should be interesting.
     
    micromark, Oct 11, 2007 IP
  10. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #10
    So far..... I get how to post to the database...But i cant display what's in the first 'gifts' table ...Any1 got any help. ?
     
    micromark, Oct 11, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Raw example
    
    $query = mysql_query("SELECT * FROM gifts") OR die(mysql_error());
    
    while ($gift = mysql_fetch_array($query))
    {
        echo $gift['name'];
        echo $gift['image'];
    }
    
    PHP:
     
    nico_swd, Oct 11, 2007 IP
  12. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #12
    You have been a big help so far !!! -- Thanks for your time.



     
    micromark, Oct 11, 2007 IP
  13. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Ok so i have inserted that, but it shows nothing...is this ok....I think im going to have to pay someone to do this...


    
    
    <?php
    
    $query = mysql_query("SELECT * FROM gifts") OR die(mysql_error());while ($gift = mysql_fetch_array($query)){    echo $gift['name'];    echo $gift['image'];}
    
    ?>
    
    
    PHP:
     
    micromark, Oct 11, 2007 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    Why did you put the whole code into a single line?

    And the field names are case-sensitive. In $gift['name'], the "name" has to be exactly the same as in your database field.
     
    nico_swd, Oct 11, 2007 IP
  15. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Fixed it....OK thanks, that is displaying the image.

    NOW....Do i make a form that will post this into the second table --Gift log.
     
    micromark, Oct 11, 2007 IP
  16. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #16
    nico_swd : I can now add to my database thanks. First part is done.


    Next part: This is the code im using to view the 'gifts' the members have been sent.

    However it displays an error.

    Any suggestions.


    
    
    <?php
    
    $query = "SELECT giftlog.name, giftlog.image".
    
    "FROM giftlog".
    
    "LEFT JOIN
    
    	gifts"
    
    "USING
    
    	(giftid)"
    
    "WHERE  giftlog.recipientid = " . $userid";
    
    ?>
    
    
    PHP:
     
    micromark, Oct 12, 2007 IP
  17. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #17
    There are no spaces where you split the string (why are you doing that anyway?)

    
    $query = "
        SELECT
            giftlog.name,
            giftlog.image
        FROM
            giftlog
        LEFT JOIN
            gifts
        USING (giftid)
        WHERE
            giftlog.recipientid = " . intval($userid);
    
    PHP:
    And also, for future reference, please always include the error messages to be able to give you more precise help.
     
    nico_swd, Oct 12, 2007 IP
  18. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Hi,

    Ok no error message now, just a blank screen -which is what is supposed to happen if there is not gift.

    No there is a problem i have added a gift to the tables but it does not show in the area where the code is ?

    Do i need to get it to display a result ?
     
    micromark, Oct 12, 2007 IP
  19. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #19
    Post the code you're currently using. Not only the query string.
     
    nico_swd, Oct 12, 2007 IP
  20. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #20
    FORM

    
    
    <form action="gift.php" method="post">
    <input type="text" name="giftid" value="$name">
    <input type="text" name="recipientid" value="$recipientid">
    <input type="text" name="userid" value="$userid">
    <input type="submit" name="Submit" value="Submit" class="mybutton">
    </form>
    
    
    HTML:
    (action=)gift.php

    
    
    <?
    require ("config.php");
    include("functions.php");
    
    
    
    // Insert a row of information into the table "giftlog"
    mysql_query("INSERT INTO giftlog
    (Giftid, Recipientid,Fromid) VALUES('$giftid', '$recipientid', '$userid' ) ")
    or die(mysql_error());
    
    
    
    echo "Data Inserted!";
    
    ?>
    
    PHP:

    View posted gifts


    
    
    
    <?php
    $query = "
        SELECT
            giftlog.name,
            giftlog.image
        FROM
            giftlog
        LEFT JOIN
            gifts
        USING (giftid)
        WHERE
            giftlog.recipientid = " . intval($userid);
                    ?>
    
    
    
    PHP:
    [/PHP]
     
    micromark, Oct 12, 2007 IP