我如何保存一个文件.txt与php?


<?php
 
 $fp = fopen("users.txt", "w");
if(!$fp) die ("Errore nella creazione dell'utente");
$fp=fwrite($email $password);
fclose($fp);
 $name="";
 $surname="";
 $email="";
 $password="";
 $nazionalita="";
 $telefono="";
 $errors= array();
 
 if($_SERVER["REQUEST_METHOD"]=="POST"){
     $name = htmlspecialchars($_POST["name"]);
     $surname = htmlspecialchars($_POST["surname"]);
     $email = htmlspecialchars($_POST["email"]);
     $password = htmlspecialchars($_POST["password"]);
     $nazionalita = htmlspecialchars($_POST["nazionalita"]);
     $telefono = = htmlspecialchars($_POST["nazionalita"]);
  }
     
 
     if(empty($name)) {
       $errors[] = "Name is required!";
   }
 
   if(empty($surname)) {
       $errors[] = "Surname is required!";
   }
 
   if(empty($email)) {
       $errors[] = "Email is required!";
   }
   if(empty($password)) {
    $errors[] = "Password is required!";
}
         
 header("location: index.php");
?>

当然,这行不通。

  1. 如何用PHP保存到特定位置?然后我就得爆炸了,只会得到我的电子邮件和密码。

谢谢。

我不打算为你修改你的代码,但给你三个提示:

  1. 像电脑一样思考。计算机将按照你编写的顺序,一次执行一条语句;因此,如果您有一行将变量写入文件,则需要在之后的行中添加值到该变量中。它还会完全按照你的要求执行,而不是猜测你的意思,也不会让你摆脱拼写错误;仔细阅读你的代码
  2. 使用PHP手册如果您输入https://php.net/,后面跟着一个内置函数的名称,比如https://php.net/fwrite,您将得到一个页面,其中解释了该函数的输入和输出。通常,有一些示例展示了使用该函数的各种方法,您可以将其作为编写自己代码的灵感。
  3. 打开display_errors设置,或者了解日志文件的位置。您所展示的代码中的大多数错误将导致错误或警告,告诉您需要查看哪一行。仔细阅读这些信息,它们通常会指出你做错了什么。

如果你想写入文件,你可以输入$fpw = fwrite($fp,$email.' : '.$password);

您应该将$fp传递给fwrite()方法,如下所示

$fp = fopen('user.txt', 'w+') or die('Unable to open file!');
$fp=fwrite($fp, $email.'~'.$password.'n'); // make sure to choose whatever seperator suits your need and be careful when choosing it
fclose($fp);

谢谢大家,我修复了我的代码!!遵循正确的代码!

<?php

 $name="";
 $surname="";
 $email="";
 $password="";
 $nazionalita="";
 $telefono="";
 $errors= array();
 
 //$fp = fopen("users.txt", "w");
//$fpw = fwrite($fp,$dati $email.' : '.$password);
//fclose($fp);

 
 if(isset($_POST["submitPut"])){
     $name = htmlspecialchars($_POST["name"]);
     $surname = htmlspecialchars($_POST["surname"]);
     $email = htmlspecialchars($_POST["email"]);
     $password = htmlspecialchars($_POST["password"]);
     $nazionalita = htmlspecialchars($_POST["nazionalita"]);
     $telefono = htmlspecialchars($_POST["telefono"]);
 
     $dati= "n". $name . ",". $surname .",".$email.",". $password . ",". $nazionalita. ",". $telefono;
    $file= fopen("users.txt", "a+");
    $credenziali=fwrite($file, $dati);
    fclose($file);
     
 
     if(empty($name)) {
       $errors[] = "Name is required!";
   }
 
   if(empty($surname)) {
       $errors[] = "Surname is required!";
   }
 
   if(empty($email)) {
       $errors[] = "Email is required!";
   }
   if(empty($password)) {
    $errors[] = "Password is required!";
}
}
         
       
 //header("location: index.php");
 ?>
<!DOCTYPE html>
<html lang="en">
<?php include_once "views/head.php" ?>
<body>
<?php include_once "views/navbar.php" ?>
<main>
<div class="container my-5">
    <h2>Registration user</h2>
<form action="registration.php" method="post">
          <div class="form-group">
          <label for="name">Name:</label>
          <input type="text" class="form-control" id="name" name="name" placeholder="Enter name" >
</div>
        
    <div class="form-group">
          <label for="surname">surname:</label>
          <input type="text" class="form-control" id="surname" name="surname" placeholder="Enter surname" >
          </div>
    <div class="form-group">
          <label for="email">email:</label>
          <input type="email" class="form-control" id="email" name="email" placeholder="Enter email" >
          </div>
     <div class="form-group">
          <label for="password">password:</label>
          <input type="text" class="form-control" id="password" name="password" placeholder="Enter password">
          </div>
     <div class="form-group">
          <label for="nazionalita">nazionalita:</label>
          <input type="text" class="form-control" id="nazionalita" name="nazionalita" placeholder="Enter nazionality" >
          </div>
     <div class="form-group">
          <label for="telefono">telefono:</label>
          <input type="text" class="form-control" id="telefono" name="telefono" placeholder="Enter phone" >
          </div>
          <button type="submit"name="submitPut" class="btn btn-primary">Submit</button>

</form>
</div>
</main>
<?php include_once "views/footer.php" ?>
  
<?php include_once "views/scripts.php" ?>
</body>
</html>

最新更新