Jump to content

[SOLVED] Does this look right?


tqla

Recommended Posts

Please take a look at this mysql string and tell me if it looks right

 

	$sql = "UPDATE member SET paydate=CURDATE(), status='regular', payamount=payamount+".$amount." , validitydate='".$validitydate."' WHERE id=".$_SESSION['userid'];

 

Why does validitydate have single and then double quotes and then concantenation? Also Why does $_Session have one "." before it?

 

It works but I've never seen it like this before.

 

Curious...

 

 

Link to comment
Share on other sites

Please take a look at this mysql string and tell me if it looks right

It works but I've never seen it like this before.

 

Sounds like you answered your own question.

 

If you understand correctly how an SQL UPDATE statement should look, how to give money and datetime data to a database, and how string concatenation in PHP works then it shouldn't be confusing at all.

Link to comment
Share on other sites

You could rewrite it like this (there are certain rules for interpolating strings).

 

   $sql = "UPDATE member SET paydate='CURDATE()', status='regular', payamount=payamount+$amount, validitydate='$validitydate' WHERE id={$_SESSION['userid']};

Link to comment
Share on other sites

It is almost stunning how sarcastic people are on this site... even the mods...

 

Well, a bit of independent thinking doesn't hurt you know. If it works then there isn't anything wrong with it. That's a logical truth. If there is something wrong with it then it doesn't work, so these are logical equivalents. Negating both of them will cause them to still have the same truth value.

Link to comment
Share on other sites

Thanks roopurt18, I do understand and I would not have written it this way but I inherited it like this so I wasn't sure if there was something about it that I did not understand.

 

Thanks Matthew, ah ha,  I knew the concat was odd.

 

Thanks Maq. That makes more sense to me

 

But what of the "WHERE id=".$_SESSION['userid'];" ? Why the period before $_SESSION and not after too? Why is it there at all?

Link to comment
Share on other sites

That's not what I (or roopurt for that matter) is talking about. The fact that your query executes and has the correct side effects means it is correct. Asking whether or not it is correct when you've just found out that is redundant.

Link to comment
Share on other sites

It's the way you concatenate strings in PHP.

 

Example:

<?php
$name = 'Ken2k7';
$e = 'hello my name is ' . $name;

 

I have the dot for concatenation, but I don't have anything else to concatenate after that, so I don't need a dot after it. :-\

Link to comment
Share on other sites

Because he's building a PHP string and concatenating the value of variables to it:

 

<?php
$sql = 
    "UPDATE member SET paydate=CURDATE(), status='regular', payamount=payamount+"                  // DOUBLE QUOTED PHP STRING

    . $amount                                                                     // CONCAT A PHP VARIABLE

    . " , validitydate='"                                                        // DOUBLE QUOTED PHP STRING

    . $validitydate                                                            // CONCAT A PHP VARIABLE

    . "' WHERE id="                                                             // DOUBLE QUOTED PHP STRING

    . $_SESSION['userid']                                                   // CONCAT A PHP VARIABLE

    ;                                                                               // END OF PHP STATEMENT
?>

 

It is almost stunning how sarcastic people are on this site... even the mods...

Everything I said was matter-of-fact, no sarcasm included.  :)

Link to comment
Share on other sites

Daniel0, hey, I don't want to argue or anything but I did not ask if it was correct. I said, "It works but I've never seen it like this before." Hoping that someone could shed some light on this style of writing that I have never seen before. :)

 

Thanks all for the explanation. I understand it now. roopurt18 thanks for taking the time to break it out like that too.

Link to comment
Share on other sites

It is almost stunning how sarcastic people are on this site... even the mods...

 

Well, a bit of independent thinking doesn't hurt you know. If it works then there isn't anything wrong with it. That's a logical truth. If there is something wrong with it then it doesn't work, so these are logical equivalents. Negating both of them will cause them to still have the same truth value.

 

I understand, it just would have been less typing to answer the question :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.