Php and Mysql, sending data from an HTML form

liunx

Guest
I am having problems getting my html form to send data to my Mysql database. I am completely new to this :) so any help would be appreciated.

Here's my php page that i am working on right now.


<html>
<body>
<?php
if ($submit) {
// process form
$db = mysql_connect("xxxxxxx", "xxxxxx","xxxxxx");
mysql_select_db("xxxxxx",$db);

$first=$_POST['first'];
$last= $_POST['last'];
$address= $_POST['address'];
$position= $_POST['position'];

$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
} else{
// display form
?>
<form method="post" action="<?php echo $PHP_SELF?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
} // end if
?>
</body>
</html>


I have replaced my database information with xxxxxx. This is following several tutorials that I have found on the internet.This should probably get scooted on over to the PHP Forum under the Server Side Programming Section of the site.

The PHP guys here are real good and knowledgeable about everything PHP and should take good care of ya.

As far as the code goes, the only thing that might be wrong is that your SQL statement should prolly have [brackets] around each of the field names for the table your inserting into.


$sql = "INSERT INTO employees ([first],[last],[address],[position]) VALUES ('$first','$last','$address','$position')";
$result = mysql_query($sql);


Can't imagine that the [brackets] would be a "show stopper" in this case, but they're a possibility.

The only other thing to always watch for is if the values you're trying to insert into the DB contain 'single quotes' then you will get errors when trying to insert them. I dont think PHP is any different than ASP in this regard, and you'll prolly want to replace all instances of 'single quotes' in your values with 2 'single quotes'.

Hope that helps, if not the PHP fellas will set me straight.so it is all your fault Putts :D

php doesn't like the [] around the field names. next time I won't be so nice :D :p LOLSee, but that's not an ASP thing.

That's a "proper SQL Statement" kinda thing to avoid field name ambiguity.

So, BAM!!!! I spit on your PHP no bracket-ness.Hmm...now I don't get embroiled in these Scoutt vs. Putts wars, but I have to say that the [ ] is NOT ANSI SQL compatible (which MySQL is, oh and it's not a PHP thing in any case). Column (or field) name disambiguity is not a problem in PHP because all variables start with a $ (except constants but that's for another day). In ASP this is not the case. My impression is that ASP has to compensate for the fact its variables have no distinguishing syntax by enclosing the non-variable column names in [ ].

Oh, and eLiteGoodGuy. Please do not cross-post! Thank you.Originally posted by putts
See, but that's not an ASP thing.

That's a "proper SQL Statement" kinda thing to avoid field name ambiguity.

So, BAM!!!! I spit on your PHP no bracket-ness.
to avoid ambiguity we just a.fieldname, b.filedname

still no PHP bracket-ness :D

stay away form the ASP dark side Putts, use the force LOLOkay people, I see my message is getting lost somewhere.

I think I used the wrong word with ambiguity. What I meant is, if you a have field name in your table like "First Name" and try to write a SQL statement like;

INSERT INTO TABLE (First Name) Values ('Bob')

Then you get errors. The fix for this is to use [brackets]:

INSERT INTO TABLE ([First Name]) Values ('Bob')

This isn't an ASP thing, it's standard SQL to avoid confusing the SQL server.

PHP's problem is that they use [brackets] to call data from collections and, for some ungodly reason, believe that that's the only way you can use [brackets] (At least, if what you guys say is true).

If you take the SQL Statement I have above, and type it into MS SQL Server's Query Analyzer, you'll get the following error:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'Name'.

BUT, if you [bracket] the "First Name" it will work perfectly. It's just a standard feature of all the SQL Servers that I deal with regularly.

That, my friends, is the Force.

BTW, can we change Torrent's nickname to Switzerland?well Putts,

in php we use back ticks to do that.

`field name`

and mysql is all fat dumb and happy, I spit on your ASP force :pThis is obviously SQL Server laughing in the face of ANSI compliant SQL (why should MS adhere to industry standard SQL when they can reinvent the wheel?) :D

This is valid syntax on all ANSI SQL RDBMS's:

INSERT INTO table VALUES (column1, column2);

No need for square-brackets, curly-brackets, masonic handshakes, or firstborn children.torrent, torrent, torrent....such hatred I sense in you.

MS SQL Server only demands them in multiple word column names. The following....

INSERT INTO TABLE (FIRSTNAME,LAST_NAME,MY_ONE_FIELD) VALUES ('Daffy','Duck','Funniest Cartoon Ever')

Works fine like that, but if you change the column from FIRSTNAME to FIRST NAME then you need the [brackets]....

INSERT INTO TABLE ([FIRST NAME],LAST_NAME,MY_ONE_FIELD) VALUES ('Daffy','Duck','Funniest Cartoon Ever')


I'll take that firstborn child now. :D<start darth vader breathing>
Then yoda Scoutt is correct with his earlier comment. MySQL uses ` to satisfy the needs of those who feel great need to include whitespace in a column name.
</end darth vader breathing>

Oh, and the way my first born is behaving at the moment (due to the humidity and heat) you are more than welcome to him. ;)i have problem sending data from form and to store into the database.. hmm but i think the submit button not functioning... coz whenever i submit it will not store into the variables.. weird..just start a new thread and explain what you have.
 
Top