截至2015年6月23日更新
更新查询仍无效。从Rene Kross那里得到了一些建议并进行了一些代码清理,偶然发现了一些缺少分号等的部分,但是更新查询仍无法正常工作。
这是adminUpdate.php
<?php
require 'Connect.php';
$staffID = null;
if ( !empty($_GET['staffID'])) {
$staffID = $_REQUEST['staffID'];
}
if ( null==$staffID ) {
header("Location: ManageAdministrator.php");
}
if ( !empty($_POST)) {
// keep track validation errors
$adminNameError = null;
$emailError = null;
$SKGError = null;
$ExtnError = null;
// keep track post values
$adminName = $_POST['adminName'];
$email = $_POST['email'];
$SKG = $_POST['SKG'];
$Extn = $_POST['Extn'];
// validate input
$valid = true;
if (empty($adminName)){
$adminNameError = "Please enter Administrator Name";
$valid = false;
}
if (empty($email)) {
$emailError = 'Please enter Email Address';
$valid = false;
} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$emailError = 'Please enter a valid Email Address';
$valid = false;
}
if (empty($SKG)) {
$SKGError = 'Please choose SKG';
$valid = false;
}
if(empty($Extn)){
$ExtnError = "Please enter Extension Number";
$valid = false;
}
// update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE admin set adminName ='$adminName', email ='$email', SKG ='$SKG', Extn ='$Extn' WHERE staffID = '$staffID'";
$q = $pdo->prepare($sql);
$q->execute(array($staffID,$adminName,$email,$SKG,$Extn));
Database::disconnect();
header("Location: ManageAdministrator.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM admin where staffID = ?";
$q = $pdo->prepare($sql);
$q->execute(array($staffID));
$data = $q->fetch(PDO::FETCH_ASSOC);
$adminName = $data['adminName'];
$email = $data['email'];
$SKG = $data['SKG'];
$Extn = $data['Extn'];
$password = $data['password'];
Database::disconnect();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Update Administrator</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Capability Management">
<!-- CSS External Link -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/sb-admin.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<div id="wrapper">
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="HomePage.php"><b> Training Log Database </b></a>
</div>
<ul class="nav navbar-right top-nav">
<li class="dropdown">
<!-- PHP REFER LOGIN NAME-->
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i> M Farhan M Fazli <b class="caret"></b></a>
<ul class="dropdown-menu">
<li>
<a href="MyProfile.php"><i class="fa fa-fw fa-user"></i> Profile</a>
</li>
<li class="divider"></li>
<li>
<!-- PHP LOGOUT FUNCTION -->
<a href="LoginPage.html"><i class="fa fa-fw fa-power-off"></i> Log Out</a>
</li>
</ul>
</li>
</ul>
<!-- Sidebar Menu Items start here -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li>
<a href="HomePage.php"><i class="fa fa-fw fa-dashboard"></i> Home</a>
</li>
<li>
<a href="ManageAdministrator.php"><i class="fa fa-fw fa-bar-chart-o"></i> Manage Administrator</a>
</li>
<li>
<!-- Add new pages here -->
<a href="#"><i class="fa fa-fw fa-bar-chart-o"></i> Manage Staff</a>
</li>
<li>
<a href="javascript:;" data-toggle="collapse" data-target="#demo"><i class="fa fa-fw fa-edit"></i> Manage Training<i class="fa fa-fw fa-caret-down"></i></a>
<ul id="demo" class="collapse">
<li>
<a href="TrainingList.php">Training Information</a>
</li>
<li>
<a href="#">TBA</a>
</li>
</ul>
</li>
<li>
<a href="javascript:;" data-toggle="collapse" data-target="#demo1"><i class="glyphicon glyphicon-wrench"></i> Tools for Administrator<i class="fa fa-fw fa-caret-down"></i></a>
<ul id="demo1" class="collapse">
<li><a href="#"> Add News </a></li>
<li><a href="#"> Add Announcement </a></li>
<li><a href="#"> Search News</a> </li>
<li><a href="#"> Search Announcement </a></li>
<li><a href="#"> View My Post </a></li>
</ul>
</li>
<li>
<a href="#"><i class="glyphicon glyphicon-info-sign"></i> About</a>
</li>
<li>
<a href="#"><i class="glyphicon glyphicon-question-sign"></i> FAQs</a>
</li>
</div>
<!-- /.navbar-collapse -->
</nav>
<div id="page-wrapper">
<div class="container-fluid">
<div class="row col-lg-12">
<h2 class="page-header"> Update Administrator Account </h2>
<div class="row col-lg-12">
<h3> Administrator details </h3>
<div class="row col-lg-12">
<form class="form-horizontal form-group" action="adminUpdate.php?id=<?php echo $staffID?>" method="post">
<!-- <div class="form-group col-lg-12 <?php echo !empty($staffIDError)?'error':'';?>">
<label>Administrator Staff ID</label>
<input class="form-control" name="staffID" type="text" value="<?php echo !empty($staffID)?$staffID:'';?>">
<?php if (!empty($staffIDError)): ?>
<span class="help-inline"><?php echo $staffIDError;?></span>
<?php endif; ?>
</div>-->
<div class="form-group col-lg-12 <?php echo !empty($adminNameError)?'error':'';?>">
<label>Administrator Name</label>
<input class="form-control" name="adminName" type="text" value="<?php echo !empty($adminName)?$adminName:'';?>">
<?php if (!empty($adminNameError)): ?>
<span class="help-inline"><?php echo $adminNameError;?></span>
<?php endif; ?>
</div>
<div class="form-group col-lg-12 <?php echo !empty($emailError)?'error':'';?>">
<label>Administrator Email</label>
<input class="form-control" name="email" type="text" value="<?php echo !empty($email)?$email:'';?>">
<?php if (!empty($emailError)): ?>
<span class="help-inline"><?php echo $emailError;?></span>
<?php endif; ?>
</div>
<div class="form-group col-lg-12 <?php echo !empty($ExtnError)?'error':'';?>">
<label> Extension Number </label>
<input class="form-control" name="Extn" type="text" value="<?php echo !empty($Extn)?$Extn:'';?>">
<?php if (!empty($ExtnError)): ?>
<span class="help-inline"><?php echo $ExtnError;?></span>
<?php endif; ?>
</label>
</div>
<div class="form-group col-lg-12 <?php echo !empty($SKGError)?'error':'';?>">
<label>SKG</label>
<select class="form-control" name="SKG" value="<?php echo !empty($SKG)?$SKG:'';?>">
<option> SKG 09 </option>
<option> SKG 18 </option>
<option> SKG 20 </option>
<option> SKG 16 </option>
<option> SKG 11 </option>
<option> SKG 13 </option>
<option> SKG 14 </option>
<option> SKG 12 </option>
<option> SKG 15 </option>
<option> SKG 10 </option>
<option> SKG 25 </option>
</select>
<?php if (!empty($SKGError)): ?>
<span class="help-inline"><?php echo $SKGError;?></span>
<?php endif; ?>
</div>
<!-- <div class="form-group col-lg-12 <?php echo !empty($passwordError)?'error':'';?>">
<label>Password</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Password" value="<?php echo !empty($password)?$password:'';?>">
<?php if (!empty($passwordError)): ?>
<span class="help-inline"><?php echo $passwordError;?></span>
<?php endif; ?> -->
</div>
<div class="form-actions col-lg-12">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn btn-default" href="ManageAdministrator.php">Back</a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div><!--wrap-->
</body>
</html>
这是Connect.php
<?php
class Database
{
private static $dbName = 'tlad' ;
private static $dbHost = 'localhost' ;
private static $dbUsername = 'root';
private static $dbUserPassword = '';
private static $cont = null;
public function __construct() {
exit('Init function is not allowed');
}
public static function connect()
{
// One connection through whole application
if ( null == self::$cont )
{
try
{
self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont = null;
}
}
?>
这是我的表:
正如我前面提到的,read,create和delete函数正在工作,除了这个更新函数不是。
请注意:我已经按照有关创建CRUD数据表的教程,虽然他的模板有效,但我的代码却没有。这是教程的更新模板代码:
<?php
require 'database.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header("Location: index.php");
}
if ( !empty($_POST)) {
// keep track validation errors
$nameError = null;
$emailError = null;
$mobileError = null;
// keep track post values
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
// validate input
$valid = true;
if (empty($name)) {
$nameError = 'Please enter Name';
$valid = false;
}
if (empty($email)) {
$emailError = 'Please enter Email Address';
$valid = false;
} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$emailError = 'Please enter a valid Email Address';
$valid = false;
}
if (empty($mobile)) {
$mobileError = 'Please enter Mobile Number';
$valid = false;
}
// update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE customers set name = ?, email = ?, mobile =? WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($name,$email,$mobile,$id));
Database::disconnect();
header("Location: index.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM customers where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$name = $data['name'];
$email = $data['email'];
$mobile = $data['mobile'];
Database::disconnect();
}
?>
<!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 a Customer</h3>
</div>
<form class="form-horizontal" action="update.php?id=<?php echo $id?>" method="post">
<div class="control-group <?php echo !empty($nameError)?'error':'';?>">
<label class="control-label">Name</label>
<div class="controls">
<input name="name" type="text" value="<?php echo !empty($name)?$name:'';?>">
<?php if (!empty($nameError)): ?>
<span class="help-inline"><?php echo $nameError;?></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" 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($mobileError)?'error':'';?>">
<label class="control-label">Mobile Number</label>
<div class="controls">
<input name="mobile" type="text" value="<?php echo !empty($mobile)?$mobile:'';?>">
<?php if (!empty($mobileError)): ?>
<span class="help-inline"><?php echo $mobileError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>
答案 0 :(得分:3)
您的execute
有5个参数,但查询需要6个。
$sql = "UPDATE admin set staffID = ?, adminName = ?, email = ?, SKG = ?, Extn = ? WHERE staffID = ?";
$q = $pdo->prepare($sql);
$q->execute(array($staffID, $adminName, $email, $SKG, $Extn, $staffID));
在$
,email
和SKG
前面也遗漏了Extn
。