![](/img/trans.png)
[英]Have a Insert and a Edit button in the same form using PHP and SQL
[英]Insert and edit the data using same form in php
我有頁面(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>
您可以使用$_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>
您可以從網址獲取ID來確定它是編輯/創建操作,然后確定要顯示/隱藏的字段,如下所示
if(isset($_GET['id']))
{
// get record from table of that id and show into form
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();
}
// Show your form fields here.like below
?>
<input type="text" class="form-control col-xs-3" id="year" name ="year" value="<?php echo $results[0]["year"] ?>">
<?php
}
else
{
// Its create action , so show your form fields like
?>
<input type="text" class="form-control col-xs-3" id="year" name ="year">
<?php
}
在您的布局中。只需在要在表單中顯示的值之前插入符號@。 當頁面調用添加新數據時,插入@符號告訴編譯器抑制由於未聲明變量而導致的錯誤。 因此,您的頁面將如下所示...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.