Simple Insert into database problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
klycette
Forum Newbie
Posts: 9
Joined: Sat May 31, 2003 9:25 pm

Simple Insert into database problem

Post by klycette »

I'm very new to php and I'm trying to insert some information from a form into my database. Right now, my form handling script is on the same page as the form and I'd prefer to keep it that way. My problem is that when I run it, I always get the same error....that there's an undefined variable. I've cut the script down into it's most basic form and here it is.

Code: Select all

<?php

require_once('Connections/connJF.php');//connects to database

mysql_select_db($database, $conn) or die ("Could not open database.");

$sql = "INSERT INTO applicants (first_name) VALUES ('$first_name')";

?>
I think my problem might be what I should be putting as the form action; I know that it must be simple, but I'm not seeing it. The error message I'm getting is indicating that it's not recogizing "first_name" as a variable in my $sql line. This is the name of the field in the form however. Does it matter where this script is placed when it runs on the same page?

While I'm on the subject, what's the difference between using variables from forms as $variable versus the $HTTP_POST_VARS method? Thanks.
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

I can not answer your second question but I can answer your first.

In the form tag you need

Code: Select all

method="get"
or

Code: Select all

method="post"
As it it going back to the same page your first line of php code needs to be either

Code: Select all

$first_name = $_GET['first_name'];
or

Code: Select all

$first_name = $_POST['first_name'];
depending on wether you used the get or post method.

Your last line of code is also a bit off IMHO. You need to have a mysql_query in there. So you could have

Code: Select all

mysql_query($sql);
after the third line.

Note that your actual sql query is incorrect. It should be

Code: Select all

$sql = "INSERT INTO applicants (first_name) VALUES ('".$first_name."')";
This is done to concatenate the variables into the string. Hope this all helps. I think it might be right. :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Paddy wrote:Note that your actual sql query is incorrect. It should be

Code: Select all

$sql = "INSERT INTO applicants (first_name) VALUES ('".$first_name."')";
It's not incorrect - you can have variables within a double quoted string without concenating them. With syntax highlighting though, concentation can make finding your variables easier.

Mac
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Cheers. Guess it was blind leading the blind. :)
klycette
Forum Newbie
Posts: 9
Joined: Sat May 31, 2003 9:25 pm

Post by klycette »

I'll make those changes and see what happens. In the meantime, can someone tell me what the proper syntax is for the form action when the script that is handling the form is in the same file as the form itself? I think PHP_SELF is involved, but I'm not sure of the exact syntax. Thanks.
User avatar
stc7outlaw
Forum Newbie
Posts: 21
Joined: Mon Jun 09, 2003 9:36 pm

Post by stc7outlaw »

this would be the correct code:

Code: Select all

<form method="post" action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>">
Post Reply