Jump to content


Insert Problem

  • Please log in to reply
2 replies to this topic

#1 smilie

  • Members
  • Pip
  • Newbie
  • 1 posts
  • LocationColumbus, Oh

Posted 26 December 2003 - 02:33 PM

I\'m new to PHP/MySQl and i\'m trying to enter data into a db. It keeps giving me error\'s saying the form vaules are not defined.

Here is the PHP file:
<?php$host = "localhost";$username ="root";$password ="TheDBPassword";$database ="mydb";mysql_connect($host,$username,$password)  or die( "<br><center>Unable to connect to database server</center>");@mysql_select_db($database) or die("<br><center>Unable to select to database</center>");// Line 13 is the line below$query = "INSERT INTO employees VALUES ("$first","$last","$address","$position")";mysql_query($query);mysql_close()?>
Here is the Form File
<html><body><form action="test14.php" method="post">First: <input type="text" name="first"><p>Last: <input type="text" name="last"><p>Address: <input type="text" name="address"><p>Position: <input type="text" name="position"><p><input type="submit"><p></form></body></html>

The Error Messages i get:
(With quotes around value example: \"$first\")
Parse error: parse error in /Library/WebServer/Documents/test14.php on line 13
(Without quotes around value example: $first)
Notice: Undefined variable: first in /Library/WebServer/Documents/test14.php on line 13

Notice: Undefined variable: last in /Library/WebServer/Documents/test14.php on line 13

Notice: Undefined variable: address in /Library/WebServer/Documents/test14.php on line 13

Notice: Undefined variable: position in /Library/WebServer/Documents/test14.php on line 13

Anything will help,

Thanks K
---- K :)

#2 WhiteDevil

  • Members
  • Pip
  • Newbie
  • 2 posts
  • LocationUK

Posted 26 December 2003 - 02:37 PM

Unless register globals is switched on you will have to tell the processing form what the form variables were:

$firstname = $HTTP_POST_VARS[\"firstnamefield\"];
$lastname = $HTTP_POST_VARS[\"lastnamefield\"];

On your form use \"method=post\". This should hopefully write each value to a variable which can then be added to the form...

Any more problems, will try to help :D

#3 DaMaAn

  • Members
  • Pip
  • Newbie
  • 2 posts
  • LocationLink√∂ping, Sweden

Posted 26 December 2003 - 03:41 PM

$query = \\\"INSERT INTO employees VALUES (\\\"$first\\\",\\\"$last\\\",\\\"$address\\\",\\\"$position\\\")\\\";

Try to use \' around your insert statement OR escape all the rest of the \" used. Like this:
$query = \'INSERT INTO employees VALUES ("\'.$first.\'","\'.$last.\'","\'.$address.\'","\'.$position.\'")\';
This will not translate any PHP variables so they must be concatenated into the string.
$query = "INSERT INTO employees VALUES (/"$first/",/"$last/",/"$address/",/"$position/")";
if you must have \" in the INSERT statement (doubt that)
$query = "INSERT INTO employees VALUES (\'$first\',\'$last\',\'$address\',\'$position\')";
Will probably produce the correct result with the \"nicest\" code.
The problem is that you use the same quotes in the string as quoting the string.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users