Forum Moderators: coopster

Message Too Old, No Replies

invisible syntax error - php/mysql

syntax error, unexpected T_CONSTANT_ENCAPSED_STRING - don't see it!

         

Ishach

6:19 pm on May 8, 2007 (gmt 0)

10+ Year Member



Can't penetrate this syntax error. I am trying to retrieve the LPID from one table to insert it as a foreign key into another table from a MySQL db. I keep getting this message:
"Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in line 48 of the file below."
I don't see any mistakes. HELP!
Ishach

//code below
<?php
// include the database configuration and
// open connection to database
include ('config.php');
include ('opendb.php');

// check if the form is submitted
if(isset($_POST['Submit']))
{ //validate input with PHP classes validator
include ('habProcessor.php');
// get the input from $_POST variable
// trim all input to remove extra spaces
$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$topic = trim($_POST['topic']);
$grade = trim($_POST['grade']);
$standards = trim($_POST['standards']);
$description = trim($_POST['description']);
$lessonplan = trim($_POST['lessonplan']);
$phone = trim($_POST['phone']);
$contact_me = trim($_POST['contact_me']);

reset ($_POST);
while (list ($key, $val) = each ($_POST)) {
if ($val == "") $val = "NULL";
$key = (get_magic_quotes_gpc())? $val : addslashes($val);
if ($val == "NULL")
$_POST[$key] = NULL;
else
$_POST[$key] = $val;
}

$query = "INSERT INTO `tbllessonplans` (`Grade`, `Subject`, `Description`, `Topic`, `Standards`, `LessonPlan`)
VALUES
('".$_POST[grade]."', '".$_POST[subject]."', '".$_POST[description]."', '".$_POST[topic]."', '".$_POST[standards]."', '".$_POST[lessonplan]."')";

//echo $query;
$result = mysql_query($query) or die("Invalid query: " . mysql_error() . "<br><br>". $query);

//get LPID from tbllessonplans to insert into tbllpcontacts for referential integrity

$insertid = mysql_insert_id();

$query1 = "INSERT INTO `tbllpcontact` 
(`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`) VALUES 
('".$insertid."',
'".$_POST[fname]."',
 '".$_POST[lname]."', '".$_POST[phone]."', '".$_POST[email]."', '".$_POST[contact_me]."')";
/*

$query1 = 'INSERT INTO `tbllpcontact` (`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`)
VALUES
($insertid, $_POST['fname'], 
$_POST['lname'], $_POST['phone'],
$_POST['email'], $_POST['contact_me'])';
*/

// execute the query to insert the input to database
// if query fail the script will terminate 
$result1 = mysql_query($query1) or die("Invalid query: " . mysql_error() . "<br><br>". $query1);
//redirect to thank you
header("Location: ../../thanks/thankyou.php");

// quit script
exit;
}
?>

Duskrider

7:52 pm on May 8, 2007 (gmt 0)

10+ Year Member



You should be able to take the double quotes out completely and just use single quotes with no string concatination.
`Contact_me`) VALUES ('$insertid',

Though I may be wrong. :)

[edited by: Duskrider at 7:56 pm (utc) on May 8, 2007]

mcibor

8:27 pm on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The error is inside the POST - you forgot to put ' ' around the array, therefore it is seeing as constants, whereas you cannot use a constant in key declaration.

The following code should work

$query1 = "INSERT INTO `tbllpcontact`
(`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`) VALUES
('".$insertid."',
'".$_POST['fname']."',
'".$_POST['lname']."', '".$_POST['phone']."', '".$_POST['email']."', '".$_POST['contact_me']."')";

However you have some security issues:
make $_POST safe, eg. by using:

$_POST = array_map("mysql_real_escape_string", $_POST);

and I would use as Duskrider suggested:


$_POST = array_map("mysql_real_escape_string", $_POST);
$query1 = "INSERT INTO `tbllpcontact`
(`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`) VALUES
('$insertid','{$_POST['fname']}',
'{$_POST['lname']}', '{$_POST['phone']}', '{$_POST['email']}', '{$_POST['contact_me']}')";

this way the code is a bit clearer.
Regards and have fun!
Michal

Ishach

12:12 pm on May 11, 2007 (gmt 0)

10+ Year Member



I am sorry but, none of the suggestions seem to work

[edited by: eelixduppy at 12:16 pm (utc) on May 11, 2007]
[edit reason] spelling [/edit]

jatar_k

12:29 pm on May 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Ishach,

at first glance it all seems fine, try something simple and make your query all one line like so

$query1 = "INSERT INTO `tbllpcontact` (`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`) VALUES ('".$insertid."', '".$_POST[fname]."', '".$_POST[lname]."', '".$_POST[phone]."', '".$_POST[email]."', '".$_POST[contact_me]."')";

at very least we can see if that changes the line number

Ishach

3:42 am on May 12, 2007 (gmt 0)

10+ Year Member



Sorry, but that did not work. Here is the error message:
Fatal error: Call to undefined function: require () in C:\public_html\library\habesha\phpdesigner_tmp3.php on line 4.

Line 4 is the start of the first require statement.

Ishach

eelixduppy

3:47 am on May 12, 2007 (gmt 0)



This error seems to be related to something else. Has one of the above solutions fixed your previous problem? Also, the new error you are getting doesn't make sense with the code you originally posted; in that code, you aren't using
require
, so I don't know where this error is coming from...

Ishach

10:24 pm on May 14, 2007 (gmt 0)

10+ Year Member



I found the problem yesterday. After I tabbed the require line I stopped getting the error message on line 4. Likewise, I did the same to line 5 and that error message stopped as well. I did not realize a tab could result in so much trouble.

eelixduppy

2:06 am on May 15, 2007 (gmt 0)



A tab shouldn't cause too much trouble assuming it wasn't in between something it shouldn't have been ;)

Glad you got everything resolved, though. :)

ItzFX

2:39 am on May 15, 2007 (gmt 0)

10+ Year Member



Judging by your reply (using tabs to fix the problem) it seems like a faulty linebreak... the difference between \r\n and just \rn