homepage Welcome to WebmasterWorld Guest from 54.204.127.56
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Form Error
parse error, unexpected T_STRING in PHP Form
marito17




msg:1269491
 9:59 pm on Oct 25, 2002 (gmt 0)

I am new to PHP and MySQL and I have a Form that asks for values in a text box and inserts the values into a MySQL database.

---- START OF CODE ------------

<html>
<head><title>Enter your user information</title><head>
<body>
<?
// Set a variable with the database name
$database_name = "mydb";
// Attempt to connect to database and store result
$dbh = mysql_connect("localhost","root");
// Check the result of the connection attempt
if (!mysql_select_db($database_name)) {
echo "Can't Select $database_name";
}
// Conditional test to see if submit button was pressed
if (isset ($enter_data)) {
// Build our query string
$sql = “insert into employees (first,last,address) values (‘$first’, ‘$last’, ‘$address’)”;
// Execute a query and store the result
$res = mysql_query($sql,$dbh);
// Check to make sure the query actually ran
if (!$res) {
echo mysql_errno().": ". mysql_error ()."";
return 0;
}
printf(”<p>Record successfully added</p>”);
}

?>

<h2>A listing of all current users</h2>
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>

<?
// Execute a query and store the result
$res = mysql_query("SELECT * from employees",$dbh);

// Check to make sure the query actually ran
if (!$res) {
echo mysql_errno().": ". mysql_error ()."";
return 0;
}

// Use the query results
while ($thearray = mysql_fetch_array($res)) {
printf("<tr><td>$thearray[id]</td><td>$thearray[first]
</td><td>$thearray[last]</td><td>$thearray[address]
</td></tr>");
}
?>
</table>

<form name=”user_info” method=”post” action=”<? $_SERVER['PHP_SELF']?>”>

First Name: <input type=”text” name=”first”>
Last Name: <input type=”text” name=”last”>
Email address: <input type=”text” name=”address”>
<input type=”submit” name=”enter_data” value=”Enter Data”>
</form>

</body>
</html>

------------- END OF CODE --------------

When I load the php page I get the following error:
Parse error: parse error, unexpected T_STRING in C:\Program Files\Apache Group\Apache\htdocs\mysql6.php on line 16

Line 16 is the insert statement. I checked it in the database and table employees with columns first, last, and address exist. Line 16 in the code is:

$sql = “insert into employees (first,last,address) values (‘$first’, ‘$last’, ‘$address’)”;

Please help me out with this.

Thanks

Mario

[edited by: jatar_k at 1:59 am (utc) on Oct. 26, 2002]
[edit reason] no sigs please [/edit]

 

jdMorgan




msg:1269492
 11:44 pm on Oct 25, 2002 (gmt 0)

Mario,

PHP may not like the left and right single quotes - those are "extended-code" characters.
Try editing that line with a plain-text editor to use a "standard" single quote instead.
I'm no PHP expert, but that has caused problems in other languages I've used.

Jim

dingman




msg:1269493
 3:11 am on Oct 26, 2002 (gmt 0)

Looks to me like Jim is right. Use standard straight open-is-the-same-as-close double-quotes for interpolated strings, the character otherwise known as an apostrophe for non-interpolated strings, and single 'back quotes', '`' for shell commands to be replaced with their own output. Use the latter with great care and input-checking!

I don't know about MySQL, but a quick test shows that Postgres likes '`' as little as I expected, which is to say not at all.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved