我有以下代码可以完美地更新我的数据库表。但是,我现在希望它要么更新一个现有行(如果$ status的值为“ open”),要么创建一个新行(如果$ status的值为“ completed”)。
到目前为止,这是我的代码;
<?php
if (isset($_POST['submit_update_activity'])); {
require 'dbh.inc.php';
$activity_id = $_POST['hidden_activity_id'];
$idFromKnowledgeBase = $_POST['hidden_idFromKnowledgeBase'];
$hiddenUserID = $_POST['hidden_userId'];
$title = $_POST['title'];
$description = $_POST['description'];
$assigned_to = $_POST['assigned_to'];
$category = $_POST['category'];
$cost = $_POST['cost'];
$next_due = $_POST['next_due'];
$due_every = $_POST['due_every'];
$frequency = $_POST['frequency'];
$supplier = $_POST['supplier'];
$status = $_POST['status'];
$comments = $_POST['comments'];
$emptyAssignedTo = $_POST['empty_assigned_to'];
$emptyStatus = $_POST['empty_status'];
$emptyCategory = $_POST['empty_category'];
$dateCompleted = $_POST['date_completed'];
$emptyFrequency = $_POST['empty_frequency'];
$emptyNextDue = $_POST['empty_next_due'];
$next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
if (empty($frequency)) {
$frequency = $emptyFrequency;
}
if (empty($status)) {
$status = $emptyStatus;
}
if (empty($assigned_to)) {
$assigned_to = $emptyAssignedTo;
}
if (empty($category)) {
$category = $emptyCategory;
}
//This line isn't working
if ($status == 'open') {
$next_due = $emptyNextDue;
} else {
$next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
}
$stmt = $conn->prepare("UPDATE activities SET idFromKnowledgeBase = ?, userId = ?, title = ?, description = ?, assigned_to = ?, category = ?, cost = ?, last_completed = ?,next_due = ?, frequency = ?, supplier = ?, status = ?, comments = ? WHERE id = ?");
$stmt->bind_param("ssssssssssssss", $idFromKnowledgeBase, $hiddenUserID, $title, $description, $assigned_to, $category, $cost, $dateCompleted, $next_due, $frequency, $supplier, $status, $comments, $activity_id);
$stmt->execute();
if($stmt->affected_rows >0) {
header('Location: ../all_activities.php?updated');
}
}
?>
我已经尝试使用=,==和===来比较$ status。如果我将其缩回一个凹口并根据$ status的值回显“ open”或“ completed”,则它会正常工作,每次都回显正确的答案。
感谢您的咨询。
答案 0 :(得分:0)
您缺少INSERT语句
<?php
if (isset($_POST['submit_update_activity'])); {
require 'dbh.inc.php';
$activity_id = $_POST['hidden_activity_id'];
$idFromKnowledgeBase = $_POST['hidden_idFromKnowledgeBase'];
$hiddenUserID = $_POST['hidden_userId'];
$title = $_POST['title'];
$description = $_POST['description'];
$assigned_to = $_POST['assigned_to'];
$category = $_POST['category'];
$cost = $_POST['cost'];
$next_due = $_POST['next_due'];
$due_every = $_POST['due_every'];
$frequency = $_POST['frequency'];
$supplier = $_POST['supplier'];
$status = $_POST['status'];
$comments = $_POST['comments'];
$emptyAssignedTo = $_POST['empty_assigned_to'];
$emptyStatus = $_POST['empty_status'];
$emptyCategory = $_POST['empty_category'];
$dateCompleted = $_POST['date_completed'];
$emptyFrequency = $_POST['empty_frequency'];
$emptyNextDue = $_POST['empty_next_due'];
$next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
if (empty($frequency)) {
$frequency = $emptyFrequency;
}
if (empty($status)) {
$status = $emptyStatus;
}
if (empty($assigned_to)) {
$assigned_to = $emptyAssignedTo;
}
if (empty($category)) {
$category = $emptyCategory;
}
//This line isn't working
if ($status == 'open') {
$next_due = $emptyNextDue;
} else {
$next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
}
if($status == 'completed')
{
$stmt = $conn->prepare('INSERT INTO activities(idFromKnowledgeBase, userId, title , description, assigned_to, category, cost, last_completed,next_due, frequency, supplier , status, comments) VALUES(:kb, :uid, :title, :descr, :assign, :cat, :cost, :last, :next, :freq, :sup, :stat, :com)');
$stmt->execute(array(
'kb' => $idFromKnowledgeBase,
'uid' => $hiddenUserID,
'title' => $title,
'descr' => $description,
'assign' => $assigned_to,
'cat' => $category,
'cost' => $cost,
'last' => $dateCompleted,
'next' => $next_due,
'freq' => $frequency,
'sup' => $supplier,
'stat' => $status,
'com' => $comments,
));
}
else
{
$stmt = $conn->prepare("UPDATE activities SET idFromKnowledgeBase = :kb, userId = :uid, title = :title, description = :descr, assigned_to = :assign, category = :cat, cost = :cost, last_completed = :last,next_due = :next, frequency = :freq, supplier = :sup, status = :stat, comments = :com WHERE id = :id");
$stmt->execute(array(
'kb' => $idFromKnowledgeBase,
'uid' => $hiddenUserID,
'title' => $title,
'descr' => $description,
'assign' => $assigned_to,
'cat' => $category,
'cost' => $cost,
'last' => $dateCompleted,
'next' => $next_due,
'freq' => $frequency,
'sup' => $supplier,
'stat' => $status,
'com' => $comments,
'id' => $activity_id
));
}
if($stmt->affected_rows >0) {
header('Location: ../all_activities.php?updated');
}
}
?>