Jump to content

endouken

Members
  • Posts

    48
  • Joined

  • Last visited

    Never

Posts posted by endouken

  1. MySQL can't, but PHP can: strtotime() or just store the duration as seconds that way if you calculate the TIMESTAMP of a date (or just stored the TIMESTAMP) you can just add it.

     

    $enddate = time() + 86400; // 86400 = 1 day

     

    That's not a great idea if i read it correctly - different months have different seconds, as do years.  May seem punitive, but when a whole companies membership is out by a day it will be a very real problem.

     

    What seems to be being looked over in the "store a start and end date" response, is to calculate the end date i need to know the interval between the two, that interval needs be assumed somewhere - and all assumptions should be made in the database, this way a different value can be put in to the database and the php spits out a new answer without any modification; this is database design 101.

     

    How can i store "+1 year" in my database - would a varchar work?  Then i can do (e.g.) $interval = GET duration from table products where productid = $productid and use that in the php date + $interval rather than date +1 year...

     

    Thanks for everyones replies so far, i did have a convo with Pikachu about this, which shed some light - but i'm always keen to debate and pick more brains!

     

    Tom.

  2. Hey guys & gals,

     

    i have the following code:

    <?php
    $date= $_SESSION['date'];
    echo $date;
    ?>

     

    Which is an echo of an earlier page:

    <?php $_SESSION['date'] = date("c")?>;

     

    How do i format the echo of the first quote code? At present it echo's like so...

    2011-03-30T15:43:43+01:00

     

    ...which although is useful for back office purposes, not so easy on the eye for the customer.  How can i format so it's like DATE-MONTH-YEAR?  I have googled this and there are examples, but none for when echoing a $_SESSION which is where i'm falling flat on my face.

     

    I tried this: but it doesn't work :-(

    echo $startdate ("F j, Y")

     

    Thanks in advance to all who read and reply :)

     

     

  3. I'm not trying to be rude, nor did I perceive you as rude, even if my response seemed rather curt. But you did get the correct response as to how to handle the problem in that thread; you don't need to store a duration, you need to store an end date. After that, your response went right back to wanting to store a duration in the database. Doing so forces a calculation to be made every time you need to check if something has expired, rather than performing a simple comparison.

     

    You said the expiration date would either be a month or a year, so what you really need to do is use MySQL's DATE_ADD() function, using a variable to store the interval, when inserting the record to calculate and store the expiration date. If there's the possibility that it may be a renewal of something that hasn't yet expired, you first query to see if the product is already purchased, and if so has it expired previously? If it has expired, update the expiration with the new expiration date based on CURDATE(), if it hasn't expired, update it by adding the interval to the expiration date in the record.

     

    Perfect - thanks! 

     

    I believe i miss-understood (down to lack of experience i admit - been doing php for 1-2 weeks now)  The Little Guy's response - but your explanation goes further, far enough for me to grasp what you (and probably he originally) meant. In which case I am happy to admit you were partially correct - i did receive the response required, i just didn't realize it!

     

    So i've currently got a start and end date in my database, but i have "+1 year" in my php code.  You mentioned using a variable to store the interval, could i just:

    - store a varchar of "+ 1 year" in a 'duration' column in my db, then

    - get this in my php code (pseudo: $duration = select duration from products where productid = $productid )

    - make the end date calculation (pseudo: strtotime(date("F j, Y", strtotime($todayDate)) . "+$duration");

    - and then insert it into the purchasehistory table.

     

    That way all the assumptions are in the database, or is this method unsuitable? Is there another (better) method of storing the +1 year variable? Have i got completely the wrong end of the stick?!

     

    Really appreciate you taking the time to reply - an honest thanks  :)

     

    Tom.

  4. You got the correct responses; even if they weren't what you wanted to hear.

     

    I got two responses - one from optikalefx who suggest what i've done - but this is not the correct/best way of calculating memberships based products for the reason I've stated above.  The other response from The Little Guy suggested i use the MySQL date_sub() - i responded asking a follow up question as it seemed to me the end date was still being calculated using an assumption in php rather than an assumption in mysql.  I received no reply back from The Little Guy so ergo i got no satisfactory response. 

     

    There are two satisfactory responses: 1) confirmation that mysql cannot store a duration, or 2) confirmation that mysql can store a duration along with the method. 

     

    "What i want to hear" bears absolutely no relevance to my perception of a satisfactory answer.  If you look back over that thread, you will see neither 1) or 2) were garnered and instead i was (i believe) presented with two php based methods, which although leans towards conclusion #1 does not confirm it.

     

    Perhaps if you know the answer, you could let me know as i realize you have much more experience than I or even most on here?

     

    Also, i would like to point out i have been polite and courteous to all on here, despite the anonymity that internet forums provide.  I realize you probably speak to many irritating and rude people on here, but I hope you do not judge me as one of these based on a my brief posting history.  Maybe i need to post more to earn your respect, but that frankly seems like floored logic when deciding who you are and are not polite to. 

     

    I did appreciate your earlier response however and my thanks to you was, and still is, genuine.

     

    Rgrds,

  5. Fixed!  8)

     

    I commented out the line

     

    "echo $form_block;"

     

    and redirected back to the form using a header command at line 64

     

    header ('Location: http://www.mysite');

     

    And all seems to be working.  Is what I've done OK?

     

    Thanks again for your help - let me know that email address  :intoxicated:

     

    Tom.

     

    PS - don't suppose you know the answer to "what mysql datatype is best for storing intervals" - i asked this in the mysql section of phpfreaks and didn't get any satisfactory responses.  Currently I calculate the renewal date of the product in php, but if i want to add a different product with a different renewal date i would need to start messing around with php code - where ideally i want to reference todays date and add a duration to it which is assumed in the productdetails table...but it looks like mysql can only store dates, not a duration of time.  And as there are different seconds in each month/year i can't use the integer datatype...

  6. Okay I created a test database based on what you were using, and tested the code to make sure it worked and I had no issues. I've also setup this code to start each uniquecode with 2 uses. If you wished to start each uniquecode you'd have to modify the code to change the IF statment to the following:

     

       if ($uses >= 0 && $uses != 2) {

     

    And then you'd have to change the update to the database to add uses so you'd change it to this

     

    $subtractuse just change to $adduse so you can keep track of whats going on.

     

    $adduse = mysql_query("UPDATE discounttable SET uses=(uses+'1') WHERE uniquecode = '".$uniquecode."'");

     

    I've also added error checking for the following:

     

    Make sure the user entered a code and didn't just click submit, if not notify the user.

    Make sure the user has entered a valid code, if not notify the user.

    Make sure the user hasn't used all of their uses, if they have notify the user.

     

    So here is your super duper kick butt discount engine: (I also attached it to my post so you can download it, selecting code has issues with keeping formatting.)

     

    ( I'll also take that drink now! LMAO )

     

    <?php
    // Some error reporting
       error_reporting(E_ALL);
    
    // Start our session
       session_start();
    
    // Database variables
       $localhost = "00.000.000.00";
       $username = "###";
       $password = "###";
       $database = "mfirst";
    
    // Establish a connection to the mfirst database
       $link = mysql_connect($localhost, $username, $password);
       if (!$link) {
            die('Cound not connect: ' . mysql_error());
       }
    
    // Select the database we want to work with
       @mysql_select_db($database, $link) or die( "Unabled to select $database database");
    
    // Echo our HTML form for user submission
    
       echo $form_block;
    
    // Let's start working with what happens when a user enters a code and clicks submit!
    
       if (isset($_POST['submit'])) {
    
    // Clean up the user inputted code to make it safe
       $uniquecode = mysql_real_escape_string($_POST['discountcode']);
    
    // Let's check to make sure unique code isn't empty!
    
       if (!empty($uniquecode)) {
    
    // Let's query the database to get the unique code, uses, and the discount amount.
    
       $codeinfo = "SELECT uniquecode, discount, uses FROM discounttable WHERE uniquecode = '".$uniquecode."'";
       $result = mysql_query($codeinfo) or die( "Couldn't get the requested information" . mysql_error());
    
    // Let's get the information returned from our $codeinfo query and place it into an array
    
       $row = mysql_fetch_array($result, MYSQL_ASSOC); // $row is now the array that will hold our data for the given code.
    
    // Make some variables to hold the information we want to keep things organized
    
       $dcode = $row['uniquecode'];
       $discountamt = $row['discount'];
       $uses = $row['uses'];
    
    // Let's check to make sure the code entered has been found (Rows returned will be 1)
    
      if (mysql_num_rows($result) == '1') {
    
    // A code was found so now let's find out if they have any uses remaining
    // Let's make sure uses isn't 0 and that they have 2 or fewer uses left
    
      if ($uses != 0 && $uses <= 2) {
       $discount = ($_SESSION['sessionprice'] - $discountamt); // Discount math to well subtract the discount from the end users cost
       $subtractuse = mysql_query("UPDATE discounttable SET uses=(uses-'1') WHERE uniquecode = '".$uniquecode."'");
       $_SESSION['sessionprice'] = $discount; // Make sure the price for the session reflects the total discount applied
    
          // They have used all of their uses so let's tell the end user
         ?><script type="text/javascript">
              alert("You used discount code <?php echo "$dcode"; ?> and saved <?php echo "$discountamt"; ?> . Your new total is <?php echo "$startprice"; ?> !");
         history.back();
         </script><?php
    
       } else {  
    
          // They have used all of their uses so let's tell the end user
         ?><script type="text/javascript">
              alert("You have used that discount code <?php echo "$dcode"; ?> twice and may no longer use it.");
         history.back();
         </script><?php
    
         } // Ends the uses check
    
       } else {
    
         // The code the user entered wasn't found
         ?><script type="text/javascript">
              alert("Sorry but you've entered an invalid code.");
         history.back();
         </script><?php
    
        } // Ends the code check
    
       } else {
         // The user didn't enter a code
         ?><script type="text/javascript">
              alert("You must enter a discount code in order to apply a discount.");
         history.back();
         </script><?php
    
        } // Ends the empty post code
    
      } // Ends the submit routine
    
    // Close our database connection since we no longer need it.
    mysql_close($link);
    
    ?>

     

    I will happily donate some money for a few drinks to a PayPal account - no jokes, you've been of unparalleled help - just let me know the email address :)  (That's not against forum rules is it...?)

     

    This is working, but is throwing up a minor bug.  The below error message is displayed...

     

    Notice: Undefined variable: form_block on line 25

     

    ...triggered by:

    - Entering a valid code - the screen changes to this error message (at mysite/discoutncode.php) and stops.  Hitting the back button returns to the previous page with the price changed successfully. So it works, but with an odd error message.

    - Enter an invalid code - the java error message displays and as soon as OK is clicked the website flashes to the above error page before going straight back to the previous page automatically with the price unchanged.  So it works, but there's an odd flash of that error message.

    - Enter a code with 0 uses left - same as above: entering an invalid code.

    - Blank code - same as above: entering an invalid code.

     

    Worth noting a different/the correct java message is shown for 2, 3 and 4.

     

    Is it safe to just delete this line as it's undefined - or is there a bit missing from the code?

     

    Thanks!

     

    Tom.

     

  7. if (($dcode = $uniquecode) && . . . 

     

    Needs to be

    if (($dcode == $uniquecode) && . . . 

     

    Thanks for replying Pikachu.

     

    Unfortunately that's not had any impact.  It's as if the code is simply ignoring this element of the code, and processing the discount regardless of whether the 'uses' remaining is 0 or 2...

  8. That's because the code I gave you was generic and not all out super kick butt LOL. In reference to the Java error pop up. The if statement has to check uses so it knows they must be over 0 but under 2. That's why I did <= 2 (less than or equal to 2) and to prevent negative numbers I did != 0 (does not equal 0). Do you see my logic? I will be happy to do a kick butt super one when I get home in 10hrs so 11pm or later EST.

     

    Phone typing slow LOL

     

    Heya.

     

    If you could help with a super kick butt one that would be awesome - i'm busy with the rest of the shopping cart process which i must admit is a hell of a lot easier than the discount engine you wrote! :P

     

    Chat laterz

     

    Tom.

  9. Cant edit my above post,

     

    Might be worth mentioning that i've also noticed the following - which may help diagnose the problem:

     

    - When the input box is blank the java error message is displayed correctly on submit, however when an incorrect code is entered the error message is not displayed - it runs the scripts and the price doesn't change (so it works OK....but it doesn't show the 'if false' java error response other than on a blank submit)

     

    - i tried flipping the < and > signs to see what error it would throw up...however using the below code, both codes were accepted regardless of which < or > sign was used...

     

    if (($dcode = $uniquecode) && ($uses <= 0)) {

     

    Perhaps food for thought...

     

  10. Heya,

     

    I don't know how to respond to that - other than i wish him and you well.

     

    Apologies i fell asleep last night - 4:30am over here when i sent my last message 

     

    Still not working - just rejects both codes.  I can re-assure you the mysql side of it is set up correctly, i've phpmyadmin as well as mysql workbench which both confirm the values for the two codes uses are 0 and 2 respectively.

     

    I look forward to hearing from you in due course, thank you for your help and patience.

     

    Tom.

  11. Hmmm, it's still rejecting both codes (still with one code with 2 uses, the other has 0).

     

    It was working 50% before, subtracting -1 from the uses column - but it was also accepting a code with 0/zero uses remaining and then subtracting -1 so the codes remaining uses ran into negatives.  Code we were using when this was happening was:

     

    if (($dcode = $uniquecode) && ($uses <= 2)) {

  12. Made the changes to php and the database, and now it's rejecting both codes (one code has 2 uses, the other has 0).

     

    Where you say:

    if (($dcode = $uniquecode) && ($uses != 0)) {

    We will now check to make sure that they have 2 uses by saying they start with 2 uses for each code used by saying uses can't be 0. (You'll have to set the uses to 2 in the database before testing again, so clean up any negative numbers and numbers over 2.) It was working because it was rejecting a user from using a code.

     

    What does the exclamation mark mean in $uses != 0, and where did we say in php they have 2 uses - or did you simply mean via mysql we would specify this?

     

    Appreciate the perseverance and the tutorship!

     

    Tom.

  13. I'm working with chunks of the code from our different posts, do me a favor and post our fully updated new code to make it easier :)

     

    <?php
    session_start();
    $localhost="00.000.000.00";
    $username="###";
    $password="###";
    $database="mfirst";  
    
    mysql_connect($localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    // Clean up the code the user entered
    $uniquecode = mysql_real_escape_string($_POST['discountcode']); // OR whatever the name of the input is
    
    $discountcodecheck = mysql_query("SELECT uniquecode, discount FROM discounttable WHERE uniquecode = '".$uniquecode."'");
    $row = mysql_fetch_array($discountcodecheck,  MYSQL_BOTH); // MYSQL_BOTH incase it's alphanumeric, and establish the $row array with the codes
    
    $dcode = $row['uniquecode'];
    
    $remainingusescheck = mysql_query("SELECT uses FROM discounttable WHERE uniquecode = '".$uniquecode."'");
    
    $row1 = mysql_fetch_array($remainingusescheck, MYSQL_NUM); // MYSQL_NUM since the uses should be a number value, establish $row1 array with the uses
    
    $uses = $row1['uses'];
    
        // Check the code vr the code the user entered, and let's make sure the user hasn't used it over 2 uses
        if (($dcode = $uniquecode) && ($uses <= 0)) {
    
         // Since they check out we can do our discount
       $discount = ($_SESSION['sessionprice'] - $row['discount']);
       $adduse = mysql_query("UPDATE discounttable SET uses=uses-1 WHERE uniquecode = '".$uniquecode."'");
       $_SESSION['sessionprice'] = $discount;
       } else {
    
        // Let them know the code or the uses have been reached, you could seperate the if statement above to give an error for uses
        		?><script type="text/javascript">
    		alert("Discount Code Entered Is Either Not Valid Or Has Been Previously Used.");
            	history.back();
          		</script><?php
    
      } // close our if for checking the code and the uses
    
    mysql_close();
    ?>

     

  14. Your most welcome!

     

    $uses <= 2 is going to make sure the value is less then 2.

     

    The query I gave you was adding to uses but your subtracting starting with 2 uses.

     

    This may do the trick:

     

    if (($dcode = $uniquecode) && ($uses <= 2 && $uses > 0)) {

     

    Remember your telling it that you want $uses to be less then or equal to 2 but greater then 0 in order for the user to use the code.

     

    I'm beat been doing php work all day but give that  a try and see if it's working how you want and if not we'll take another look!

     

     

    Not quite working still - just rejects both the code with negative uses and the code with 90 uses (i changed <=2 to <=99)

     

    $row1 = mysql_fetch_array($remainingusescheck, MYSQL_NUM); // MYSQL_NUM since the uses should be a number value, establish $row1 array with the uses
    
    $uses = $row1['uses'];

     

    Will the problem lie here?  as we're saying uses = $row1, however we're not specifying uses anywhere in $row1...i dunno?

     

    I may need to sleep on it :-)

     

    Cheers again buddy,

     

    Tom.

  15. I can't see (though i may be wrong) the code which is then updating $_SESSION['sessionprice'] with this new price - how do we add this?

     

    $discount is the new price.  :)  --- Remember we are taking the session price and then applying the discount by subtracting it from $row['discount'] and then we just show the new price by echoing $discount.

     

    I tried simply deleting the echo, and the screen just refreshes but the price doesnt change - so i'm assuming the $_SESSION['sessionprice'] hasn't been updated as it's echoing correctly when that line of code is included.

     

    There are a few ways to do this. You could simply update the $_SESSION['sessionprice'] with the $discount value by setting the session variable again.

     

    $_SESSION['sessionprice'] = $discount;

     

    You could also just use $discount as that is the new total cost for the customer right?

     

    Finally, i also have a 'uses' column (which we checked earlier in the if statement)...how do we 'minus one' from this column once it's been searched?  Otherwise the code would have infinite uses 

     

    That is correct so you'd have to track the customer by either a $_SESSION variable or something like a login username that won't change. So let's say for the uses table we have 2 columns  (Custname, uses).

    Now when we go to compare the uses we have something to pull it from ( WHERE username = '" . $_SESSION['username'] . "'";) and we could tac that onto our query. Like so:

    $remainingusescheck = mysql_query("SELECT uses FROM discounttable WHERE uniquecode = '".$uniquecode."' && username = '" . $_SESSION['username'] . "'");

     

     

    so after the first validation let's say they have used the code once already so in the database the value will be 1. We'll have to have another query for updating that value. So we'll add a query after the discount is applied to show that the user has used it again.

     

    $adduse = mysql_query("UPDATE discounttable SET uses=uses+1 WHERE uniquecode = '".$uniquecode."' && username = '" . $_SESSION['username'] . "'");

     

    NOTE: this will only work if that user has a value established in that table, you could add this function to your registration script so that when the user registers the name is placed into that table and uses is defaulted to 0.

     

    I hope this is helping you out and not confusing you! Also I'm glad we we're able to get your script working layout wise, now you just need to add more functionallity.

     

    You are awesome and deserve a medal - you have effectively built me a discount code engine  :thumb-up:

     

    The final bug ("just one more thing" - like columbo if you watch it) is that...

     

    if (($dcode = $uniquecode) && ($uses <= 2)) {

     

    ...is accepting a test code which has '0' zero uses remaining.  In fact, now it has minus 2 uses remaining! lol.  I believe this is the last bug to iron out - you've been a great help and i've genuinely learnt more in the last few hours than i have in a long while.

     

    Tom.

     

  16. I took the session price: 39.99 and subtracted our discount: to come up with a total price of $discount

     

    $row['discount'] is the culprit and the output for $discount would have read 39.99 because we're subtracting by 0. Now to find out why.

     

    (AND I messed up on the echo but that's all good!) should've been:

    echo 'I took the session price: ' . $_SESSION['sessionprice'] . ' and subtracted our discount: ' . $row['discount'] . ' to come up with a total price of ' . $discount;

     

    Looking back over the code we are missing a key element. Where are we pulling the discount row from? Nowhere... We're only selecting the uniquecode in one query then the uses in another query. So, which of the tables also holds the discount table?

     

    If it's this one:

     

    $discountcodecheck = mysql_query("SELECT uniquecode FROM discounttable WHERE uniquecode = '".$uniquecode."'");

     

    Then we change to this:

     

    $discountcodecheck = mysql_query("SELECT uniquecode, discount FROM discounttable WHERE uniquecode = '".$uniquecode."'");

     

    And by doing that we'll be giving our $row['discount'] a value to subtract from our sessionprice! :)

     

    Awesome,

     

    I took the session price: 39.99 and subtracted our discount: 39.00 to come up with a total price of 0.99

     

    Which is correct as the test discount code was 39.00.

     

    I can't see (though i may be wrong) the code which is then updating $_SESSION['sessionprice'] with this new price - how do we add this?

     

    I tried simply deleting the echo, and the screen just refreshes but the price doesnt change - so i'm assuming the $_SESSION['sessionprice'] hasn't been updated as it's echoing correctly when that line of code is included.

     

    Finally, i also have a 'uses' column (which we checked earlier in the if statement)...how do we 'minus one' from this column once it's been searched?  Otherwise the code would have infinite uses  :P

     

    Thanks for all your help!

     

    Tom.

     

  17. Let's make some adjustments:

     

    $discount = ($_SESSION['sessionprice'] - $row['discount']);
    echo 'I took the session price: ' . $_SESSION['sessionprice'] . ' and subtracted our discount: ' . $row['discount'] . ' to come up with a total price of $discount';

     

    Let's see what we get from that... I'm still here and will try to help you reach your goal :)

     

    :D  You're a legend.

     

    Message received:

     

    I took the session price: 39.99 and subtracted our discount: to come up with a total price of $discount

     

    :shrug:

     

     

  18. Well atleast the syntax wasn't off I never use mysql statments yay!

     

    So the discount portion may be off because I just placed a value I made up in the IF statement.

     

    Here:

     

     $discount = $_SESSION['sessionprice'] - $row['discount']; // [b]Or whatever column in the database holds the discount amount[/b]

     

    $row['discount'] should be whatever the column name from your database is for it to work properly if not it's just a NULL value because I made it up lol.

     

    Yeh - i changed that, the amended code is:

     

    $discount = $_SESSION['sessionprice'] - $row['discount'] (my amended code)
    $discount = $_SESSION['sessionprice'] - $row['discountamt']; (your original code - replaced by the above line)

     

    So our naming logic is very similar!  However, this is not the issue.  The column is a decimal, as that's supposed to be the 'best' data type in MYSQL for monetary values - if that makes a difference?  If not, what else could it be?  I've read and re-read your code and it makes sense to me, but it's just not doing that final calculation. 

     

    The 'false' part works a-ok and I've customized that so it falls in line with how the rest of the error messages have been previously displayed.

     

    I'm determined to get this before i go to sleep!

     

    Thanks again,

     

    Tom.

     

     

  19. That's all spewed out because the database connection isn't established:

     

    //connection settings bla bla bla
    $uniquecode = $_POST['discountcode'];
    mysql_connect($localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

     

    If your including a file say connect.php that holds those $localhost, $username, $password variables check to make sure they are correct.

     

    If not then make sure you define them prior to connecting to the database.

     

    EDIT:

    Don't feel bad asking for help that's why we're all here! :)

     

    Also I left them out of the example I gave you so you'll have to put that in the script.

     

    Thanks for your kindness.

     

    Sweet - it's almost working, so close now i can taste it.  (It was me being a dumb *** - your instinct as to the cause of the error was dead on.  Sometimes i surprise myself with utter stupidity).

     

    No errors now, but the successful 'if' doesn't seem to alter the session price, it still displays the original full price after a valid discount code is entered.

     

    // Since they check out we can do our discount

      $discount = $_SESSION['sessionprice'] - $row['discount']; // Or whatever column in the database holds the discount amount

      echo $discount;

     

    I added the "echo $discount;" line and sure enough it shows the full price - what's not happening with the calculation?

     

    Thanks buddy

     

    Tom.

     

  20.  if ($dcode = $uniquecode) && ($uses <= 2) {

    should have been :

     

     if (($dcode = $uniquecode) && ($uses <= 2)) {

     

    Usually I think that just means you have an error with your  parentheses

     

    Thanks.

     

    Error message now changed to many many more:

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 9
    
    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 9
    
    Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 11
    
    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 11
    
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 12
    
    Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 16
    
    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 16
    
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 17
    Sorry but you've used twice already.
    Warning: Cannot modify header information - headers already sent by (output started at D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php:9) in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 40
    
    Warning: mysql_close(): no MySQL-Link resource supplied in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 42

     

    I feel bad continually asking for help - but do appreciate it  :thumb-up:

     

  21. I was just using your if statement and correcting the AND if part of it just like JakkyD except I didn't really give you an explanation as to the change.

     

    As for your code as it stands your just returning if the code and uses are found and not checking either:

     

    if ((mysql_num_rows($discountcodecheck) > 0) && (mysql_num_rows($remainingusescheck) > 0)) {

     

    So hey it found the code and there are uses so we're okay to continue on...

     

    This could be your first step and then you could move onto error checking and then relaying errors to the end user. I was give you examples for how to place the information into an array so that you could assign it to variables to work with for comparision.  I don't know how you plan on checking which users have used a discount code so all I could do was provide a generic example.

     

    So for an example let's do this:

     

    <?php
    // Clean up the code the user entered
    $uniquecode = mysql_real_escape_string($_POST['uniquecode']); // OR whatever the name of the input is
    
    $discountcodecheck = mysql_query("SELECT uniquecode FROM discounttable WHERE uniquecode = '".$uniquecode."'");
    $row = mysql_fetch_array($discountcodecheck,  MYSQL_BOTH); // MYSQL_BOTH incase it's alphanumeric, and establish the $row array with the codes
    
    $dcode = $row['uniquecode'];
    
    $remainingusescheck = mysql_query("SELECT uses FROM discounttable WHERE uniquecode = '".$uniquecode."'");
    $row1 = mysql_fetch_array($remainingusescheck, MYSQL_NUM); // MYSQL_NUM since the uses should be a number value, establish $row1 array with the uses
    
    $uses = $row1['uses'];
    
    // You could probably drop this now since the IF statement below will check the codes and uses
    //if ((mysql_num_rows($discountcodecheck) > 0) && (mysql_num_rows($remainingusescheck) > 0)) {
    
    // Add your checking here
        // Check the code vr the code the user entered, and let's make sure the user hasn't used it over 2 uses
       if ($dcode = $uniquecode) && ($uses <= 2) {
    
         // Since they check out we can do our discount
       $discount = $_SESSION['sessionprice'] - $row['discountamt']; // Or whatever column in the database holds the discount amount
    
       } else {
    
        // Let them know the code or the uses have been reached, you could seperate the if statement above to give an error for uses
        echo "Sorry but you've used $dcode twice already.";
    
      } // close our if for checking the code and the uses
    
    ?>

     

    I haven't tested this so I don't know if it will work 100% but hopefully give you an example you can work with.

     

    Thanks - I feel like i owe you a drink with this help  :intoxicated: :intoxicated:

     

    I get the following error:

     

    Parse error: syntax error, unexpected T_BOOLEAN_AND in D:\vhosts\ntillo.com\httpdocs\newsite\discountcode.php on line 26

     

    What does a T_boolean_and error mean?  Not sure how to correct this as i don't understand the error...

     

    Thanks again,

     

    Tom.

     

  22.  

    Thanks for taking the time to reply.

     

    You could dump the information returned from the database for the correct code and then compare that code to the code the user entered. Your if statement above will check to see if the code is found and then continue on.

     

    How do i 'dump' this information?  (i'm new to php but not to coding so i'm a very quick study).

     

    Dump the discount into a variable like:

     $row = mysql_fetch_array($results);

    Then:

    $discount = $row['discount'];
    $newprice = $_SESSION['sessionprice'] - $discount;

     

     

    How do i combine this with what i've already written?  I've been looking at this code for the last half hour and simply do not know where to start in incorporating your code...

     

     

    Also this line:

     

    if ((mysql_num_rows($discountcodecheck) > 0) && (mysql_num_rows($remainingusescheck) > 0))

     

    seems to be ignoring if the 'uses' is > 0 when i try it out.  I have a code with 0 uses and i still seems to accept it with the success message...

     

    I will secure once the code is working - but thanks for checking  :-)

     

    Cheers, I really appreciate the support from you / the php freaks community.

     

    Tom.

     

     

     

     

     

     

  23. "and if" is not a valid expression. Use "&&" to represent AND

     

    <?php 
    if (($a==1) && ($b==2) ) { 
        // code 
    } 
    ?> 

     

    ---->

     

    <?php
    if ((mysql_num_rows($discountcodecheck) > 0) && (mysql_num_rows($remainingusescheck) > 0))
    ?>
    

     

    Should work, not sure if I've got the brackets wrong though

     

    You Sir/Ma'am, are a gentleman/gentlewoman and a Saint ;D

     

    Much appreciated.

     

    That's number 1) above solved, just two more to go!

     

    2) Also, i have stored the session price in $_SESSION['sessionprice'] - is this the best way to do this, and if not how should i store the current price. 

     

    3) Lastly, and how do I do the php mathematics of "$_SESSION['sessionprice'] minus the discount" - again bearing in mind the "discount" needs to relate to the same row as the unique code entered? (as different codes will have different discounts).

     

    Thanks!

     

    Tom.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.