Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-29-2012, 06:36 PM   PM User | #1
amcf1992
Regular Coder

 
Join Date: Jul 2011
Posts: 114
Thanks: 7
Thanked 0 Times in 0 Posts
amcf1992 is an unknown quantity at this point
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");
}
?>
amcf1992 is offline   Reply With Quote
Old 08-29-2012, 06:47 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 08-29-2012, 06:52 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,665
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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]'";
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 08-29-2012, 06:56 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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']; 
Fou-Lu is offline   Reply With Quote
Old 08-29-2012, 06:56 PM   PM User | #5
nomanic
Regular Coder

 
nomanic's Avatar
 
Join Date: Feb 2009
Location: United Kingdom
Posts: 252
Thanks: 9
Thanked 33 Times in 33 Posts
nomanic is an unknown quantity at this point
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/
nomanic is offline   Reply With Quote
Old 08-29-2012, 07:01 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 08-29-2012, 10:08 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,665
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 08-29-2012, 10:15 PM   PM User | #8
nomanic
Regular Coder

 
nomanic's Avatar
 
Join Date: Feb 2009
Location: United Kingdom
Posts: 252
Thanks: 9
Thanked 33 Times in 33 Posts
nomanic is an unknown quantity at this point
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/
nomanic is offline   Reply With Quote
Old 08-29-2012, 11:00 PM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,665
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Ok ..
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 09-03-2012, 12:26 AM   PM User | #10
amcf1992
Regular Coder

 
Join Date: Jul 2011
Posts: 114
Thanks: 7
Thanked 0 Times in 0 Posts
amcf1992 is an unknown quantity at this point
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");
}
?>
amcf1992 is offline   Reply With Quote
Old 09-03-2012, 12:41 AM   PM User | #11
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
__________________
"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

Last edited by AndrewGSW; 09-03-2012 at 12:43 AM..
AndrewGSW is offline   Reply With Quote
Old 09-03-2012, 12:55 AM   PM User | #12
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,292
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
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!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 09-03-2012, 01:06 AM   PM User | #13
amcf1992
Regular Coder

 
Join Date: Jul 2011
Posts: 114
Thanks: 7
Thanked 0 Times in 0 Posts
amcf1992 is an unknown quantity at this point
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.
amcf1992 is offline   Reply With Quote
Old 09-03-2012, 01:08 AM   PM User | #14
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,292
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
$_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!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 09-03-2012, 01:25 AM   PM User | #15
amcf1992
Regular Coder

 
Join Date: Jul 2011
Posts: 114
Thanks: 7
Thanked 0 Times in 0 Posts
amcf1992 is an unknown quantity at this point
Its not an api key, its a mysql value
amcf1992 is offline   Reply With Quote
Reply

Bookmarks

Tags
error, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:32 PM.


Advertisement
Log in to turn off these ads.