-1

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. 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?

            <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

    <?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>';
    }

}




?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shawn
  • 17
  • 3

1 Answers1

0

You are using commas $employee_id."\n", -- You need to be using periods (dots) $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");
Zak
  • 6,976
  • 2
  • 26
  • 48