Jump to content

Insert Problem


smilie

Recommended Posts

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

or

(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

Link to comment
https://forums.phpfreaks.com/topic/1556-insert-problem/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/1556-insert-problem/#findComment-5107
Share on other sites

$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.

or

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

if you must have \" in the INSERT statement (doubt that)

or

$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.

Link to comment
https://forums.phpfreaks.com/topic/1556-insert-problem/#findComment-5109
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.