By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

Insert into MySQL Database with PHP POST Method

P: 2
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:

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

Here is my PHP code:



$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')";


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



As you can tell I'm not an advanced programmer, I would truly appreciate any help or suggestions. :) Thanks in advance. ctrap
Dec 18 '07 #1
Share this Question
Share on Google+
3 Replies

P: 23

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!
Dec 18 '07 #2

P: 2
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!
Dec 18 '07 #3

code green
Expert 100+
P: 1,726
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]
Dec 19 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.