Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Notice: Use of undefined constant" Error

Status
Not open for further replies.

dpdoug

Programmer
Joined
Nov 27, 2002
Messages
455
Location
US
I've got an html page with an input form on it.
I submit the info on the form with a call to insert.php in which I request the values with the $_POST like this:

<?PHP

$first = $_POST[first];
$last = $_POST[last];
$city = $_POST[city];
$state = $_POST[state];
$zip = $_POST[zip];
$phone = $_POST[phone];

$db = mysql_connect(&quot;Dev-1&quot;, &quot;root&quot;, &quot;&quot;);
mysql_select_db(&quot;db1&quot;, $db);
$sql = &quot;Insert into table1 (First, Last, City, State, zip, Phone)
Values ($first, $last, $city, $state, $zip, $phone)&quot;;
$result = mysql_query($sql, $db);
?>

The idea, obviously, is to add a record to a table in the database.

The problem is when I run it, I get these err messages:

Notice: Use of undefined constant first - assumed 'first' in C:\phptest\insert.php on line 15

Notice: Use of undefined constant last - assumed 'last' in C:\phptest\insert.php on line 16

Notice: Use of undefined constant city - assumed 'city' in C:\phptest\insert.php on line 17

Notice: Use of undefined constant state - assumed 'state' in C:\phptest\insert.php on line 18

Notice: Use of undefined constant zip - assumed 'zip' in C:\phptest\insert.php on line 19

Notice: Use of undefined constant phone - assumed 'phone' in C:\phptest\insert.php on line 20

Can anyone tell me what I'm doing wrong?

David
 
Your assignments should be in the form of
[tt]$variable = $_POST['variable'];[/tt]

//Daniel
 
danielhozac is right:

An index in an associative array should be quoted.
You can see many examples where people don't quote, but what happens then is that the constant is replaced with an assumes string value if it does not exist.

Your PHP installation's warning level is set low, so you get these warnings. The best way is to quote - especially for clarity.

What if you or the PHP folks introduce a new constant called first someday?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top