我有一个更新数据库中日期字段的脚本。 (purchased
)。
我还在另一部分中使用该数据,该部分更新第二个日期字段,该字段采用输入日期并添加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行,因此我已包含相关细分,并可根据需要添加完整文件。
答案 0 :(得分:4)
文件末尾的这些行:
if sum > 90:
sum = 90
elif sum < 0:
sum = 0
正在抛出那个未定义的变量通知,因为它被放置在条件之外并且一旦页面被加载。
这就是我所做的一切。
我发布了这个,因为我之前做了很多评论。
因此,请使用条件语句/三元运算符。
只要有价值,它就会完成它的工作。
答案 1 :(得分:0)
您的代码有点奇怪,因为您似乎在您正在更新的情况下以及在您没有更新的情况下使用该页面。
试试这个:
<?php
$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'])) {
$purchased = isset($_POST['purchased']) ? $_POST['purchased'] : null;
$due = isset($_POST['due']) ? $_POST['due'] : null;
$sql_part = array();
$prepare = array();
if ($purchased) {
$sql_part[] = 'purchased = :purchased';
$prepare[':purchased'] = $purchased;
}
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;
}
}
}
if (isset($purchased)) {
$purchasedDate = new DateTime($purchased);
}
if (isset($due)) {
$duedate = new DateTime($due);
} else {
$duedate = new DateTime($purchased?:"now");
$duedate->add(new DateInterval('P6Y'));
}
?>
<input type = "hidden" name = "due" value = "<?php echo $duedate->format('Y-m-d'); ?>">