我正在尝试将用户输入表单中的数据导入到我的wamp服务器中。我已尝试加载不同的方法和编码,但没有看到工作和提交数据。能帮助我吗?我只在网页设计和数据库上做了一个星期。
<?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)
?>
答案 0 :(得分:3)
正如我最初所说,你使用了错误的identifiers作为你的价值观,而是反击。它们应该是引号。
另外,你没有查询。
这是您需要做的事情:
确保所有表单元素都包含name属性和post方法;看到你没有发表你的表格,所以我需要指出这一点。
使用mysqli_query()
,你没用过的东西;它是执行查询所必需的。
$result = mysqli_query($dbhandle, $query)
or die(mysqli_error($dbhandle));
if($result){ echo "Success!"; }
else{ echo "Error."; }
现在,您已经定义了变量,为什么在查询中使用$_POST['var']
?只需使用变量。
将VALUES变量放在单引号中:
VALUES ('$Firstname','$Lastname', .....
并为其他人做同样的事。
清理您的输入:
$Firstname = stripslashes($_POST["Firstname"]);
$Firstname = mysqli_real_escape_string($dbhandle, $_POST["Firstname"]);
并为其他人做同样的事。
如果你得到一个&#34;未定义的索引......&#34;警告,这将是因为表单元素可能未命名,或包含拼写错误。信件很重要。
但是,我强烈建议您使用prepared statements或PDO with prepared statements,他们更安全。
将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告应仅在暂存时完成,而不是生产。
答案 1 :(得分:0)
你有一个if
语句没有做任何事情:
if($query) {
//more of your code
}
您希望使用以下内容实际执行该查询:
if ($result = $mysqli->query($query) {
//more of your code
}
答案 2 :(得分:0)
您也可以替换
$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`)");
与
$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.'")";
答案 3 :(得分:0)
$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`)";
应该是
$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."');";
然后你需要添加它:
$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!
}