从PHP将文本区域元素插入MySQL


Inserting textarea elements into MySQL from PHP

我需要以HTML形式从文本区域获取所有元素,并使用PHP将它们插入MySQL数据库。我设法将它们放入一个数组中,还找到了数组中元素的数量。然而,当我尝试在while循环中执行它时,它会在页面中继续显示单词"execution"(循环内)1000多次。

我不知道问题出在哪里,因为while循环是这个实例唯一适用的循环

$sent = $_REQUEST['EmpEmergencyNumbers'];
$data_array = explode("'n", $sent);
print_r($data_array);
$array_length = count($data_array);
echo $array_length;
while(count($data_array)){
echo "execution    ";  // This would be replaced by the SQL insert statement
}
you should use 
foreach($data_array as $array)
{
   //sql
}

当您访问php中提交的数据时,它将在$_GET或$_POST数组中可用,具体取决于您提交数据的方法(GET/POST)。避免使用$_REQUEST数组。而是使用$_GET/$_POST(取决于使用的方法)。

要循环遍历数组中的每个元素,可以使用foreach循环。

示例:

//...
foreach($data_array as $d)
{
  // now $d will contain the array element
  echo $d; // use $d to insert it into the db or do something
}

对数组使用foreach或递减计数,while循环是一个无限循环,如果计数>0