Php按钮不起作用。 [英] Php button not working.

查看:114
本文介绍了Php按钮不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个PHP文件; employees.php和profile.php。对于 employees.php ,每个记录旁边都会显示一个带有删除按钮的表格,对于 profile.php ,有一个保存按钮在底部。两个文件的问题是相同的,按钮不起作用。删除按钮和保存按钮的形式都在文件中。



**记录在不同的表中;

employees.php - >员工表

profile.php - >客户表



删除按钮(employees.php) - 删除记录

保存按钮(profile.php) - 更新记录



employees.php

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



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



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 - 这个是不完整的,因为我想测试它在尝试完成整个事情之前先用户名。





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");
  }
?>





我尝试了什么:



删除按钮

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





保存按钮

profile_process。 php



Save Button
profile_process.php

$update = "UPDATE `customer` SET `username` = `$username`";
    mysqli_query($connect,$update);
    header("location:profile.php");

推荐答案

conn = mysqli_connect(localhost,root , , kz_komputer);
if(!
conn = mysqli_connect("localhost","root","","kz_komputer"); if(!


conn){
die(无法连接:。mysqli_error());
}
conn){ die("Cannot connect: " . mysqli_error()); }


sql =SELECT * FROM employees;
sql = "SELECT * FROM employees";


这篇关于Php按钮不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆