简体   繁体   English

为什么我无法将数据保存到我的文本文件中?

[英]Why I can't save the data into my text file?

What I'm trying to do here is to create a form and the data that is inserted into the form will be saved into my employees.php file.我在这里尝试做的是创建一个表单,插入到表单中的数据将保存到我的employees.php文件中。 I can't seem to find the solution to solve this problem.我似乎无法找到解决此问题的方法。 Can I know what can I do to achieve this?我能知道我能做些什么来实现这一目标吗? Why do I get the error of fwrite() expects at most 3 parameters, 9 ?为什么我得到fwrite() expects at most 3 parameters, 9的错误, fwrite() expects at most 3 parameters, 9

            <form action="employeeprocess.php" method="POST" enctype="multipart/form-data">
              <div class="row">
                <div class="col-md-12">
                  <div class="form-group">
                    <label>Name</label>
                    <input type="text" class="form-control" placeholder="Employee Name" name="employee_name" required>
                  </div>
                </div>

                <div class="col-md-12">
                  <div class="form-group">
                    <label>Gender</label>
                    <br>
                    <input type="radio" name="employee_gender" value="male" id="male" required><label for="male">Male</label>
                    <input type="radio" name="employee_gender" value="female" id="female" required><label for="female">Female</label>
                  </div>
                </div>

                <div class="col-md-12">
                  <div class="form-group">
                    <label>Address</label>
                    <textarea class="form-control textarea" name="employee_address" required></textarea>
                  </div>
                </div>
              
                <div class="col-md-12">
                  <div class="form-group">
                    <label>Email address</label>
                    <input type="text" class="form-control" placeholder="Email Address" name="employee_email" required>
                  </div>
                </div>
                <div class="col-md-12">
                  <div class="form-group">
                    <label>Date of Birth</label>
                    <input type="date" class="form-control" placeholder=" Employee Birthday" name="employee_birthday" required>
                  </div>
                </div>
            

                <div class="card-body">
              <h1>Job Information</h1>
              <div class="row">
                <div class="col-md-12">
                  <div class="form-group">
                    <label>Staff ID</label>
                    <input type="text" class="form-control" placeholder="SID0001F" name="employee_id" required>
                  </div>
                </div>
               

                  <div class="col-md-12">
                  <div class="form-group">
                    <label>Department</label>
                    <select class="form-control" name="employee_department" required>
                      <option value="">Select Department</option>
                      <option value="itsupport">IT Support</option>
                      <option value="finance">Finance</option>
                      <option value="sales">Sales</option>
                      <option value="operation">Operation</option>
                      <option value="marketing">Marketing</option>
                    </select>
                  </div>
                </div>

                <div class="col-md-12">
                  <div class="form-group">
                    <label>Position</label>
                    <br>
                    <input type="radio" name="employee_position" value="fulltime" id="fulltime" required> <label for="fulltime">Full Time</label>
                    <input type="radio" name="employee_position" value="parttime" id="parttime" required> <label for="parttime">Part Time</label>
                    <input type="radio" name="employee_position" value="contract" id="contract" required> <label for="contract">Contract</label>
                  </div>
                </div>
              </div>
              

              <div class="row">
                <div class="update ml-auto mr-auto">
                  <button type="submit" class="btn btn-primary btn-round" name="add_employee">Submit</button>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

</div>

This is my employeeprocess.php这是我的employeeprocess.php

    <?php

if (isset($_POST['add_employee'])){
    $employee_name = $_POST['employee_name'];
    $employee_gender = $_POST['employee_gender'];
    $employee_address = $_POST['employee_address'];
    $employee_email = $_POST['employee_email'];
    $employee_birthday = $_POST['employee_birthday'];
    $employee_id = $_POST['employee_id'];
    $employee_department = $_POST['employee_department'];
    $employee_position = $_POST['employee_position'];


    if(!empty ($employee_name && $employee_gender && $employee_address && $employee_email && $employee_birthday && $employee_id && $employee_department && $employee_position)){
        $file = fopen('data/employees.txt', 'a');
        fwrite($file, 
        $employee_name."\n",
        $employee_gender."\n",
        $employee_address."\n",
        $employee_email."\n",
        $employee_birthday."\n",
        $employee_id."\n",
        $employee_department."\n",
        $employee_position."\n");

        $read = file('data/employees.txt');
        foreach($read as $aread ){
            echo $aread . "|";
        }
    echo '<script>alert("Data Insert Successfully ")</script>';



    }else{
        echo '<script>alert("Something is wrong! Please try again ")</script>';
    }

}




?>

You are using commas $employee_id."\\n", -- You need to be using periods (dots) $employee_id . "\\n" .您正在使用逗号$employee_id."\\n", -- 您需要使用句点(点) $employee_id . "\\n" . $employee_id . "\\n" . :

        fwrite($file . 
        $employee_name . "\n" .
        $employee_gender . "\n" .
        $employee_address . "\n" .
        $employee_email . "\n" .
        $employee_birthday . "\n" .
        $employee_id . "\n" .
        $employee_department . "\n" .
        $employee_position . "\n");

A better way write this would be this way, since there is no need for concatenation with double quotes:更好的写法是这样,因为不需要用双引号连接:

        fwrite("$file \n
        $employee_name \n
        $employee_gender \n
        $employee_address \n
        $employee_email \n
        $employee_birthday \n
        $employee_id \n
        $employee_department \n
        $employee_position \n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM