Page 1 of 1

Simple Insert into database problem

Posted: Sat Jun 14, 2003 1:44 am
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.

Posted: Sat Jun 14, 2003 2:22 am
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. :)

Posted: Sat Jun 14, 2003 2:59 am
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

Posted: Sat Jun 14, 2003 4:10 am
by Paddy
Cheers. Guess it was blind leading the blind. :)

Posted: Sat Jun 14, 2003 11:38 am
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.

Posted: Sat Jun 14, 2003 1:58 pm
by stc7outlaw
this would be the correct code:

Code: Select all

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