SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot BuyAnswersLinks's Avatar
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    <-- newb needs help on if statement

    Hello,

    Im working on a discount script for my site. I almost got it but a few things. You can see the script in work here www.buyblogcomments.com/test.php

    The discount codes are:
    discount
    ten
    testy

    The discount and testy work, but ten is turned off using true of false.

    The code is:

    PHP Code:
    <center>
    <form action="http://www.buyblogcomments.com/test.php" method="post">
    <p><strong>Amount Of Comments?</strong> <input type="text" name="comments" maxlength="10" size="10" class="form-text" /></p>

    <p><strong>Discount Code?</strong> <input type="text" name="discountcode" size="20" maxlength="100" class="form-text" /></p>

    <p><input type="submit" value="Find Price" class="button1"></p>
    </form>
    </center>

    <?php

    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

    // include this to grab the code off the db
    include('/Out/user-config.php');

    $result mysql_query("SELECT `code`,`expire`,`amount` FROM `OUT` WHERE `expire`='T'") or die(mysql_error());
    while(
    $row=mysql_fetch_array($result)) 
       {

    // grab the info from the db
    $code $row["code"];
    $expire $row["expire"];
    $amount $row["amount"];

    // calculate the total price
    $commentstimes $_POST['comments'];
    $commentamount .24 $commentstimes;

    // grab the discountcode from html
    $discountcode $_POST['discountcode'];

    // get the % and the total
    $percent "$amount";
    $total "$commentamount";

    // Calculate % from $total
    $discount_value = ($total 100) * $percent;

    $final_price $total $discount_value;

    if(
    $row["code"] == "$discountcode") {

    echo 
    "Price: $".$total."<br />Our special discount: <strong>".
    $percent."%</strong> (-$".$discount_value.")<br />Final price: $".$final_price;

    echo 
    "<br /> Redirecting you to the order page now...";

    } else {

    echo 
    "Wrong discount code";

    }

    }
    }

    ini_set('error_reporting'E_ALL);
    ?>
    If you type in "discount" you will see that it says "Wrong discount code", when it actually does the if statement. But if you type in "ten" it works fine.

    And when you eneter a wrong discount code, it echos "Wrong discount codeWrong discount code". Which it shouild only echo it once.

    Im a newb at php so please let me know what you think

  2. #2
    SitePoint Addict Phidev's Avatar
    Join Date
    Oct 2008
    Location
    Texas
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    remove the quotes from your comparison, they are not necessary here.

    if($row["code"] == $discountcode)

    also, it works well for me with "discount" and "ten" but it doest work with "testy"

    Can we see what the database is returning?

  3. #3
    SitePoint Zealot BuyAnswersLinks's Avatar
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I will remove the quotes thanks.

    Also, they work fine, but it displays the "Wrong discount code" when you use "discount" but not the "ten".

    What info do you want from the db exactly? the rows or do you want me to run something in myphpadmin?

    Oh sorry its "testy" that is turned off. The other 2 will work.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql "SELECT `code`,`expire`,`amount` FROM `OUT` WHERE `expire`='T' and code='" mysql_real_escape_string($_POST['discountcode']) . "'";
    $result mysql_query() or die(mysql_error());
    $num_records mysql_num_rows($result);
    if (
    $num_records 0) {
        echo 
    "found $num_records matches\n";
        
    $row mysql_fetch_array($result);
        
    // process it...
    } else {
        
    // code no exist in db


  5. #5
    SitePoint Addict Phidev's Avatar
    Join Date
    Oct 2008
    Location
    Texas
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, show me what the query is returning, please;

    and yes, the only time when I see "wrong discount code; wrong discount code" is when I type "testy" as the discount code;

  6. #6
    SitePoint Zealot BuyAnswersLinks's Avatar
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    PHP Code:
    $sql "SELECT `code`,`expire`,`amount` FROM `OUT` WHERE `expire`='T' and code='" mysql_real_escape_string($_POST['discountcode']) . "'";
    $result mysql_query() or die(mysql_error());
    $num_records mysql_num_rows($result);
    if (
    $num_records 0) {
        echo 
    "found $num_records matches\n";
        
    $row mysql_fetch_array($result);
        
    // process it...
    } else {
        
    // code no exist in db


    I tryed it and its saying "Warning: Wrong parameter count for mysql_query() in /OUT/test2.php on line 20"

    I am looking into that error now...

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    feed it the $sql variable.

  8. #8
    SitePoint Zealot BuyAnswersLinks's Avatar
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    feed it the $sql variable.
    For:
    SELECT `code`,`expire`,`amount` FROM `out` WHERE `expire`='T' and `code`='discount'

    It showed:
    Showing rows 0 - 0 (1 total, Query took 0.0001 sec)

    my db has these rows:

    id
    code
    expire
    amount
    site (which im not using at all so its NULL)

    is that what you wanted me to execute?

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_query($sql)

  10. #10
    SitePoint Zealot BuyAnswersLinks's Avatar
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did that as a php file and the page came up blank

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All i did was show you the proper way to see if a match exists in the database. You need to write code to finish it.

  12. #12
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    That all-year round sunny Singapore
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT `code`,`expire`,`amount` FROM `out` WHERE `expire`='T' and `code`='discount'

    Doesn't this pull expired discount codes instead?

    And to why there's a "Wrong discount codeWrong discount code" output, it's because you're looping the code through *each row* found by the query (probably 2 rows), so the if/else statement is executed multiple times. Why do you need the while loop in the first place? Just use something straightforward like:

    ...
    $result = mysql_query('SELECT code, expire, amount FROM OUT WHERE code = \'' . mysql_real_escape_string($_POST['discountcode']) . '\' LIMIT 1');
    $row = mysql_fetch_array($result);
    if ($row) {
    // insert whatever you want to do with $row here
    } else {
    echo 'Error: Invalid discount code';
    }
    ...

  13. #13
    SitePoint Zealot BuyAnswersLinks's Avatar
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by LatecomerX View Post
    SELECT `code`,`expire`,`amount` FROM `out` WHERE `expire`='T' and `code`='discount'

    Doesn't this pull expired discount codes instead?

    And to why there's a "Wrong discount codeWrong discount code" output, it's because you're looping the code through *each row* found by the query (probably 2 rows), so the if/else statement is executed multiple times. Why do you need the while loop in the first place? Just use something straightforward like:

    ...
    $result = mysql_query('SELECT code, expire, amount FROM OUT WHERE code = \'' . mysql_real_escape_string($_POST['discountcode']) . '\' LIMIT 1');
    $row = mysql_fetch_array($result);
    if ($row) {
    // insert whatever you want to do with $row here
    } else {
    echo 'Error: Invalid discount code';
    }
    ...
    works great. But when I add the expire part `expire`='T' it says:
    syntax error, unexpected T_STRING

    I will try and fix that though, thanks for your help.

    EDIT: I got it working by doing this - WHERE `expire`=\'T\' AND code =


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
  •