我有页面(auditplanentry.php),其中包含用于输入数据的表单控件。在使用同一个php文件中的PDO查询将数据保存在mysql中之后。 我将所有数据显示为另一个PHP页面中的表(auditplan.php)。在每行我有编辑按钮,在点击它之后另一个php页面(auditplanedit.php)与相同的表单控件填充来自mysql的数据。编辑完之后,我在同一个auditplanedit.php页面中使用Update查询更新mysql。 在这里我想知道,我如何使用相同的auditplanentry.php页面进行输入和更新。这里我重复相同的表单控制另一个页面中的编码,除了我从mysql控制值到控件。
请给我一些想法,如何做到这一点。?
auditplanentry.php
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Year:</label>
<div class="col-sm-5">
<input type="text" class="form-control col-xs-3" id="year" name ="year">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="usr">Month:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="month" name ="month">
</div>
</div>
//query code:
$sql = "INSERT INTO auditplan(auditid,year,month,status,comment) VALUES " . "(:audit, :year, :month, :status, :comment)";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":audit", $audit);
$stmt->bindValue(":year", $year);
$stmt->bindValue(":month", $month);
$stmt->bindValue(":status", $status);
$stmt->bindValue(":comment", $comment);
// execute Query
$stmt->execute();
}
catch (Exception $ex)
{
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = $ex->getMessage();
}
auditplanedit.php
<?php
include("config.php");
include("header.php");
try {
$sql = "SELECT * FROM auditplan WHERE id = :cid";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":cid",intval($_GET['cid']));
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Year:</label>
<div class="col-sm-5">
<input type="text" class="form-control col-xs-3" id="year" name ="year" value="<?php echo $results[0]["year"] ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="usr">Month:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="month" name ="month" value="<?php echo $results[0]["month"] ?>">
</div>
</div>
//database query:
$sql = "UPDATE auditplan SET auditid = :audit, year = :year, month = :month, status = :status, comment = :comment" . " WHERE id = :cid ";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":audit", $audit);
$stmt->bindValue(":year", $year);
$stmt->bindValue(":month", $month);
$stmt->bindValue(":status", $status);
$stmt->bindValue(":comment", $comment);
$stmt->bindValue(":cid", $cid);
$stmt->execute();
}
catch (Exception $ex)
{
$_SESSION["errorMsg"] = $ex->getMessage();
}
header("Location:auditplan.php");
auditplan.php中的按钮:
<?php $getId = $row["id"];?>
<td>
<a href="auditplanedit.php?cid=<?php echo $getId; ?>"><i class="ion ion-edit"></i></a>    
<a href="deleteauditplan.php?cid=<?php echo $getId; ?>" onclick="return confirm('Are you sure?')"><i class="ion ion-close"></i></a>
</td>
答案 0 :(得分:0)
您可以从网址获取ID以确定它是编辑/创建操作,然后您可以决定显示/隐藏哪些字段,如下所示
<?php
public function hoteldetails($id)
{
$this->load->helper('cookie');
$hotel_details_view = array();
$hotel_details_view = get_cookie('hotel_details_view');
$hotel_details_view[] = $id;
$this->input->set_cookie($hotel_details_view);
print_r($hotel_details_view);
$this->load->view('hotel_details',$data);
}
?>
答案 1 :(得分:0)
如果您正在编辑,可以使用$_GET
参数让表单知道。
所以,你的PHP代码看起来像这样:
<?php
if($_GET['act']=="edit"){ // If $_GET['act'] equals to 'edit'
// Select query
try {
$sql = "SELECT * FROM auditplan WHERE id = :cid";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":cid",intval($_GET['cid']));
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
if(isset($_POST['submit'])){
// Edit query
$sql = "UPDATE auditplan SET auditid = :audit, year = :year, month = :month, status = :status, comment = :comment" . " WHERE id = :cid ";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":audit", $audit);
$stmt->bindValue(":year", $year);
$stmt->bindValue(":month", $month);
$stmt->bindValue(":status", $status);
$stmt->bindValue(":comment", $comment);
$stmt->bindValue(":cid", $cid);
$stmt->execute();
}
catch (Exception $ex)
{
$_SESSION["errorMsg"] = $ex->getMessage();
}
header("Location:auditplan.php");
}
}else{ // if $_GET['act'] doesn't equal to 'edit'
if(isset($_POST['submit'])){
// Add query
$sql = "INSERT INTO auditplan(auditid,year,month,status,comment) VALUES " . "(:audit, :year, :month, :status, :comment)";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":audit", $audit);
$stmt->bindValue(":year", $year);
$stmt->bindValue(":month", $month);
$stmt->bindValue(":status", $status);
$stmt->bindValue(":comment", $comment);
// execute Query
$stmt->execute();
}
catch (Exception $ex)
{
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = $ex->getMessage();
}
}
}
?>
您的HTML表单将如下所示:
<form method="POST">
<input type="text" name="day" value="<?php echo isset($_GET['act']) && $_GET['act']=="edit" ? $results[0]["day"] : ""; ?>" />
<input type="text" name="month" value="<?php echo isset($_GET['act']) && $_GET['act']=="edit" ? $results[0]["month"] : ""; ?>" />
<input type="submit" name="submit"/>
</form>
你的链接看起来像是:
<a href="script.php/">add</a>
<a href="script.php?act=edit&cid=<?php echo $getId; ?>">Edit</a>
答案 2 :(得分:-1)
在您的布局中..只需在要在表单中显示的值之前插入符号@。插入@符号告诉编译器在页面调用添加新数据时由于未声明的变量而抑制错误。因此,您的页面将如下所示......