我创建了一个表来查看数据库中的记录,我无法更新或删除,如果我点击他们刚刚重定向而没有更改的按钮就没有任何反应。可以做些什么来解决这个问题。
update.php
<?php
include 'includes/db.php';
//$student_id = null;
if ( !empty($_GET['student_id'])) {
$student_id = $_REQUEST['student_id'];
}
if ( null==$student_id ) {
header("Location: addstudent.php");
}
if ( !empty($_POST)) {
// keep track valstudent_idation errors
$first_nameError = null;
$middle_nameError = null;
$last_nameError = null;
$emailError = null;
$idcodeError = null;
// keep track post values
$first_name = $_POST['first_name'];
$middle_name = $_POST['middle_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$idcode = $_POST['idcode'];
// validate input
$valstudent_id = true;
if (empty($first_name)) {
$nameError = 'Please enter Name';
$valstudent_id = false;
}
$valstudent_id = true;
if (empty($middle_name)) {
$nameError = 'Please enter Name';
$valstudent_id = false;
}
$valstudent_id = true;
if (empty($last_name)) {
$nameError = 'Please enter Name';
$valstudent_id = false;
}
if (empty($email)) {
$emailError = 'Please enter Email Address';
$valstudent_id = false;
} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$emailError = 'Please enter a valid Email Address';
$valstudent_id = false;
}
if (empty($idcode)) {
$idcodeError = 'Please enter IDCODE';
$valstudent_id = false;
}
// update data
if ($valstudent_id) {
$first_name=$_POST['first_name'];
$middle_name=$_POST['middle_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];
$idcode=$_POST["idcode"];
$sql = "UPDATE student SET first_name=:first_name, middle_name=:middle_name, last_name=:last_name, idcode=:idcode, email=:email WHERE student_id=:student_id";
$stmt = $pdo->prepare($sql);
//echo "<br/>Query: ".$stmt->query()."<br/>"
$stmt->bindValue(':first_name', $first_name);
$stmt->bindValue(':middle_name', $middle_name);
$stmt->bindValue(':last_name', $last_name);
$stmt->bindValue(':idcode', $idcode);
$stmt->bindValue(':email', $email);
$result= $stmt->execute();
//$result = execute(array($first_name,middle_name,last_name,$email,$idcode));
header("Location: table.php");
}
}
else {
$sql = "SELECT * FROM student where student_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($student_id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$first_name = $data['first_name'];
$middle_name = $data['middle_name'];
$last_name = $data['last_name'];
$idcode = $data['idcode'];
$email = $data['email'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Update People</h3>
</div>
<form class="form-horizontal" action="update.php?student_id=<?php echo $student_id?>" method="post">
<div class="control-group <?php echo !empty($first_nameError)?'error':'';?>">
<label class="control-label">First Name</label>
<div class="controls">
<input name="first_name" type="text" placeholder="first_Name" value="<?php echo !empty($first_name)?$first_name:'';?>">
<?php if (!empty($first_nameError)): ?>
<span class="help-inline"><?php echo $middle_nameError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($nameError)?'error':'';?>">
<label class="control-label">Middle Name</label>
<div class="controls">
<input name="middle_name" type="text" placeholder="middle_Name" value="<?php echo !empty($middle_name)?$middle_name:'';?>">
<?php if (!empty($middle_nameError)): ?>
<span class="help-inline"><?php echo $middle_nameError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($nameError)?'error':'';?>">
<label class="control-label">Last Name</label>
<div class="controls">
<input name="last_name" type="text" placeholder="last_Name" value="<?php echo !empty($last_name)?$last_name:'';?>">
<?php if (!empty($last_nameError)): ?>
<span class="help-inline"><?php echo $lastnameError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($emailError)?'error':'';?>">
<label class="control-label">Email Address</label>
<div class="controls">
<input name="email" type="text" placeholder="Email Address" value="<?php echo !empty($email)?$email:'';?>">
<?php if (!empty($emailError)): ?>
<span class="help-inline"><?php echo $emailError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($idcodeError)?'error':'';?>">
<label class="control-label">IDCODE</label>
<div class="controls">
<input name="idcode" type="text" placeholder="IDCODE" value="<?php echo !empty($idcode)?$idcode:'';?>">
<?php if (!empty($idcodeError)): ?>
<span class="help-inline"><?php echo $idcodeError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn" href="table.php">Back</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>
delete.php
<?php
error_reporting(1);
include 'includes/db.php';
$student_id = 0;
if ( !empty($_GET['student_id'])) {
$student_id = $_REQUEST['student_id'];
}
if ( !empty($_POST)) {
$id = $_POST['student_id'];
// delete data
$sql = "DELETE FROM people WHERE student_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($student_id));
header("Location: table.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Delete the Person</h3>
</div>
<form class="form-horizontal" action="delete.php" method="post">
<input type="hidden" name="student_id" value="<?php echo $student_id;?>"/>
<p class="alert alert-error">Are you sure to delete ?</p>
<div class="form-actions">
<button type="submit" class="btn btn-danger">Yes</button>
<a class="btn" href="table.php">No</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>