I am trying to get data from a user input form into my wamp server. I have tried loads off different methods and coding but none seen to work and submit the data. Can you please help me I am have only done a week on web design and database.
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "project";
//connection to the server
$dbhandle = mysqli_connect($hostname, $username, $password)
or die ("Unable to connect to server<br>");
echo "Connected to server<br>";
//connection to the database
$select_db = mysqli_select_db($dbhandle, $database)
or die ("Could not select Project database");
echo "connected to database";
//Getting the Values from the form
$Firstname = $_POST["Firstname"];
$Lastname = $_POST["Lastname"];
$DOB = $_POST["DOB"];
$Address = $_POST["Address"];
$Telephone_No = $_POST["Telephone_No"];
$NOK = $_POST["NOK"];
$NOK_Address = $_POST["NOK_Address"];
$vetting_date = $_POST["vetting_date"];
$Clearance_Expiry = $_POST["Clearance_Expiry"];
$Current_Employer = $_POST["Current_Employer"];
$Resubmission = $_POST["Resubmission"];
$Number_of_Attempts = isset($_POST["Number_of_Attempts"]) ? $_POST["Number_of_Attempts"] : 1;
$Qualification1 = $_POST["Qual_1"];
$date_completed_1= $_POST["date_completed_1"];
$Expiry_date_1 = $_POST["Run_out_date_1"];
$Qualification2 = $_POST["Qual_2"];
$date_completed_2 = $_POST["date_completed_2"];
$Expiry_date_2 = $_POST["Run_out_date_2"];
$Qualification3 = $_POST["Qual_3"];
$date_completed_3 = $_POST["date_completed_3"];
$Expiry_date_3 = $_POST["Run_out_date_3"];
$Qualification4 = $_POST["Qual_4"];
$date_completed_4 = $_POST["date_completed_4"];
$Expiry_date_4 = $_POST["Run_out_date_4"];
$Qualification5 = $_POST["Qual_5"];
$date_completed_5 = $_POST["date_completed_5"];
$Expiry_date_5 = $_POST["Run_out_date_5"];
$Qualification6 = $_POST["Qual_6"];
$date_completed_6 = $_POST["date_completed_6"];
$Expiry_date_6 = $_POST["Run_out_date_6"];
$Qualification7 = $_POST["Qual_7"];
$date_completed_7 = $_POST["date_completed_7"];
$Expiry_date_7 = $_POST["Run_out_date_7"];
$Qualification8 = $_POST["Qual_8"];
$date_completed_8 = $_POST["date_completed_8"];
$Expiry_date_8 = $_POST["Run_out_date_8"];
$Qualification9 = $_POST["Qual_9"];
$date_completed_9 = $_POST["date_completed_9"];
$Expiry_date_9 = $_POST["Run_out_date_9"];
$Qualification10 = $_POST["Qual_10"];
$date_completed_10 = $_POST["date_completed_10"];
$Expiry_date_10 = $_POST["Run_out_date_10"];
$why = $_POST["why"];
//inserting to the database
$query = "INSERT INTO `applicant` (`Firstname`, `Lastname`, `DOB`, `Address`, `Telephone_No`, `NOK`, `NOK_Address`, `vetting_date`, `Clearance_Expiry`, `Current_Employer`, `Resubmission`, `Number_of_Attempts`, `Why`)
VALUES (`$_POST[Firstname]`,`$_POST[Lastname]`,`$_POST[DOB]`,`$_POST[Address]`,`$_POST[Telephone_No]`,`$NOK`,`$NOK_Address`,`$vetting_date`,`$Clearance_Expiry`,`$Current_Employer`,`$Resubmission`,`$Number_of_Attempts`,`$why`)";
//$sql = "INSERT INTO qualification_link (date_completed) VALUES (`$date_completed_1`), (Run_out_Date) VALUES (`$Expiry_date_1`), (date_completed) VALUES (`$date_completed_2`), (Run_out_Date) VALUES (`$Expiry_date_2`), (date_completed) VALUES (`$date_completed_3`), (Run_out_Date) VALUES (`$Expiry_date_3`), (date_completed) VALUES (`$date_completed_4`), (Run_out_Date) VALUES (`$Expiry_date_4`), (date_completed) VALUES (`$date_completed_5`), (Run_out_Date) VALUES (`$Expiry_date_5`), (date_completed) VALUES (`$date_completed_6`), (Run_out_Date) VALUES (`$Expiry_date_6`), (date_completed) VALUES (`$date_completed_7`), (Run_out_Date) VALUES (`$Expiry_date_7`), (date_completed) VALUES (`$date_completed_8`), (Run_out_Date) VALUES (`$Expiry_date_8`), (date_completed) VALUES (`$date_completed_9`), (Run_out_Date) VALUES (`$Expiry_date_9`), (date_completed) VALUES (`$date_completed_10`), (Run_out_Date) VALUES (`$Expiry_date_10`)";
// $sql = "INSERT INTO qualification (Qual_1) VALUES (`$Qualification1`), (Qual_2) VALUES (`$Qualification2`), (Qual_3) VALUES (`$Qualification3`), (Qual_4) VALUES (`$Qualification4`), (Qual_5) VALUES (`$Qualification5`), (Qual_6) VALUES (`$Qualification6`), (Qual_7) VALUES (`$Qualification7`), (Qual_8) VALUES (`$Qualification8`), (Qual_9) VALUES (`$Qualification9`), (Qual_10) VALUES (`$Qualification10`)";
// successfully insert data into database, displays message "Successful".
if($query){
echo "Successful";
echo "<BR>";
echo "<a href=index.php>Back to Home page</a>";
}
else {
echo "Data not Submitted";
}
//closing the connection
mysqli_close($dbhandle)
?>
As I originally stated, you're using the wrong identifiers for your VALUES, being backticks. They should be quotes.
Plus, you're not querying.
Here's what you need to do:
Make sure that all your form elements contain a name attribute and a post method; seeing that you did not post your form, so I need to point that out.
Use
mysqli_query()
, something that you didn't use; it's required to execute the query.Now, you've already defined your variables, so why use
$_POST['var']
inside your query? Just use the variables.Place your VALUES variables in single quotes:
and do the same for the rest.
Sanitize your inputs:
and do the same for the rest.
If you get an "Undefined index..." warning, it will be because a form element may either not be named, or contains a typo. Letter-case is important.
However, I highly suggest that you use prepared statements, or PDO with prepared statements, they're much safer.
Add error reporting to the top of your file(s) which will help find errors.
Sidenote: Error reporting should only be done in staging, and never production.
should be
and you need to add this afterwards:
You have an
if
statement which doesn't do anything:You want to actually execute that query with something like:
You can also replace
with