У меня есть скрипт, который обновляет поле даты в моей базе данных. (purchased
).Undefined Variable - Works
Я также использую эти данные в другой части, которая обновляет второе поле даты, которое принимает дату ввода и добавляет 6 лет $duedate
. Он работает отлично, но я получаю ошибку Неопределенная переменная для переменной purchased
.
$duedate = new DateTime($purchased);
$duedate->add(new DateInterval('P6Y'));
Я попытался определить его, используя нижеследующее, но он останавливает обновление второго поля и не вызывает никаких ошибок.
$duedate = new DateTime($_POST['purchased']);
$duedate->add(new DateInterval('P6Y'));
$purchased = "";
$duedate = new DateTime($purchased);
$duedate->add(new DateInterval('P6Y'));
$purchased = null;
$duedate = new DateTime($purchased);
$duedate->add(new DateInterval('P6Y'));
$purchased = isset($_POST['purchased']) ? $_POST['purchased'] : '';
$duedate = new DateTime($purchased);
$duedate->add(new DateInterval('P6Y'));
$purchased = !empty($_POST['purchased']) ? $_POST['purchased'] : '';
$duedate = new DateTime($purchased);
$duedate->add(new DateInterval('P6Y'));
Код
$barcode = $_GET['barcode'];
$stmt = $conn->prepare("SELECT * FROM assets WHERE barcode=:barcode");
$stmt->execute(array(":barcode"=>$barcode));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if (isset($_POST['update'])) {
$category = isset($_POST['category']) ? $_POST['category'] : null;
$manufactuer = isset($_POST['manufactuer']) ? $_POST['manufactuer'] : null;
$model = isset($_POST['model']) ? $_POST['model'] : null;
$serial = isset($_POST['serial']) ? $_POST['serial'] : null;
$itemcondition = isset($_POST['itemcondition']) ? $_POST['itemcondition'] : null;
$locationb = isset($_POST['locationb']) ? $_POST['locationb'] : null;
$locationr = isset($_POST['locationr']) ? $_POST['locationr'] : null;
$comments = isset($_POST['comments']) ? $_POST['comments'] : null;
$purchased = isset($_POST['purchased']) ? $_POST['purchased'] : null;
$retired = isset($_POST['retired']) ? $_POST['retired'] : null;
$stolen = isset($_POST['stolen']) ? $_POST['stolen'] : null;
$latest = isset($_POST['latest']) ? $_POST['latest'] : null;
$due = isset($_POST['due']) ? $_POST['due'] : null;
$sql_part = array();
$prepare = array();
if ($category){
$sql_part[] = 'category = :category';
$prepare[':category'] = $category;
}
if($manufactuer){
$sql_part[] = 'manufactuer = :manufactuer';
$prepare[':manufactuer'] = $manufactuer;
}
if($model){
$sql_part[] = 'model = :model';
$prepare[':model'] = $model;
}
if($serial){
$sql_part[] = 'serial = :serial';
$prepare[':serial'] = $serial;
}
if($itemcondition){
$sql_part[] = 'itemcondition = :itemcondition';
$prepare[':itemcondition'] = $itemcondition;
}
if($locationb){
$sql_part[] = 'locationb = :locationb';
$prepare[':locationb'] = $locationb;
}
if($locationr){
$sql_part[] = 'locationr = :locationr';
$prepare[':locationr'] = $locationr;
}
if($comments){
$sql_part[] = 'comments = :comments';
$prepare[':comments'] = $comments;
}
if($purchased){
$sql_part[] = 'purchased = :purchased';
$prepare[':purchased'] = $purchased;
}
if($retired){
$sql_part[] = 'retired = :retired';
$prepare[':retired'] = $retired;
}
if($stolen){
$sql_part[] = 'stolen = :stolen';
$prepare[':stolen'] = $stolen;
}
if($latest){
$sql_part[] = 'latest = :latest';
$prepare[':latest'] = $latest;
}
if($due){
$sql_part[] = 'due =:due';
$prepare[':due'] = $due;
}
$prepare[':barcode'] = $barcode;
if(count($sql_part)){
$sql = 'UPDATE assets SET ';
$sql .= implode(', ', $sql_part);
$sql .= ' WHERE barcode = :barcode';
$stmt = $conn->prepare($sql);
if($stmt){
$result = $stmt->execute($prepare);
$count = $stmt->rowCount();
header('Location: ./usearch.php');
exit;
}
}
}
$duedate = new DateTime($purchased);
$duedate->add(new DateInterval('P6Y'));
<input type="hidden" name="due" value="<?php echo $duedate->format('Y-m-d'); ?>">
Файл 261 строк, поэтому я включал соответствующие сегменты и добавить полный файл, если необходимо.
'$ _POST ['purchase]' отсутствует цитата здесь. –
В вашей части «Код» (внизу вашего вопроса), где инициализируется '$ purchase'? –
Фред, не исправить, но спасибо, что указали это. «Брэндон». Я добавил полный раздел mysql. –