Кнопка Php не работает.
У меня есть два PHP-файла; employees.php, и profile.php-да. Для employees.php таблица отображается с помощью кнопка Delete рядом с каждой записью, и для profile.php есть кнопка сохранения внизу. Проблема одинакова для обоих файлов, кнопка не работает. И кнопка удаления, и кнопка сохранения находятся в форме в своих файлах.
**Записи находятся в различный таблицы;
employees.php --> таблица сотрудников
profile.php --> таблица клиентов
кнопка Удалить(employees.php) - для удаления записи
кнопка Сохранить(profile.php) - для обновления записи
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 - этот вариант неполный, потому что я хотел сначала проверить его на имени пользователя, прежде чем пытаться сделать все это целиком.
<?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"); } ?>
Что я уже пробовал:
кнопка Delete
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
$update = "UPDATE `customer` SET `username` = `$username`"; mysqli_query($connect,$update); header("location:profile.php");