SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If else question

    Hi all,

    Having trouble with the if else part of this script
    Here is a dump of the table.

    (
    uid int(10) NOT NULL default '0',
    id int(5) NOT NULL default '0',
    name varchar(50) NOT NULL default '',
    image varchar(100) NOT NULL default '',
    quantity int(10) NOT NULL default '1',
    PRIMARY KEY (id),
    KEY uid (uid)
    ) TYPE=MyISAM;


    PHP Code:
    $db="SELECT  uid FROM {$config["prefix"]}_users WHERE uid={$session["uid"]}";
    $ret mysql_query($db);
    while(list(
    $db)=mysql_fetch_row($ret))
    $user $db;



    $query "SELECT * FROM {$config["prefix"]}_items where id = '$id'";
    $result mysql_query($query);
    $row mysql_fetch_array($result);
    $name $row[name];
    $pid $row[image];
    $itemid $row[id];
                                                                                    
    if(
    $itemid){

    $query "INSERT INTO {$config["prefix"]}_my_items (uid, id, name, image) VALUES ('$user', '$itemid', '$name','$pid')";
    $ret mysql_query($query)
                                                                        or die(
    "Unable to insert into the database");
                    }else{                                                    
    $query_update ="UPDATE {$config["prefix"]}_my_items SET quantity = quantity + 1 WHERE id = '$id'";
    $result=mysql_query($query_update);

    This is the second way I hav tried it. Here is the first:

    PHP Code:
    $db="SELECT  uid FROM {$config["prefix"]}_users WHERE uid={$session["uid"]}";
    $ret mysql_query($db);
    while(list(
    $db)=mysql_fetch_row($ret))
    $user $db;



    $query "SELECT * FROM {$config["prefix"]}_items where id = '$id'";
    $result mysql_query($query);
    $row mysql_fetch_array($result);
    $name $row[name];
    $pid $row[image];
    $itemid $row[id];
                    if(
    $itemid == '$id'){
    $query_update ="UPDATE {$config["prefix"]}_my_items SET quantity = quantity + 1 WHERE id = '$id'";
    $result=mysql_query($query_update);
                    }else{                                                                

    $query "INSERT INTO {$config["prefix"]}_my_items (uid, id, name, image) VALUES ('$user', '$itemid', '$name','$pid')";
    $ret mysql_query($query)
                                                                        or die(
    "Unable to insert into the database");

    What I need to do is update the quantity if id is already there and if it isn't then insert the info into the db. Can someone point out what I have done wrong and help me fix it?

    Thanks
    Jennifer.
    Wanna be php coder!

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "SELECT * FROM {$config["prefix"]}_items where id = '$id'";
    $result mysql_query($query);
    if (
    mysql_num_rows($result) == 0)
    {
    // id doesn't exist, insert
    }
    else
    {
    // id existed, go fetch array - update


  3. #3
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you johnn it worked like a charm!
    Wanna be php coder!

  4. #4
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a slight bug and am wondering if someone could help.

    If another user logs in and buys the same item as I do then my item is updated instead of adding the second user to the db and viseversa if they buy a different item.

    I have tried elseif with the uid but I am getting no where fast does anyon have any ideas?

    Thanks
    Jennifer
    Wanna be php coder!


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
  •