42 users online (1 members and 41 guests)  


  Results 1 to 4 of 4

Related

  1. Replies: 2
  2. What is wrong with it??    Forum: Show it off!
    Replies: 1
  3. What's wrong with this?    Forum: HTML Forum
    Replies: 5
  4. what am i doing wrong....?    Forum: HTML Forum
    Replies: 1
  1. #1
    wudz's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2005
    Posts
    1

    Script, logic or syntax wrong.....or have I got it all wrong

    Hi,
    I am a newbie to PHP and I have a problem with the script below. I do not get errors but I do not get a reduction in credit of at least 1 being the minimum. I think that the $qtity is always 0 in the last line why, is it my syntax. .
    If I put credit=(credit-1) all works well.. ...All help would be greatfully received with an explaination of my error/s.
    $qtity = $items_sold;
    if ($qtity < 1) {
    $qtity = 1;
    }
    else { $qtity = $items_sold;
    }
    //---Reduce credit of Seller
    mysql_query('UPDATE PHPAUCTION_users SET credit=(credit-$qtity) WHERE id = '.$Seller['id']);
    Many thanks in anticipation
    John


  2. #2
    vinyl-junkie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    721

    Re: Script, logic or syntax wrong.....or have I got it all wrong

    Welcome to the forum, wudz. We're glad you decided to join us.

    First, a point of picky-ness. You need to learn to use the [ code][/code] tags when postings, as it is much easier to read.

    Second, you might find this useful in showing you what is going on:
    Code:
    $qtity = $items_sold;
    if ($qtity < 1) {
    $qtity = 1;
    }
    else { $qtity = $items_sold;
    }
    //---Reduce credit of Seller
    $sql = 'UPDATE PHPAUCTION_users SET credit=(credit-$qtity) WHERE id = '.$Seller['id'];
    echo "sql = ".$sql;
    mysql_query($sql);
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more

  3. #3
    ftenadmin's Avatar
    New User

    Status
    Offline
    Join Date
    May 2006
    Posts
    1

    Arrow Re: Script, logic or syntax wrong.....or have I got it all wrong

    perhaps you need to quote out to get the varibable to pass properly..
    mysql_query('UPDATE PHPAUCTION_users SET credit=(credit-'.$qtity.') WHERE id = '.$Seller['id']);
    or try this...
    mysql_query('UPDATE PHPAUCTION_users SET credit=credit-$qtity WHERE id = $Seller[id]');
    or mabey use the object to fetch the sellers id like this
    $sellers = mysql_result("select * from PHPAUCTION_users");
    while ($seller=mysql_fetch_object($sellers)) {
    //some code here balh blah blah
    mysql_query("UPDATE PHPAUCTION_users SET credit=credit-$qtity WHERE id=$seller->id");
    Last edited by ftenadmin; 05-03-2006 at 05:40 AM.

  4. #4
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: Script, logic or syntax wrong.....or have I got it all wrong

    PHP Code:
    $qtity $items_sold;
    if (
    $qtity 1) {
    $qtity 1;
    }
    else { 
    $qtity $items_sold;
    }
    //---Reduce credit of Seller
    mysql_query('UPDATE PHPAUCTION_users SET credit=(credit-$qtity) WHERE id = '.$Seller['id']); 
    Two things confuse me about this bit of code:
    Firstly: why the
    PHP Code:
    else { $qtity $items_sold;

    $qtity is already == $items_sold.

    Secondly, if $qtity==1 and $Seller['id']=="DeadMeatGF" your query will com out as:
    Code:
    UPDATE PHPAUCTION_users SET credit=(credit-1) WHERE id = DeadMeatGF
    Which means that the WHERE clause is looking for the column "id" to equal the column "DeadMeatGF" rather than the value "DeadMeatGF"
    You need to use:
    Hold on - just realised as I was typing - I think you need to doublequote the string - the single quotes can cause the PHP parser to use a literal string instead of substituting variables with their values!
    PHP Code:
    mysql_query("UPDATE PHPAUCTION_users SET credit=(credit-$qtity) WHERE id = '".$Seller['id']."'"); 
    Note the horrible-looking multiple-quotes bits, you need to get them right.

    Try that - I'm hopeful it will work

    So - to answer your question: I think "Syntax"

    *edit*
    As an afterthought - I always build the query in a separate string, then you can debug more easily ...
    PHP Code:
    $qtity $items_sold;
    if (
    $qtity 1) {
    $qtity 1;
    }

    //---Reduce credit of Seller
    $query "UPDATE PHPAUCTION_users SET credit=(credit-$qtity) WHERE id = '".$Seller['id']."'";
    //---Debug output to check query is as we expect
    echo "$query<br />";
    //---Uncomment query once we know the correct value is being passed
    //mysql_query($query);
    ... 
    Which I think is what Vinyl-Junkie was getting at.
    Last edited by DeadMeatGF; 05-03-2006 at 06:13 AM.



Tags for this Thread