Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 31st, 2003, 03:07 PM
Authorized User
 
Join Date: Jul 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error on form handling

Hi
I have been trying to sort this out but i could not.
It gives this error every time: Parse error, expecting 't_string' or 't_variable' or 't_num_string' in ........ on line 21
The related line is like this:
$Query = "INSERT INTO $TableName VALUES ($HTTP_POST_VARS['mytextbox'])";
The variable $HTTP_POST_VARS['mytextbox'] works when I use it in an echo statement but it does not work here. I am going to be mad.
Pls help me...
 
Old July 31st, 2003, 03:18 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

when you access arrays like $HTTP_POST_VARS['mytextbox'] you cannot place them inside quotes (""). do it like this:
$Query = "INSERT INTO $TableName VALUES (".$HTTP_POST_VARS['mytextbox'].")";

----------------------------
http://aeonofdarkness.com
 
Old July 31st, 2003, 03:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is a cross post with PHP Databases. The following is a copy of my answer posted there (http://p2p.wrox.com/topic.asp?TOPIC_ID=2468)

You should remove the single-quotes from your array index. When parsing double-quoted strings, if PHP finds an array variable, it can only parse a single level of nesting. It assumes that whatever is in the square brackets is either the numerical, string, or variable index. If you attempt to echo nested arrays, the first level of nesting will evaluate to the string "Array", and the additional array indexes will be echoed as literal strings.

You can also use curly-brace syntax for all variable substitutions, including nested arrays.

Here are some examples:

$me['name']['first'] = 'nikolai';
$me['age'] = 27;
$stat = "age"

echo "My name is {$me['name']['first']}."; // "My name is nikolai."
echo "My name is $me['name']['first']."; // Parse error
echo "My name is $me[name][first]."; // "My name is Array[first]."
echo "I am $me[age]."; // I am 27.
echo "I am {$me['age']}."; // I am 27.
echo "I am $me['age']}." // Parse error.
echo "I am $me[$stat]." // I am 27.


As always, read the manual!
 http://www.php.net/types.string


Take care,

Nik
http://www.bigaction.org/
 
Old July 31st, 2003, 07:09 PM
Authorized User
 
Join Date: Jul 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx for your replies.
But now I have had another error message which is:
1054: Unknown column 'fff' in 'field list'
The problem is that i am trying to create a php file to write on a mysql database.
Codes are like this:
FORM:
<?
echo("
<form method=\"post\" action=\"formhandle4.php\">
Name: <input type=\"text\" name=\"mytextbox\" /><br/>
<p><input type=\"submit\" name=\"mybutton\" value=\"Submit\" /> <input type=\"reset\" value=\"Reset Form\" /> </p>
</form>
");
?>
formhandle4.php:
<?php
if (isset($HTTP_POST_VARS['mybutton'])) {
global $HTTP_POST_VARS;
echo "You clicked the button\n";
echo "You typed ". $HTTP_POST_VARS['mytextbox'] . " in the textbox.<br>\n";

$Host = "...";
$DBName = "...";
$TableName = "...";
$Link = mysql_connect ($Host, '...', '...');
if ($Link==false) {
echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_select_db ($DBName, $Link);
$Query = "INSERT INTO $TableName VALUES ($HTTP_POST_VARS[mytextbox])";
if (mysql_query ($Query, $Link)) {
print ("The query was successfully executed!<BR>\n");
}
else {
print ("The query could not be executed!<BR>\n");
echo mysql_errno().": ".mysql_error()."<BR>\n";
}
}
else {
echo "you must have come here from somewhere else.\n";
}
?>

When i submit the form i get this message:
You clicked the button You typed fff in the textbox.
The query could not be executed!
1054: Unknown column 'fff' in 'field list'

Your help will be highly appreciated.

MEG
 
Old July 31st, 2003, 10:33 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default


Some observations:
Use the $_POST array instead of $HTTP_POST_VARS... The $HTTP_POST_VARS is deprecated and should be avoided... That is unless you're using PHP 3 or PHP 4 prior to 4.1.

Saying that you cannot include the quoted array inside quotes isn't entirely true...
You can use curly braces around a variable inside a quoted string and PHP will expect that to be a variable or constant thereby treating the quotes as you would expect it to, as part of the array variable. (hopefully I am quoting that correcting...)...all I know is it works! I got that from Nik a few months ago and has been one of the most useful things I've learned!

$Query = "INSERT INTO $TableName VALUES ('{$_POST['mytextbox']}')";

And unless your query is always changing its easier to just stick it right into the function.. it also helps to clean up the code a bit.

mysql_query ("INSERT INTO $TableName VALUES ('{$_POST[mytextbox]}')";, $Link)

Also, if you are using the $_POST array,
There would be no need for this statement:
global $HTTP_POST_VARS;
As $_POST is an autoglobal.

As for your error, I'm not sure, your variable is not quoted, and neither is the field... Do you have only one field in your database?

This is not syntactically correct:
$Query = "INSERT INTO $TableName VALUES ($HTTP_POST_VARS[mytextbox])";

The [mytextbox] should be quoted. If it isn't quoted it would be assumed a constant first and then a string. Try the curly braces, or try Nik's suggestion, by doing the join.

Check that you database is set-up correctly. Your VALUES() attribute of the query string must match field for field. If you have three fields in your database, your query string must also show three fields. The field name shouldn't matter as you're not explicitly defining the field in the query string.

hth,
: )
Rich






:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 1st, 2003, 02:05 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Actually I missed Nik's post there... I thought natmaster was Nik... So I guess I could be wrong about the unquoted array in a string being first assumed a defined constant and then a string... (after reading Nik's post there) That happens when you don't quote an array in regular context, when not dealing with strings. Regardless I would stick with the curly brace syntax that Nik pointed out and just quote the array in that way! IMO.

If you can't get this resolved tell us how you have your database table set-up...

: )
Rich




:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 1st, 2003, 05:00 AM
Authorized User
 
Join Date: Jul 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx very much guesadilla.
By using curly braces I sorted it out.
 
Old August 1st, 2003, 02:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Usually, scalar string values inserted into database columns should be single-quoted. Only numerical values, other column names, and built-in functions should NOT be quoted.


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
form feed Character Handling in VB.net rupesh_india Classic ASP Basics 0 August 21st, 2006 02:35 AM
error handling Abhinav_jain_mca General .NET 5 December 21st, 2005 01:12 AM
Error handling ppenn Access VBA 3 September 2nd, 2005 06:58 AM
Error Handling evad Excel VBA 2 August 18th, 2005 03:54 AM
Error handling Hudson40 Access VBA 2 February 11th, 2005 05:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.