SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member moneybyus's Avatar
    Join Date
    Mar 2001
    Location
    MN
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I run this script it always uses method 3, but I want it to only use that method if a number is less than 10. It just keeps adding up even after 10. Can anyone tell me whats wrong, I know it sound confusing. It cxalled when I enter a url like,

    /enter2.php?contest=1&ban=271&user=114

    Here is the part of the code that isnt working.

    $date = date("Y-m-d");
    $result = mysql_query("Select lastentry, con1, con2, con3, con4, con5 from entrylimit where id = '$user'");
    $array = mysql_fetch_array($result);
    // method 2
    if (mysql_result($result,0,0)>0 and $array{'lastentry'} == $date and $array{'con$contest'} >= 10) {
    $action = "max";
    $method = "2";
    }
    // method 3
    elseif (mysql_result($result,0,0)>0 and $array['lastentry'] == $date and $array['con$contest'] < 10) {
    mysql_query("UPDATE count SET entries=entries+1 where id = '$contest'");
    mysql_query("UPDATE banners SET clicks=clicks+1 where num = '$ban'");
    mysql_query("UPDATE userinfo SET con$contest=con$contest+1, last_entry='$date' where id = '$user'");
    mysql_query("UPDATE entrylimit SET con$contest=con$contest+1 where id = '$user'");
    $action = "done";
    $method = "3";
    }
    // method 4
    elseif (mysql_result($result,0,0)>0 and $array['lastentry'] != $date) {
    mysql_query("UPDATE count SET entries=entries+1 where id = '$contest'");
    mysql_query("UPDATE banners SET clicks=clicks+1 where num = '$ban'");
    mysql_query("UPDATE userinfo SET con$contest=con$contest+1, last_entry='$date' where id = '$user'");
    mysql_query("UPDATE entrylimit SET con$contest='1', lastentry='$date' where id = '$user'");
    $action = "done";
    $method = "4";
    }
    // method 1
    else { // add id if it doesnt exist.
    $sql = "INSERT INTO entrylimit SET id = '$user', lastentry = '$date', con$contest = '1'";
    if (!mysql_query($sql))
    error("A database error occurred in processing your ".
    "submission.\\nIf this error persists, please ".
    "contact support@moneybyus.com.");
    mysql_query("UPDATE count SET entries=entries+1 where id = '$contest'");
    mysql_query("UPDATE banners SET clicks=clicks+1 where num = '$ban'");
    mysql_query("UPDATE userinfo SET con$contest=con$contest+1, last_entry='$date' where id = '$user'");
    $action = "done";
    $method = "1";

    }

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Why are you using this:

    $array = mysql_fetch_array($result);

    and then this:

    mysql_result($result,0,0)

    $array[0] is the same as mysql_result($result,0,0), which is the same as $array[lastentry]. The array returned by mysql_fetch_array() is ordered the same as the fields in your SELECT statement. You can refer to them by $array[0], $array[1], $array[2], etc., or you can refer to them by $array[con1], $array[con4], $array[con2]; but it is easier to use the fieldnames as the array keys because then you don't have to figure out what position they occupy in the array, and it is better practice because it makes it easier to follow what your code is doing: if you use $array[5], then to know what field that represents you have to go to your select statement and count the 6th field, but if you use $array[con5] then the reader knows immediately.

    Also, what is this: $array{'lastentry'}? That is going to evaluate to $arraylastentry which is a variable that doesn't exist, so it will be 0 by default.

    Finally, you are not understanding the difference between single quotes and double quotes. When something is enclosed in double quotes, you are telling php to evaluate the variables in the string, and when you enclose something in single quotes, you are telling php not to evaluate the string. Try this simple example and see what your output is:

    <?php
    $test = 'hello';
    echo '$test world!';
    ?>

    So, let's see what happens when you use this:

    $array['con$contest']

    Since php does not evaluate what's between single quotes, it looks for the key in your array that is literally: con$contest . You have no key con$contest for your array, and the value for any variable you use that has not been assigned is 0. However, you do have an array value for the key con1, but for the string con$contest to evaluate to con1 you need double quotes like this:

    $array["con$contest"]
    Last edited by 7stud; May 5, 2001 at 03:17.


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
  •