Connecting Tech Pros Worldwide Help | Site Map

Insert into MySQL Database with PHP POST Method

Newbie
 
Join Date: Dec 2007
Location: PA
Posts: 2
#1: Dec 18 '07
Hi Everyone,

Here's my situation: I'm making a website for an employment agency, there is a page where potential employees can visit and "pre-register". When the submit button is hit the information is supposed to be stored in a MySQL database. Number one, when I hit submit nothing happens. I have to manually execute the PHP script. When the PHP script is executed it inserts a blank record into the database and I get my Thank You message. I realize my dilemma is pretty basic and has probably been answered several times, I have searched the forum and tried different pieces code but nothing seems to work for me. I've tried code using $_POST in front of the variables but it just leads to more errors and confusion. I'm using PHP5 and MySQL Server 5.0. Here is my HTML code:

<code>
<form action="AddApplicant.php" method="POST">
<font face="Times New Roman", size="3", color="151B8D">
<b>*First Name: <input type="text" name="firstname" id="firstname">
*Last Name: <input type="text" name="lastname" id="lastname"><br><Br>
*Address: <input type="text" name="address" id="address" size="25">
*City: <input type="text" name="city" id="city"><br><br>
*State: <input type="text" name="state" id="state" size="1">
*Zip Code: <input type="text" name="zipcode" id="zipcode" size="5">
*Phone: <input type="text" name="phono" id="phono"><br><br>
Alternate Phone: <input type="text" name="altphono" id="altphono">
E-mail: <input type="text" name="email" id="email"><br><br>
*Objective</b><br>Provide a brief description of the type of employment you are seeking.<br>
<Textarea name="objective" rows="5" cols="50" id="objective"></textarea><br><br>

<b>*Education</b><br>Select One<br>
<select name="education" "id="education">
<option value="High School Diploma">High School Diploma</option>
<option value="College Credits">College Credits</option>
<option value="Associates Degree">Associates Degree</option>
<option value="Bachelors Degree">Bachelors Degree</option>
<option value="Past Grad Creidts">Past Grad Credits</option>
<option value="Masters Degree">Masters Degree</option>
<option value="Other">Other</option></select><br><br>


<b>Additional Certifications, Seminars, & Licenses<br>
<Textarea name="certnsems" rows="3" cols="50" "id="certnsems"></textarea><br><br>

*Work Experience</b><br>
Describe your work experience or copy and paste your resume.
<Textarea name="workexp" rows="7" cols="50" id="workexp"></textarea><br><br>

<center><input type="submit" name="submit" value="Submit"></center><br><br>
</form>
</code>

Here is my PHP code:

<code>
<body>
<?php

$host="localhost";
$username="root";
$password="363market";
$db_name="svesapplicants";
$tbl_name="applicants";

global
$firstname, $lastname, $address, $city, $state, $zipcode, $phono, $altphono, $email, $objective, $education, $certnsems, $workexp;

mysql_connect("$host", "$username", "$password")or die("Cannot Connect to Server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="INSERT INTO $tbl_name(firstname, lastname, address, city, state, zipcode, phono, altphono, email, objective, education, certnsems, workexp)
VALUES('$firstname', '$lastname', '$address', '$city', '$state', '$zipcode', '$phono', '$altphono', '$email', '$objective', '$education', '$certnsems', '$workexp')";
$result=mysql_query($sql);


if($result){

echo "Thank You";
}
else {
echo "ERROR";
}

mysql_close();
?>

</body><html>
</code>

As you can tell I'm not an advanced programmer, I would truly appreciate any help or suggestions. :) Thanks in advance. ctrap
Newbie
 
Join Date: Jul 2007
Posts: 19
#2: Dec 18 '07

re: Insert into MySQL Database with PHP POST Method


Hi!

I think this is your problem.

Expand|Select|Wrap|Line Numbers
  1. global
  2. $firstname, $lastname, $address, $city, $state, $zipcode, $phono, $altphono, $email, $objective, $education, $certnsems, $workexp;
As far as I understand you think that $firstname will contain the value from the textbox 'firstname'. It doesn't work like this. Change the following code

Expand|Select|Wrap|Line Numbers
  1. VALUES('$firstname', '$lastname', '$address', '$city', '$state', '$zipcode', '$phono', '$altphono', '$email', '$objective', '$education', '$certnsems', '$workexp')";
... with this code ...
Expand|Select|Wrap|Line Numbers
  1. VALUES($_POST['firstname'], $_POST['lastname'], $_POST['address'], $_POST['city'], $_POST['state'], $_POST['zipcode'], $_POST['phono'], $_POST['altphono'], $_POST['email'], $_POST['objective'], $_POST['education'], $_POST['certnsems'], $_POST['workexp'])";
Good luck!
Newbie
 
Join Date: Dec 2007
Location: PA
Posts: 2
#3: Dec 18 '07

re: Insert into MySQL Database with PHP POST Method


Hi there,

Thanks so much for the reply, I have tried the code you suggested and it gives me this error (line 23 being the VALUES line):

PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\SVES\SVES Website\- on line 23

When I put the single quotes around the entire piece of code i.e. (I've seen examples of code in both formats):

'$_POST[firstname]' as opposed to $_POST['firstname']

I get this error message for each variable, firstname, lastname, address, and so on:

Thank You PHP Notice: Undefined index: firstname in C:\SVES\SVES Website\- on line 23 PHP

Still not sure what's going on. Thanks again!
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#4: Dec 19 '07

re: Insert into MySQL Database with PHP POST Method


First, you haven't seen this anywhere
Expand|Select|Wrap|Line Numbers
  1. '$_POST[firstname]' as opposed to $_POST['firstname']
It is better practice to copy POST values into variables and validate them.
[PHP]$firstname = $_POST['firstname'];
//Check firstname is valid etc[/PHP]Then insert using the variables.
You have got a syntax error because the POST variables are in an array, so cannot be parsed.
The same error can occur echoing out arrays.
The way around it is to use curly braces
Also text values need quotes around them when inserting.
I think this is your confusion.
So try this
[PHP]VALUES('{$_POST['firstname']}','{$_POST['lastname']}', '{$_POST['address']}'... etc";[/PHP]
Reply