使用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
的{{1}}值,因为idDepartment
期望mysql
但收到{{1 }}。这没有帮助,我仍然面临问题。
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
<!doctype html>
<html lang="en">
<head>
<title>Title</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<?php require_once 'formprocessor.php';?>
<!-- Based on query type it will use bootstrap alert class -->
<?php
if (isset($_SESSION['message'])): ?>
<div class = "alert alert-<?=$_SESSION['msg_type'] ?>">
<?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 ?>
<div class = "container">
<?php
// Connect to databse
$mysqli = new mysqli('127.0.0.1', 'root', "", 'v2HospitalDB')
or die(mysqli_error($mysqli));
// Will select everthing from Doctor and display on the page
$result = $mysqli->query("SELECT * FROM Doctor") or die($mysqli->error);
?>
<!-- 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>
<?php else: ?>
<button class="btn btn-primary" type="submit" name="save">Save</button>
<?php endif; ?>
</div>
</form>
</div>
</div>
</body>
</html>