Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 54

Thread: PHP mysql error

  1. #16
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by amcf1992 View Post
    Its not an api key, its a mysql value
    Okay, so where is it supposed to come from then? As mentioned, "key" is not defined in your form to post, so therefore it will not exist in $_POST. Your query will return no results unless there's a record with the 'key' field set to an empty string (not null though).

  2. #17
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,479
    Thanks
    63
    Thanked 538 Times in 525 Posts
    Quote Originally Posted by Fou-Lu View Post
    That should work, and even without error if I'm not mistaken.
    Just reading through to get familliar and noticed this slightly weird remark!

    Whats wrong with the using technique I provided? It's a feature of PHP that lets you make life easier. If it works, it works. It saves you bashing out more stuff on the keyboard too
    I can't really think of anything to write here now...

  3. #18
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by tangoforce View Post
    Just reading through to get familliar and noticed this slightly weird remark!

    Whats wrong with the using technique I provided? It's a feature of PHP that lets you make life easier. If it works, it works. It saves you bashing out more stuff on the keyboard too
    I think he touched based on it but here is the reason why it is better not to use it straight from the manual.

    http://fr2.php.net/manual/en/languag....array.foo-bar
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  4. #19
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,479
    Thanks
    63
    Thanked 538 Times in 525 Posts
    Yes and even the php manual confirms that its ok to do what I showed:

    PHP Code:
    // The following is okay, as it's inside a string. Constants are not looked for
    // within strings, so no E_NOTICE occurs here
    print "Hello $arr[fruit]";      // Hello apple 
    For some reason a few of you in this thread seem to think that I had missed out the single quotes inside the square brackets despite being an sql query wrapped in double quotes. I'm perplexed as to why you all seem to think I've got it so dreadfully wrong when as shown above, the php manual clearly shows this technique is 'okay'.

    To quote:
    as it's inside a string. Constants are not looked for within strings
    Last edited by tangoforce; 09-03-2012 at 04:08 PM.
    I can't really think of anything to write here now...

  5. #20
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    But did you read the explanation? It is okay but there is a small risk you need to be willing to take that it may not work in future versions of php if they decide to define constants that aren't defined so now you would end up with something like $_POST[] if you used $_POST[bar] because bar as a constant wouldn't contain anything.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  6. #21
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,479
    Thanks
    63
    Thanked 538 Times in 525 Posts
    You're slightly misunderstanding what they're saying I think:
    PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.
    Thats different to the technique I advised. You're thinking of
    PHP Code:
    print $_POST[foo]; 
    but I was talking of
    PHP Code:
    print "$_POST[foo]"
    PHP are talking of defining constants instead of using a string if it exists. I am not demonstrating using a constant but using a string which (as quoted in my previous post) is treated as a string because its already inside a string. Strings are variable so PHP isn't suddenly going to decide that there is a constant half way through a string is it!
    Last edited by tangoforce; 09-03-2012 at 04:18 PM.
    I can't really think of anything to write here now...

  7. #22
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Makes sense. Just looks strange I guess, similar to how Fou-Lu to him makes it look like a constant so we need to make it a string.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  8. #23
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,479
    Thanks
    63
    Thanked 538 Times in 525 Posts
    Quote Originally Posted by tangoforce View Post
    Should be this:

    "SELECT id, email, key FROM friendko_text WHERE key = '$_POST[key]'";
    Quote Originally Posted by Fou-Lu View Post
    That should work, and even without error if I'm not mistaken.
    But I don't like that. The 'key' in my eyes is now a constant which should be a string.
    What I mean is to do as:
    PHP Code:
    $query "SELECT id, email, key FROM friendko_text WHERE key = {$_POST['key']}";
    // or
    $query "SELECT id, email, key FROM friendko_text WHERE key = " $_POST['key']; 
    Having looked at this post again, the only way 'key' could become a constant in my example string is if you're thinking that my double quotes were just used to quote a string and not to show the quotes as part of the code. I did however leave a semicolon there to show that it should be seen as code.

    I would like to clarify that my double quotes were supposed to be included as part of the code and NOT just to show the SQL itself. I should have used php code tags but I was in a rush.
    I can't really think of anything to write here now...

  9. #24
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,479
    Thanks
    63
    Thanked 538 Times in 525 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Just looks strange I guess, similar to how Fou-Lu to him makes it look like a constant so we need to make it a string.
    Which is why I was asking Fou what was wrong! Fou operates on a higher plane of intelligence to the rest of us using complicated words, explanations and understandings so I'm sure there must be another reason logical for his previous comments
    I can't really think of anything to write here now...

  10. #25
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Whoa whoa, I didn't say it was wrong to do it. I said it was wrong in my eyes to do that.

    Strings are evaluated as scalar and never expand constants. Because of this, using print "an $array[var]"; is valid since 'var' is considered a string already and will not fall back to a constant (and therefore it will not trigger a notice). But to me, strings should only evaluate scalar data unless indicated that it should parse complex data. PHP is greedy in its expansion as well, regardless of if a variable is a complex type.

    All of these expansion issues is why I normally NEVER embed variables within a string and go with printf's for control instead. For an example:
    PHP Code:
    $a = array('var' => 'value');
    $b 'array';

    print 
    "$a[var], $b[var]\n"
    My intent here is to print the string value, array[var], but I won't end up with that. Instead I end up with value, a since 'var' is an invalid offset, but won't trigger a notice of such. Any of the following will give me what I want:
    PHP Code:
    print "{$a['var']}, {$b}[var]" PHP_EOL;
    print 
    $a['var'] . ', ' $b '[var]' PHP_EOL;
    printf('%2$s, %1$s[var]' PHP_EOL$b$a['var']); 
    Given the complexity of the first two, that is why I much prefer the use of a print formatter instead. This reminds me a lot of the back references as well, when you say \11, do you mean \1 followed by a 1, or \11, or a tab? Complex evaluation would get around that as well.

    Edit:
    BTW, for a real world example of such:
    PHP Code:
    $name 'myinput';
    for (
    $i 0$i 4; ++$i)
    {
        print 
    "<input type=\"text\" name=\"$name[$i]\" value=\"\" />\n";

    Which would return
    Code:
    <input type="text" name="m" value="" />
    <input type="text" name="y" value="" />
    <input type="text" name="i" value="" />
    <input type="text" name="n" value="" />
    Instead of my intended
    Code:
    <input type="text" name="myinput[0]" value="" />
    <input type="text" name="myinput[1]" value="" />
    <input type="text" name="myinput[2]" value="" />
    <input type="text" name="myinput[3]" value="" />
    Last edited by Fou-Lu; 09-03-2012 at 06:39 PM.

  11. #26
    Regular Coder
    Join Date
    Jul 2011
    Posts
    141
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Code update:

    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","friendko_admin","Macbookpro17#");
     if (!
    $con)
       {
       die(
    'Could not connect: ' mysql_error());
       }
     
    mysql_select_db("friendko_text"$con);



    $query "SELECT id, email, key FROM friendko_text WHERE key = '{$_POST['key']}'";
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))  
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    'Carrier: ' $carrier'] . '<br>';
    }


    $to = $_POST['
    to'];
    $carrier = $_POST['
    carrier'];
    $message = stripslashes($_POST['
    message]);

    if ((empty(
    $from)) || (empty($to)) || (empty($message))) {
    header ("Location: sms_error.php");
    }

    else if (
    $carrier == "verizon") {
    $formatted_number $to."@vtext.com";
    mail("$formatted_number""SMS""$message"); 
    // Currently, the subject is set to "SMS". Feel free to change this.

    header ("Location: sms_success.php");
    }

    else if (
    $carrier == "tmobile") {
    $formatted_number $to."@tomomail.net";
    mail("$formatted_number""SMS""$message");

    header ("Location: sms_success.php");
    }

    else if (
    $carrier == "sprint") {
    $formatted_number $to."@messaging.sprintpcs.com";
    mail("$formatted_number""SMS""$message");

    header ("Location: sms_success.php");
    }

    else if (
    $carrier == "att") {
    $formatted_number $to."@txt.att.net";
    mail("$formatted_number""SMS""$message");
    header ("Location: sms_success.php");
    }

    else if (
    $carrier == "virgin") {
    $formatted_number $to."@vmobl.com";
    mail("$formatted_number""SMS""$message");

    header ("Location: sms_success.php");
    }








    ?>
    HTML

    PHP Code:
    <html>
    <
    head>
    </
    head>
    <
    body>
    <
    form id="sms" name="sms" method="post" action="send.php">
    <
    table width="400">
      <
    tr>
        <
    td align="right" valign="top">11 Digit ID:</td>
        <
    td align="left"><input name="key" type="text" id="key" size="11" /></td>
      </
    tr>
     
        <
    td align="right" valign="top">Message:</td>
        <
    td align="left"><textarea name="message" cols="40" rows="5" id="message"></textarea></td>
      </
    tr>
      <
    tr>
        <
    td colspan="2" align="right"><input type="submit" name="Submit" value="Submit" /></td>
        </
    tr>
    </
    table>
    </
    form>
    </
    body>
    </
    html
    I did make the key change to the html form, the php code is not redirecting. Thanks for all your help!





    Quote Originally Posted by _Aerospace_Eng_ View Post
    $_POST['key'] will always be empty. You have no form input with name="key". You probably got some kind of key from the place letting you use their SMS service that you need to provide to their api.

  12. #27
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You have a problem here
    PHP Code:
    $query "SELECT id, email, key FROM friendko_text WHERE key = '{$_POST['key']}'";
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))  
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    'Carrier: ' $carrier'] . '<br>';
    }


    $to = $_POST['
    to'];
    $carrier = $_POST['
    carrier'];
    $message = stripslashes($_POST['
    message]); 
    It should be
    PHP Code:

    $to 
    $_POST['to'];
    $carrier $_POST['carrier'];
    $message stripslashes($_POST['message']);
    $query "SELECT id, email, key FROM friendko_text WHERE key = '{$_POST['key']}'";
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))  
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    'Carrier: ' $carrier '<br>';

    Your code is very insecure by the way. I suggest you look into preventing SQL injection: http://www.tizag.com/mysqlTutorial/m...-injection.php
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  13. #28
    Regular Coder
    Join Date
    Jul 2011
    Posts
    141
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    You have a problem here
    PHP Code:
    $query "SELECT id, email, key FROM friendko_text WHERE key = '{$_POST['key']}'";
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))  
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    'Carrier: ' $carrier'] . '<br>';
    }


    $to = $_POST['
    to'];
    $carrier = $_POST['
    carrier'];
    $message = stripslashes($_POST['
    message]); 
    It should be
    PHP Code:

    $to 
    $_POST['to'];
    $carrier $_POST['carrier'];
    $message stripslashes($_POST['message']);
    $query "SELECT id, email, key FROM friendko_text WHERE key = '{$_POST['key']}'";
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))  
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    'Carrier: ' $carrier '<br>';

    Your code is very insecure by the way. I suggest you look into preventing SQL injection: http://www.tizag.com/mysqlTutorial/m...-injection.php
    Yes, very insecure. I am just focusing on getting this to work

    I removed $from, since it had no value. Also the its still stuck on send.php

    now its

    PHP Code:
    $con mysql_connect("localhost","friendko_admin","Macbookpro17#");
     if (!
    $con)
       {
       die(
    'Could not connect: ' mysql_error());
       }
     
    mysql_select_db("friendko_text"$con);




    $key $_POST['key'];
    $carrier $_POST['carrier'];
    $message stripslashes($_POST['message']);
    $query "SELECT id, email, key FROM friendko_text WHERE key = '{$_POST['key']}'";
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))  
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    'Carrier: ' $carrier '<br>';
    }  

    if ((empty(
    $key)) || (empty($message)) {
    header ("Location: sms_error.php");

    Last edited by amcf1992; 09-04-2012 at 12:42 AM.

  14. #29
    Regular Coder
    Join Date
    Jul 2011
    Posts
    141
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Full PHP Code
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","friendko_admin","Macbookpro17#");
     if (!
    $con)
       {
       die(
    'Could not connect: ' mysql_error());
       }
     
    mysql_select_db("friendko_text"$con);




    $key $_POST['key'];
    $message stripslashes($_POST['message']);
    $query "SELECT id, email, key FROM friendko_text WHERE key = '{$_POST['key']}'";
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))  
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    'Carrier: ' $row['carrier'] . '<br>';
      echo 
    'To: ' $row['cell'] . '<br>';
    }  

    if ((empty(
    $key)) || (empty($message)) {
    header ("Location: sms_error.php");
    }

    else if (
    $row['carrier'] == "verizon") {
    $formatted_number $to."@vtext.com";
    mail("$formatted_number""SMS""$message"); 
    // Currently, the subject is set to "SMS". Feel free to change this.

    header ("Location: sms_success.php");
    }

    else if (
    $row['carrier'] == "tmobile") {
    $formatted_number $to."@tomomail.net";
    mail("$formatted_number""SMS""$message");

    header ("Location: sms_success.php");
    }

    else if (
    $row['carrier'] == "sprint") {
    $formatted_number $to."@messaging.sprintpcs.com";
    mail("$formatted_number""SMS""$message");

    header ("Location: sms_success.php");
    }

    else if (
    $row['carrier'] == "att") {
    $formatted_number $to."@txt.att.net";
    mail("$formatted_number""SMS""$message");
    header ("Location: sms_success.php");
    }

    else if (
    $row['carrier'] == "virgin") {
    $formatted_number $to."@vmobl.com";
    mail("$formatted_number""SMS""$message");

    header ("Location: sms_success.php");
    }





    I think the best thing is getting the while statement to echo the $row valuesAny help is greatly appreciated.


    ?>
    Last edited by amcf1992; 09-04-2012 at 12:53 AM.

  15. #30
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You still need $to which you don't have anymore so no emails will get sent but to get the values to print in the while loop you need to actually select them
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","friendko_admin","Macbookpro17#");
     if (!
    $con)
       {
       die(
    'Could not connect: ' mysql_error());
       }
     
    mysql_select_db("friendko_text"$con);




    $key $_POST['key'];
    $message stripslashes($_POST['message']);
    $query "SELECT id, email, key, carrier, cell FROM friendko_text WHERE key = '{$_POST['key']}'";
    $result mysql_query($query) or die($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))  
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    'Carrier: ' $row['carrier'] . '<br>';
      echo 
    'To: ' $row['cell'] . '<br>';

    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||


 
Page 2 of 4 FirstFirst 1234 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •