gpt4 book ai didi

php - undefined variable - 作品

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:00 26 4
gpt4 key购买 nike

我有一个脚本可以更新数据库中的日期字段。 (已购买)。

我还在另一部分中使用该数据更新第二个日期字段,该字段采用输入日期并添加 6 年 $duedate。它工作正常,但我收到变量 purchasedUndefined Variable 错误。

$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 行,所以我已经包含了相关的片段,如果需要可以添加完整的文件。

最佳答案

文件末尾的这些行:

$duedate = new DateTime($purchased);
$duedate->add(new DateInterval('P6Y'));

<input type="hidden" name="due" value="<?php echo $duedate->format('Y-m-d'); ?>">

正在抛出 undefined variable 通知,因为它位于条件语句之外,并且在页面加载后立即发出通知。

这就是我从这一切中得到的。

我发布了这个,因为我很早就对此发表了评论。

因此,使用条件语句/三元运算符。

一旦有它的值(value),它就会完成它的工作。

关于php - undefined variable - 作品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38638493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com