我正在用foreach生成几个动态输入。每个输入都通过循环遍历文本文件中的数组而获得其name
和id
。
然后将表单数据发送到另一个PHP页面,以使用POST执行一些数据库查询。
我面临的问题是每个输入值都返回NULL。
我不知道发生了什么,因为当我在“网络”选项卡上的Web控制台中查看时,可以看到正在收集参数。
array.txt
first_name
last_name
occupation
company_name
industry
city
country
countryCode
phone
email
address
stateProvince
postalZipeCode
form.php
//get array of names and ids for inputs
$array = explode("\n", file_get_contents('array.txt'));
//loop through the array
echo '<form method="POST" action="insert.php">';
foreach($array as $input) {
echo '<label>'.$input.'</label>'
. '<input type="text" id="'.$input.'" name="'.$input.'" required="required" class="form-control" placeholder="'.$input.'">';
echo '<br>';
}
echo '<input type="submit" value="Submit">';
echo '</form>';
网络标签(网络控制台)
insert.php
if (!empty($_POST)) {
//get variables
if (isset($_POST['first_name'])) {
$first_name= $_POST['first_name']; //1.
}
if (isset($_POST['last_name'])){
$last_name=$_POST['last_name']; //2.
}
if (isset($_POST['occupation'])) {
$occupation=$_POST['occupation']; //3.
}
if (isset($_POST['company_name'])) {
$company_name=$_POST['company_name']; //4
}
if (isset($_POST['industry'])){
$industry = $_POST['industry']; //5
}
if (isset($_POST['city'])) {
$city = $_POST['city']; //6
}
if(isset($_POST['country'])){
$country=$_POST['country']; //7
}
if (isset($_POST['countryCode'])) {
$countryCode = $_POST['countryCode']; //8
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone']; //9
}
if (isset($_POST['email'])) {
$email = $_POST['email']; //10
}
if (isset($_POST['address'])) {
$address = $_POST['address']; //11
}
if (isset($_POST['stateProvince'])) {
$stateProvince = $_POST['stateProvince']; //12
}
if (isset($_POST['postalZipeCode'])) {
$postalZipeCode = $_POST['postalZipeCode']; //13
}
// insert into table
$insertProspectQuery = $conn->prepare("INSERT INTO users (first_name, last_name, occupation, company,industry,city,country,countryCode,phone,email,address,stateProvince,postalZipeCode) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
$insertProspectQuery->bind_param('sssssssssssss',$first_name,$last_name,$occupation,$company_name,$industry,$city,$country,$countryCode,$phone,$email,$address,$stateProvince,$postalZipeCode);
$insertProspectQuery->execute();
$insertProspectQuery->close();
$ok = 1;
} else {
//handle error
}
答案 0 :(得分:1)
通过按照MonkeyZeus的建议通过使用var_dump输出变量,我发现问题是在每个名称变量的末尾都有空白。
这是var_dump的简短摘要:
new String[0]
。
我不知道是什么在名称变量中创建了空格,这很可能是因为变量是从文本文件创建的,而文本文件在每一行的末尾添加了空格。
因此,由于它应该是[“ first_name”]而不是[[first_name“],所以array(19) { ["first_name "]=> string(6) "John" ["last_name "]=> string(8) "Doe"[.......]
无法识别发布在$_POST
上的变量。
在这种错误的用例中,这绝不会运行:
insert.php
因为if(isset($_POST["first_name"])){
$first_name = $_POST["first_name"];
}
我通过在foreach中使用名称变量之前修剪它们来解决了这个问题。
解决方案快速修整:
form.php
["first_name"] != ["first_name "]
答案 1 :(得分:1)
问题是您的foreach循环中的$input
包含了一个可能从array.txt
文件中获取的空间。
为避免将来出现问题,您只需修剪$input
:
foreach($array as $input) {
$input = trim($input);
// Proceed as normal
}
您可以选择尝试修复array.txt
中的数据,但是如果以后出现错误,则您的应用将再次停止工作,因此最好对从您的检索到的每个项应用trim()
文件。