RE: [PHP] Help with logic :(
| From: | Kilbride, James | Date: | Tue, 11 Oct 2005 17:40:26 +0000 |
| Subject: | RE: [PHP] Help with logic :( | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-223909@lists.php.net to get a copy of this message | ||
Did you remember to open the database connection?
> -----Original Message-----
> From: aaronjw@martekbiz.com [mailto:aaronjw@martekbiz.com]
> Sent: Tuesday, October 11, 2005 1:34 PM
> To: Dan McCullough
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Help with logic :(
>
> <?php
>
> include ("../utils.inc");
>
> $date = date("U");
> $dateExpire = $date + 90 * 86400;
>
> $code = "jack";
>
> $query = "INSERT INTO CouponTable VALUES
>
> ('','$date','0','$code','preset','$dateExpire','3.75')";
>
> $result = mysql_query($query);
>
> echo mysql_error($result);
>
> ?>
>
> This SHOULD error out but I'm getting the error instead.
>
> It's just a test page to test my logic...
>
> A
> > got some code so we can see?
> >
> > On 10/11/05, aaronjw@martekbiz.com <aaronjw@martekbiz.com> wrote:
> >> Thanks everyone. Makes sense. Now I got another issue.
> >>
> >> When I try to see if an error exists I get this:
> >>
> >> Warning: mysql_error(): supplied argument is not a valid
> MySQL-Link
> >> resource in /home/********/public_html/Store/test.php on line 14
> >>
> >> Is there a setting somewhere that I need to set to make
> sure this is
> >> available?
> >>
> >> Thanks and sorry is these are dumb-ass questions :)
> >>
> >> Aaron
> >>
> >>
> >> > Yes I believe that is what Richard was saying about "Trap the
> >> > INSERT error", you should get an error back from the
> database about
> >> > having a problem with the insert instead of showing that
> error you
> >> > will want to add some logic that when that error occurs
> you simply
> >> > generate another number.
> >> >
> >> > On 10/11/05, aaronjw@martekbiz.com <aaronjw@martekbiz.com> wrote:
> >> >> Hi Richard,
> >> >>
> >> >> Ok... I do already have a Unique index on the column but this
> >> >> process
> >> is
> >> >> something that I cannot echo out the error code to the
> user. The
> >> >> code
> >> is
> >> >> generated on the fly when an order an is placed so you
> can see...
> >> >> I can't echo out the error to the customer.
> >> >>
> >> >> Can I do an if statement IF an error is returned to try
> generatre
> >> >> another randon code and keep going until one is
> available? I can
> >> >> do this all intenrally without the customer knowing/seeing
> >> >> anything?
> >> >>
> >> >> Hope I am making some sense. Not trying to confuse the issue.
> >> >>
> >> >> Thanks. Appreciate your thoughts.
> >> >>
> >> >> Aaron
> >> >>
> >> >> > On Mon, October 10, 2005 3:24 pm, Dan McCullough wrote:
> >> >> >> create a function to check if the rndnumber=couponcode row
> >> >> >> count =
> >> 0
> >> >> >> if not then redo rndnumber if it does = 0 then
> insert rndnumber
> >> >> >
> >> >> > Noooooooooooooooooooooooooooo!
> >> >> >
> >> >> > You are creating a RACE CONDITION in which ONE user might
> >> >> > generate
> >> a
> >> >> > 'valid' code, and ANOTHER user might generate a
> 'valid' code AT
> >> >> > THE SAME TIME, and then they BOTH get the same coupon code.
> >> >> >
> >> >> > The probability of this is very very very low, but
> still NOT zero.
> >> >> >
> >> >> > And it's the kind of thing that won't show up in testing, but
> >> >> > sure
> >> as
> >> >> > God made little green apples, it WILL happen at the worst
> >> >> > possible time after you "go live"
> >> >> >
> >> >> > The database engine has a *TON* of code in it to
> avoid this kind
> >> >> > of Bad Thing happening.
> >> >> >
> >> >> > Use it.
> >> >> >
> >> >> > create a UNIQUE INDEX on the column that needs to be unique.
> >> >> >
> >> >> > Trap the INSERT error.
> >> >> >
> >> >> > --
> >> >> > Like Music?
> >> >> > http://l-i-e.com/artists.htm
> >> >> >
> >> >> > --
> >> >> > PHP General Mailing List (http://www.php.net/) To
> unsubscribe,
> >> >> > visit: http://www.php.net/unsub.php
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >
> >> > --
> >> > PHP General Mailing List (http://www.php.net/) To unsubscribe,
> >> > visit: http://www.php.net/unsub.php
> >> >
> >> >
> >>
> >>
> >
> > --
> > PHP General Mailing List (http://www.php.net/) To
> unsubscribe, visit:
> > http://www.php.net/unsub.php
> >
> >
>
> --
> PHP General Mailing List (http://www.php.net/) To
> unsubscribe, visit: http://www.php.net/unsub.php
>
>