I am currently working on a simple survey system for educational purposes. I know it is subject to SQL Injection, but I am new to PHP/MySQL and wanted to learn something basic so I have something to build upon later.
Basically, I have an HTML form that processes the PHP on the same page:
<form action="" method="post">
Please enter your first name: <input type="text" name="first_name" size="40" /><br /><br />
Please enter your last name: <input type="text" name="last_name" size="40" /><br /><br />
A number where you can be reached: <input type="text" name="phone" size="15" /><br /><br />
Was your technical issue resolved? <select name="resolved"><option selected="selected" value="">Select One...</option><option value="yes">Yes</option><option value="no">No</option></select><br /><br />
On a scale of 1 - 5, was your technician knowledgeable?<br /> <select name="knowledge"><option selected="selected" value="">Select One...</option><option value="1">1 - The technician was not knowledgeable at all</option><option value="2">2 - The technician was somewhat knowledgeable</option><option value="3">3 - The technician met my standards</option><option value="4">4 - The technician was knowledgeable</option><option value="5">5 - The technician was very knowledgeable</option></select><br /><br />
On a scale of 1 - 5, was your technician friendly?<br /><select name="friendly"> <option selected="selected" value="">Select One...</option><option value="1">1 - The technician was not friendly at all</option><option value="2">2 - The technician was somewhat friendly</option><option value="3">3 - The technician met my standards</option><option value="4">4 - The technician was friendly</option><option value="5">5 - The technician was very friendly</option></select><br /><br />
On a scale of 1 - 5, was your issue quickly resolved?<br /> <select name="quickness"> <option selected="selected" value="">Select One...</option><option value="1">1 - The issue was not resolved quickly at all</option><option value="2">2 - The issue was resolved somewhat quickly</option><option value="3">3 - The issue resolution time met my standards</option><option value="4">4 - The issue was resolved quickly</option><option value="5">5 - The issue was resolved very quickly</option></select><br /><br />
Would you recommend our company to a friend or relative? <select name="referral"><option selected="selected" value="">Select One...</option><option value="yes">Yes</option><option value="no">No</option></select><br /><br />
Do you have any comments, compliments, suggestions, or complaints about your service today?<br /> <textarea name="comments" rows="15" cols="30"></textarea><br />
<input type="submit" value="Send Survey" />
</form>
And I would like to capture the form data and record it into my MySQL database. As of now, the text fields insert without any problems. However, the selected dropdown value does not insert. It doesn't insert anything (a blank record) where the dropdown items are (as well as my comments textbox).
My PHP is as follows:
<?php
error_reporting(0);
$con = mysql_connect("localhost","xxxxxxxxx","xxxxxxxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db ("survey", $con);
$sql="INSERT INTO survey (first_name, last_name, phone, resolved, knowledge, friendly, quickness, referral, comments)
VALUES
('$_POST[first_name]','$_POST[last_name]','$_POST[phone]','$POST[resolved]','$POST[knowledge]','$POST[friendly]','$POST[quickness]','$POST[referral]','$POST[comments]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
I have error reporting turned off because I'm using a Joomla template that conflicts with one of my modules.
My database is setup with the following columns:
- id - Primary - Auto Increment
- date - Timestamp - CurrentTimestamp
- first_name - Text
- last_name - Text
- phone - Text
- resolved - Text
- knowledge - Text
- friendly - Text
- quickness - Text
- referral - Text
- comments - Text
The id and date fields do what they are supposed to, and the text fields (first name, last name, and phone) all record with no problems. The problem seems to be with the dropdown lists, and the textarea box.
Am I missing something in my PHP in order to capture the values from the dropdown options and comments textarea? I tried removing the selected value of " " from the html but it still records empty records.
Any advice is greatly appreciated! I'm a pretty novice programmer (if I can call myself that for lack of better words), but am learning. Thank you for taking a look.