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 1 of 4 123 ... LastLast
Results 1 to 15 of 54

Thread: PHP mysql error

  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts

    PHP mysql error

    I am having the following error:

    PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /public_html/path1/path2/ on line 10

    Introduction: I am building a PHP text messaging service. Db connect to mysql, script selects the user information from the database and uses it to send the sms to that person.

    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($result))
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    '<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"); 
    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");
    }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You cannot do this:
    PHP Code:
    $query "SELECT id, email, key FROM friendko_text WHERE key = $_POST['key']"
    Either break from the string and apply the variable through standard evaluation, or wrap the complex type with {} to evaluate them within a string.

    It appears that you have whitespace preceding your code as well. You may want to remove that if you decide to use sessions, cookies, or redirection headers (which you have used), as they will only work if output buffering is enabled at a system level. I can't be 100% sure its there, but my count puts your error on line 9, not 10.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    The simple version of what Fou is saying is that this:
    "SELECT id, email, key FROM friendko_text WHERE key = $_POST['key']";

    Should be this:

    "SELECT id, email, key FROM friendko_text WHERE key = '$_POST[key]'";
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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']; 

  • #5
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    hi tangoforce

    this is purely a question

    would '$_POST['key']' work in this instance?

    shouldn't it either be '".$_POST['key']."'

    or {$_POST['key']}

    ???

    only asking
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by nomanic View Post
    hi tangoforce

    this is purely a question

    would '$_POST['key']' work in this instance?

    shouldn't it either be '".$_POST['key']."'

    or {$_POST['key']}

    ???

    only asking
    It will, the issue that PHP is having is the use of [''] inside of the string where it is aware that you have a complex datatype but isn't sure how to deal with it now (since strings are scalar). Best I know it actually does so without error since the inner associate index is already a part of a string, so it should work without triggering an undefined constant error.

    Still, I don't like it, and never have. I prefer the complex evaluation or the print formatters.

    Edit:
    Sorry it won't, missed your example quotations. The inner ones are the problem, they can be removed without error.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by nomanic View Post
    hi tangoforce

    this is purely a question

    would '$_POST['key']' work in this instance?
    Hi

    No.

    Quote Originally Posted by nomanic View Post
    shouldn't it either be '".$_POST['key']."'
    Possibly though after the final "' you'd need another " to terminate the string.

    Quote Originally Posted by nomanic View Post
    or {$_POST['key']}
    It its still inside a double quoted string, again yes, possibly.

    I only say possibly because its one of 3 different methods that would work. I used the simplest / easiest to understand method earlier. Fou isn't keen on the in-string (as I call it) method whereas I find it rather useful and it saves a few seconds to type.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    I didnt see
    you took the quotes out of your suggestion
    thats why I was confused
    I didint think it would work with them in, but you took them out!
    sorry!
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Ok ..
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks for everyones responses, I did make the change as mentioned but its still redirection me to sms_error.php

    Here my code so far

    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($result))
    {
      echo 
    '11 Digit Key: ' $row['key'] . '<br>';
      echo 
    'Email: ' $row['email'] . '<br>';
      echo 
    '<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");
    }
    ?>

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If key is a string then enclose the value in apostrophes:

    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)) 
    If you are only using $row as an associative array that it is better to explicitly return such an array by specifying MYSQL_ASSOC (or use mysql_fetch_assoc()).

    But I'm not sure about the re-direction. Use echo $to; etc. to check these values.
    Last edited by AndrewGSW; 09-03-2012 at 12:43 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #12
    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
    Where is $from defined? I don't see that anywhere in your code which will always cause you go to to the sms_error page.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #13
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Where is $from defined? I don't see that anywhere in your code which will always cause you go to to the sms_error page.
    Here is my updated code:

    HTML FORM

    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="to" type="text" id="to" 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
    Here's the php

    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: ' $row['carrier'] . '<br>';
    }

    ?>
    Once I can successfully echo the variables i can proceed to the rest of the code.

  • #14
    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
    $_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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #15
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Its not an api key, its a mysql value


  •  
    Page 1 of 4 123 ... 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
    •