I am getting no errors but the data is not going i

2019-09-20 18:24发布

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)

   ?>

标签: php html mysqli
4条回答
仙女界的扛把子
2楼-- · 2019-09-20 18:59

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.

$result = mysqli_query($dbhandle, $query) 
    or die(mysqli_error($dbhandle));

if($result){ echo "Success!"; }

  else{ echo "Error."; }

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:

VALUES ('$Firstname','$Lastname', .....

and do the same for the rest.

Sanitize your inputs:

$Firstname = stripslashes($_POST["Firstname"]);
$Firstname = mysqli_real_escape_string($dbhandle, $_POST["Firstname"]);

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.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

查看更多
萌系小妹纸
3楼-- · 2019-09-20 19:00
$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`)";

should be

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

and you need to add this afterwards:

$attempt = mysqli_query($dbhandle , $query);

# You will then need to check $attempt to see if it was true or not.
if($attempt) {
    # Query executed successfully
} else {
    # Whoops, it's failed!
}
查看更多
看我几分像从前
4楼-- · 2019-09-20 19:02

You have an if statement which doesn't do anything:

if($query) {
    //more of your code
}

You want to actually execute that query with something like:

if ($result = $mysqli->query($query) {
    //more of your code
}
查看更多
孤傲高冷的网名
5楼-- · 2019-09-20 19:15

You can also replace

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

with

$query = mysqli_query($dbhandle, "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.'")";
查看更多
登录 后发表回答