使用 sql
以及 php
. 在申请表中,我有一个 Edit
允许用户更新字段的按钮 doctorFname,doctorLname, idDepartment, and specialty
. 当用户输入新数据并单击 save
,它应该发布到 mysql
. 这是我的项目的最后一步,我很难弄清楚为什么它没有更新。我可以 delete
并添加 new
用户。我会把代码贴在下面。任何帮助都将不胜感激,因为我离完成任务只有一步之遥。再次感谢!
更新:我已经 error reporting
这就是我得到的错误。 Cannot modify header information - headers already sent by (output started at /Users/moe/Desktop/CRUD/index.php:1) in /Users/moe/Desktop/CRUD/formprocessor.php on line 87
更新2:看了我的 UPDATE
用户单击 update
巴顿,我忘了加一个 '
进入 sql
声明。在中运行相同的查询之后 sql workbench
为了让它发挥作用,我试着看看我最终是否做对了。但我得到下面的错误 Incorrect integer value: '' for column 'idDepartment' at row 1
更新3:我尝试使用以下正则表达式来获取 integer
价值 idDepartment
自 mysql
期待着一个 int
但是收到一封信 string
相反。这没有帮助,我仍然面临着这个问题。
$idDepart = preg_replace("/[^0-9,.]/", "", $idDepart); Doctor Table
```
Table: Doctor
Columns:
doctorID int(11) AI PK
doctorFName varchar(45)
doctorLname varchar(45)
idDepartment int(11)
specialty varchar(45)`index.php`
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<?php
// Based on the message type it will echo it and then unset the session
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<!-- End of if statment -->
<?php endif ?>
<!-- This is the format for the table with the folowing columns -->
<div class="row justify-content-center">
<table class="table">
<thead>
<tr>
<th> Doctor ID </th>
<th> First Name </th>
<th> Last Name</th>
<th> Department ID</th>
<th> Speciality </th>
<th colspan="2"> Action </th>
</tr>
</thead>
<?php
// everything is fetched from db and stored in row
while($row = $result->fetch_assoc()):?>
<!-- Each row will have its repsected column from the database -->
<tr>
<td> <?php echo $row['doctorID']; ?></td>
<td> <?php echo $row['doctorFName']; ?></td>
<td> <?php echo $row['doctorLname']; ?></td>
<td> <?php echo $row['idDepartment']; ?></td>
<td> <?php echo $row['specialty']; ?></td>
<td>
<a href="index.php?edit=<?php echo $row['doctorID']; ?>"
class="btn btn-info">Edit</a>
<a href="formprocessor.php?delete=<?php echo $row['doctorID'];?>"
class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile;
?>
</table>
</div>
<?php
// This function prints the array in a nice format
function pre_r($array) {
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
<div class="row justify-content-center">
<form action="formprocessor.php" method="post">
<!-- hidden input field for the update -->
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="form-group">
<label> First Name</label>
<input type="text" name="fname" class="form-control" value="<?php echo $firstname;?>" placeholder="Enter First Name">
</div>
<div label="form-group">
<label> Last Name</label>
<input type="text" value="<?php echo $lastname;?>" name="lname" class="form-control" placeholder="Enter Last Name">
</div>
<div label="form-group">
<label> Department ID</label>
<input type="text" value="<?php echo $idDepart;?>" name="departmentID" class="form-control" placeholder="Enter DepartmentID">
</div>
<div label = "form-group">
<label> Speciality </label>
<input type="text" value="<?php echo $special;?>" name="speciality" class="form-control" placeholder="Enter Specialty">
</div>
<div class="form-group">
<?php
if($update == true): ?>
<button class="btn btn-info" type="submit" name="update">Update</button>
<button class="btn btn-primary" type="submit" name="save">Save</button>
</div>
</form>
</div>
</div>
// Connect to mysql database
$mysqli = new mysqli('127.0.0.1','root',"",'v2HospitalDB') or die(mysqli_error($mysqli));
// Reset values to empty
$firstname = "";
$lastname = "";
$departID = "";
$special = "";
$update = false;
$id = 0;
// Check if the save button has been pressed
if(isset($_POST['save'])){
// store columns from database
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$departID = $_POST['departmentID'];
$special = $_POST['speciality'];
// Insert records into database
$mysqli -> query("INSERT INTO Doctor(doctorFName , doctorLname , idDepartment, specialty)
VALUES('$firstname', '$lastname', '$departID' , '$special')") or
die($mysqli->error);
// will show at top of screen once the record has been saved
$_SESSION['message'] = "You have saved a record into the database";
$_SESSION['msg_type'] = "success";
// redirect back to the index.php after inserting records
header("location: index.php");
}
// This will delete the record from the table based on the id
if(isset($_GET['delete'])){
$id = $_GET['delete'];
$mysqli->query("DELETE FROM Doctor WHERE idDepartment AND doctorID = '$id'") or die($mysqli->error);
// When you delete a record, will show at top of screen
$_SESSION['message'] = "You have saved a deleted a record from the database";
$_SESSION['msg_type'] = "danger";
session_destroy();
// redirect back to the index page
header("location:index.php");
}
// If the edit button is clicked
if(isset($_GET['edit'])){
$update = true;
$id = $_GET['edit'];
// change back to where doctorId and idDepartment
$result = $mysqli->query("SELECT * FROM Doctor WHERE doctorID = '$id'") or die($mysqli->error);
// will fetch all colums in table from the result array
// If the record has been found in the database
if(count($result) == 1){
$row = $result->fetch_array();
$firstname = $row['doctorFName'];
$lastname = $row['doctorLname'];
$idDepart = $row['idDepartment'];
$special = $row['specialty'];
// echo (var_dump($result));
}
// will show at top of page when user updates the table
$_SESSION['message'] = "Record has been selected";
$_SESSION['msg_type'] = "info";
session_destroy();
header('location: index.php');
}
// If user clicks update then will insert values into columns
if(isset($_POST['update'])){
$id = $_POST['id'];
$firstname = $_POST['doctorFName'];
$lastname = $_POST['doctorLname'];
$idDepart = $_POST['idDepartment'];
$special = $_POST['specialty'];
$mysqli->query("INSERT INTO Doctor(doctorFName, doctorLname, idDepartment, specialty) VALUES
('$firstname', '$lastname', '$idDepart', '$special' ");
// will show at top of page when user updates the table
$_SESSION['message'] = "Record has been updated";
$_SESSION['msg_type'] = "warning";
session_destroy();
header('location: index.php');
}
暂无答案!
目前还没有任何答案,快来回答吧!