如何将所有参数添加到表单中的.txt?

时间:2016-11-25 06:10:52

标签: php forms file fwrite file-handling

我的问题是我想将下面显示的所有值都写入txt文件。

我在行fwrite(...)时收到错误 - 它只能除3个参数外。

我做错了吗? 有一个更好的方法吗?

感谢您的帮助。

<?php
if(isset($_POST['submit']))
{
  $name = $_POST['name'];
  $gender = $_POST['gender'];
  $age = $_POST['age'];
  $personality = $_POST['personality'];
  $OS = $_POST['OS'];
  $age_min = $_POST['age_min'];
  $age_max = $_POST['age_max'];

  $file = fopen("singles.txt","a+");
  fwrite($file,$name,$gender,$age,$personality,$OS,$age_min,$age_max);
  fclose($file); 
  print_r(error_get_last());
}
?>

如果需要任何其他信息,我可以尝试帮忙!

2 个答案:

答案 0 :(得分:1)

函数fwrite希望你传递文件指针(在你的情况下是$ file)和你想要写的字符串。如果你想要一个以逗号分隔的列表,那么你应该这样做:

fwrite($file,$name . "," . $gender . "," . $age . "," . $personality . "," . $OS . "," . $age_min . "," . $ag);

http://php.net/manual/en/function.fwrite.php

http://php.net/manual/en/language.operators.string.php

答案 1 :(得分:0)

<?php
if(isset($_POST['submit']))
{
  $name = $_POST['name'];
  $gender = $_POST['gender'];
  $age = $_POST['age'];
  $personality = $_POST['personality'];
  $OS = $_POST['OS'];
  $age_min = $_POST['age_min'];
  $age_max = $_POST['age_max'];

   $variable_str = $name.",".$gender.",".$age.",".$personality.",".$os.",".$age_min.",".$age_max;

  $file = fopen("singles.txt","a+");
  fwrite($file,$variable_str);
  fclose($file); 
  print_r(error_get_last());
}
?>

您应该创建这些值的字符串,然后传递给fwrite()函数。有关fwrite()Link的详细信息,请点击此处。