Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two PHP files; employees.php, and profile.php. For employees.php a table is displayed with a delete button beside each record, and for profile.php there is a save button at the bottom. The problem is the same for both files, the button doesn't work. Both the delete button and the save button are in form in their files.

**The records are in different tables;
employees.php --> employees table
profile.php --> customer table

delete button(employees.php) - To delete the record
save button(profile.php) - To update the record

employees.php
<?PHP include('process/employee_process.php'); ?>

<html>
  <head>
    <link href="CSS/style.css" rel="stylesheet">
  </head>
  
  <body background="images/background-2.jpg">
    <div>
      <div class="position">
        <h2> Add a new employee</h2>
        <!--Form to add employee-->
        <form name="add_employee" method="post" action="employees.php">
          <table>
            <tr style="height:70px">
            <td>
              ID:<br>
              <input class="textbox" type="text" name="id">
            </td>
              <td>
                Name:<br>
                <input class="textbox" type="text" name="name">
              </td>
              <td>
                Phone Number:<br>
                <input class="textbox" type="text" name="phone_number">
              </td>
            </tr>
            <tr style="height:70px">
              <td>
                Date Employed:<br>
                <input class="textbox" type="date" name="start_date">
              </td>
              <td>
                Date Unemployed:<br>
                <input class="textbox" type="date" name="end_date">
              </td>
            </tr>
            <tr style="height:70px">
              <td>
                Position:<br>
                <input class="textbox" type="text" name="position">
              </td>
              <td>
                Salary:<br>
                <input class="textbox" type="text" name="salary">
              </td>
            </tr>
          </table>
          <br>
          <button class="button" type="submit" name="add"> Add</button>
        </form>
        <br><br>
        
        <h2> Employees</h2>
        <!-- display employee records -->
        <?PHP
        //Connecct to database
        $conn = mysqli_connect("localhost","root","","kz_komputer");
        if(!$conn){
          die("Cannot connect: " . mysqli_error());
        }
        $sql = "SELECT * FROM employees";
        $data = mysqli_query($conn,$sql);
        
        echo "<table width=900px>
        <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Phone Number</th>
        <th>Date Employed</th>
        <th>Date Unemployed</th>
        <th>Position</th>
        <th>Salary</th>
        </tr>";
        while($record = mysqli_fetch_array($data)){
          echo "<form>";
          echo "<tr align=center height=40px>";
          echo "<td>" . $record['id'] . "</td>";
          echo "<td>" . $record['name'] . "</td>";
          echo "<td>" . $record['phone_number'] . "</td>";
          echo "<td>" . $record['date_employed'] . "</td>";
          echo "<td>" . $record['date_unemployed'] . "</td>";
          echo "<td>" . $record['position'] . "</td>";
          echo "<td>" . $record['salary'] . "</td>";
          echo "<td>" . "<input type='submit' name='delete' value='Delete'>" . "</td>";
          echo "</tr>";
          echo "</form>";
        }
        echo "</table>";
        mysqli_close($conn);
        ?>
      </div>
    </div>
  </body>
</html>


employees_process.php
<?PHP
  error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
  
  $id = "";
  $name = "";
  $number = "";
  $employed = "";
  $unemployed = "";
  $position = "";
  $salary = "";
  
  //connect to database
  $connect = new mysqli("localhost","root","","kz_komputer");
  
  /*-----add employee records-----*/
  if(isset($_POST['add'])){
    //get inputs
    $id = $_POST['id'];
    $name = $_POST['name'];
    $number = $_POST['phone_number'];
    $employed = $_POST['start_date'];
    $unemployed = $_POST['end_date'];
    $position = $_POST['position'];
    $salary = $_POST['salary'];
    
    if($connect){
      $sql = $connect->prepare("INSERT INTO employees(id,name,phone_number,date_employed,date_unemployed,position,salary)VALUES('$id','$name','$number','$employed','$unemployed','$position','$salary')");
      $sql->execute();
      header("location: employees.php");
    }
  }
  
  /*------delete employee records-----*/
  if(isset($_POST['delete'])){
    $id = $_POST['id'];
    
    $delete = "DELETE FROM employees WHERE id = '$id'";
    mysqli_query($connect,$delete);
    header("location: employees.php");
  }
?>


profile.php
<?PHP include("process/profile_process.php")?>
<html>
  <head>
    <link href="CSS/style.css" rel="stylesheet">
  </head>
  
  <body background="images/background.jpg">
    <div id="wrapper">
      <div style="margin:100px 0 0 80px">
      <h2>Profile</h2>
        <!--Display error message-->
        <?PHP include('error.php'); ?>
        <?PHP
        //connect to database
        $conn = mysqli_connect("localhost","root","","kz_komputer");
        if(!$conn){
          die("Cannot connect: " . mysqli_error());
        }
        
        $sql = "SELECT * FROM customer";
        $data = mysqli_query($conn,$sql);
        $record = mysqli_fetch_array($data);
        
        //display records from database
        echo "<form name='profile_form' method='post' action='profile.php'>";
        echo "<table>";
        echo "<tr>";
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Username: <br> <input class='textbox' type='text' name='user' value='" . $record['username'] . "' </td>";
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Email: <br> <input class='textbox' type='text' name='email' value='" . $record['email'] . "' </td>";
        echo "</tr>";     
        echo "<tr>";
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Name: <br> <input class='textbox' type='text' name='name' value='" . $record['name'] . "' </td>";
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Phone Number: <br> <input class='textbox' type='text' name='phone' value='" . $record['phone_number'] . "' </td>";
        echo "</tr>";        
        echo "<tr>";
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Home Address: <br> <input class='textbox' type='text' name='address' value='" .  $record['home_address']  . "' </td>"; 
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Postcode: <br> <input class='textbox' type='text' name='postcode' value='" .  $record['postcode']  . "' </td>"; 
        echo "</tr>";      
        echo "<tr>";
        echo "<td style='padding:0 15px 0 15px; height: 70px'> State: <br> 
        <select class='textbox' name='state'>
                  <option selected>" . $record['state'] . "</option>
                  <option value='Johor'> Johor</option>
                  <option value='Kedah'> Kedah</option>
                  <option value='Kelantan'> Kelantan</option>
                  <option value='Melaka'> Melaka</option>
                  <option value='Negeri Sembilan'> Negeri Sembilan</option>
                  <option value='Pahang'> Pahang</option>
                  <option value='Perak'> Perak</option>
                  <option value='Perlis'> Perlis</option>
                  <option value='Pulau Pinang'> Pulau Pinang</option>
                  <option value='Sabah'> Sabah</option>
                  <option value='Sarawak'> Sarawak</option>
                  <option value='Selangor'> Selangor</option>
                  <option value='Terengganu'> Terengganu</option>
                </select> </td>"; 
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Birthdate: <br> <input class='textbox' type='date' name='birthdate' value='" .  $record['birthdate']  . "' </td>";
        echo "</tr>";      
        echo "<tr>";
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Password: <br> <input class='textbox' type='password' name='password' value='" .  $record['password']  . "' </td>";
        echo "<td style='padding:0 15px 0 15px; height: 70px'> Confirm Password: <br> <input class='textbox' type='password' name='confirm' value='" .  $record['password']  . "' </td>";  
        echo "</tr>";     
        echo "</table>";
        echo "<br>";
        echo "<input class='button' type='submit' name='save' value='Save'>";
        echo "</form>"; ?>        
      </div>
    </div>
  </body>
</html>


profile_process.php - This one is incomplete because I wanted to test it on the username first before trying to do the whole thing.

<?PHP
    $username = "";
    $name = "";
    $email = "";
    $phone = "";
    $address = "";
    $postcode = "";
    $state = "";
    $password = "";
    $password2 = "";
    $birthdae = "";
    $error = array();
  
  if(isset($_POST['save'])){
    $username = $_POST['user'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $postcode = $_POST['postcode'];
    $state = $_POST['state'];
    $password = $_POST['password'];
    $password2 = $_POST['confirm'];
    $birthdae = $_POST['birthdate'];
    
    $connect = new mysqli("localhost","root","","kz_komputer");
    
    $sql = "SELECT * FROM customer";
    $data = mysqli_query($conn,$sql);
    $record = mysqli_fetch_array($data);

    $update = "UPDATE `customer` SET `username` = '$username' WHERE `customer`.`username` = '" . $record['username'] . "';";
    mysqli_query($connect,$update);
    header("location:profile.php");
  }
?>


What I have tried:

Delete Button
profile.php
while($record = mysqli_fetch_array($data)){
          echo "<form>";
          echo "<tr align=center height=40px>";
          echo "<td>" . $record['id'] . "</td>";
          echo "<td>" . $record['name'] . "</td>";
          echo "<td>" . $record['phone_number'] . "</td>";
          echo "<td>" . $record['date_employed'] . "</td>";
          echo "<td>" . $record['date_unemployed'] . "</td>";
          echo "<td>" . $record['position'] . "</td>";
          echo "<td>" . $record['salary'] . "</td>"; 
          ?>

          <input type='submit' name='delete' value='Delete'>;

          <?PHP echo "</tr>";
          echo "</form>"; 
          ?>
        }


Save Button
profile_process.php
$update = "UPDATE `customer` SET `username` = `$username`";
    mysqli_query($connect,$update);
    header("location:profile.php");
Posted
Updated 15-Mar-18 3:07am

1 solution

In employees.php you have the form "add_employee" with action employees.php and a submit button named "add". If that button is pressed, employees.php is called with $_POST['add'] set. That should work because employees.php includes employee_process.php which checks the parameter.

You have also unnamed forms without actions and the delete buttons as table rows. That won't work because the action attribute is required with forms. As a result the form members (the buttons) are rendered on screen but nothing happens when activating the buttons.

The profile.php looks OK for me and it should be called with $_POST['save'] set when activating the button. But you are not generating any output or check for database errors in profile_process.php. So the button might work but you did not recognise it when the database is not updated for other reasons.

I suggest to add error checking to your SQL related code and report errors. You might even add output upon success (at least for debugging during development). Then you would know when it is triggered by a button.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900