Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2012
    Posts
    10

    Unanswered: PHP check for !result not working after query

    Hi there.

    I just wrote this code that is meant to check if a row exists. If it doesn't then it creates it. If it already does, then it simply updates it.

    First of all, I'm not even sure if this is how its supposed to be done, or if there is a more efficient, quicker, and easier way to do this.

    Secondly, here are my results:

    556Test Lab removed from building queue, and Test Lab added to finished building table
    The new amount is 1
    55 is my user id and 6 is the str_id. So they are going through.

    When I run the query within myphpadmin, it returns no results. Thus, I assume, based on the code I wrote, that it should attempt to add a new row, though it doesn't. It goes straight to the else and runs that code.

    And to make things even better! The "else" code doesn't properly update the database either.

    Here is my code:

    Code:
    echo $user_id;
    echo $str_id;
    $query = "SELECT amount FROM user_structures WHERE user_id=$user_id AND str_id=$str_id";
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    
    if (!$result) {
    $query = "INSERT INTO user_structures (str_id, user_id, amount, numactive) VALUES ('$str_id', '$user_id', '1', '0')";
    mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    } else { 
    $amount = $amount[0] + 1;
    $query = "UPDATE user_structures SET amount='$amount' WHERE user_id=$user_id AND str_id=$str_id";
    mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    echo "$str_name removed from building queue, and $str_name added to finished building table<br>";
    echo "The new amount is $amount";
    }
    ?>
    Any help would be appreciated. I will take a look at this forum and the code tomorrow and see if anything clicks.

    Thanks

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    well as its php question, its probably best asked in the PHP part of this site
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Jan 2012
    Posts
    10
    solved it.

    stupidly never took the first $result and array-i-fied it.

Posting Permissions

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