我尝试将数据从我保存在数组中的表单写入数据库。不幸的是它不起作用:
$hardCodedTitles = array(
array("Lead inserted by", $_POST["input_lead_insert"]),
array("Staffname", $_POST["input_Staffname"]),
array("Staff email", $_POST["input_Staffemail"]),
array("Workplace", $_POST["input_Workplace"]),
array("Gender", $_POST["input_gender"]),
array("First Name", $_POST["input_Firstname"]),
array("Last Name", $_POST["input_Lastname"]),
array("Company", $_POST["input_Company"]),
array("Job Position", $_POST["input_Jobposition"]),
array("Industry", $_POST["input_Industry"]),
array("Mailstop", $_POST["input_Mailstop"]),
array("Street", $_POST["input_Street"]),
array("Zip Code", $_POST["input_ZIP-Code"]),
array("City", $_POST["input_City"]),
array("Country", $_POST["input_Country"]),
array("Email", $_POST["input_E-Mail"]),
array("Phone Number", $_POST["input_PhoneNumber"]),
);
foreach($hardCodedTitles as $title) {
if(isset($_POST[$title[1]]) && $_POST[$title[1]] != "") {
mysql_query("INSERT INTO lead_data (lead_id, title, value) VALUES ($createdID, '".$title[0]."', '".$title[1]."')");
}
}
有人看到错误吗? 当我使用print_r()...
时,看起来数据正确存储在数组中答案 0 :(得分:2)
这将有效:
foreach($hardCodedTitles as $title) {
if(isset($title[1]) && $title[1] != "") {
mysql_query("INSERT INTO lead_data (lead_id, title, value) VALUES ($createdID, '".$title[0]."', '".$title[1]."')");
}
}
不要忘记清理该用户输入,以免留下被黑或破坏的数据库!
答案 1 :(得分:1)
看起来你正在处理$ _POST [$ _ POST ['something']]。尝试在第一个数组中的键周围取消$ _POST []
答案 2 :(得分:1)
您不需要检查$ _POST [$ title [1])是否已设置或不为空,因为它未设置且为空白!
相反,只需使用$ title [1],就像这样:
(编辑:以下代码供您学习/调试:))
$_POST["input_lead_insert"] = 1;
$_POST["input_Staffname"] = 2;
$_POST["input_Staffemail"] = 3;
$_POST["input_Workplace"] = 4;
$_POST["input_gender"] = 5;
$_POST["input_Firstname"] = 6;
$_POST["input_Lastname"] = 7;
$_POST["input_Company"] = 8;
$_POST["input_Jobposition"] = 9;
$_POST["input_Industry"] = 10;
$_POST["input_Mailstop"] = 11;
$_POST["input_Street"] = 12;
$_POST["input_ZIP-Code"] = 13;
$_POST["input_City"] = 14;
$_POST["input_Country"] = 15;
$_POST["input_E-Mail"] = 16;
$_POST["input_PhoneNumber"] = 17;
$hardCodedTitles = array(
array("Lead inserted by", $_POST["input_lead_insert"]),
array("Staffname", $_POST["input_Staffname"]),
array("Staff email", $_POST["input_Staffemail"]),
array("Workplace", $_POST["input_Workplace"]),
array("Gender", $_POST["input_gender"]),
array("First Name", $_POST["input_Firstname"]),
array("Last Name", $_POST["input_Lastname"]),
array("Company", $_POST["input_Company"]),
array("Job Position", $_POST["input_Jobposition"]),
array("Industry", $_POST["input_Industry"]),
array("Mailstop", $_POST["input_Mailstop"]),
array("Street", $_POST["input_Street"]),
array("Zip Code", $_POST["input_ZIP-Code"]),
array("City", $_POST["input_City"]),
array("Country", $_POST["input_Country"]),
array("Email", $_POST["input_E-Mail"]),
array("Phone Number", $_POST["input_PhoneNumber"]),
);
foreach($hardCodedTitles as $title) {
if(isset($title[1]) && $title[1] != "") {
// will only display the numbers for the purpose of your debugging
echo $title[1]."<br>";
}
}
答案 3 :(得分:0)
尝试在每个循环中使用print_r检查数组$ title,看看你是否得到了你想要的正确数组。
也许你可以试试
foreach($hardCodedTitles AS $title) {
foreach($title AS $key=>$value ) {
echo $key ." ". $value ."<br>";
}
}