Welcome to WebmasterWorld Guest from 54.162.226.212

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Form Error

parse error, unexpected T_STRING in PHP Form

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

10+ Year Member



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]

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month