SaucedApples SaucedApples - 4 months ago 5
PHP Question

Undefined Variable - Works

I have a script which updates a date field in my database. (

purchased
).

I'm also using that data in another part which updates a second date field which takes the input date and add's 6 years
$duedate
. It works just fine but I get the Undefined Variable error for the variable
purchased
.

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


I have tried defining it using the below, but it stops the second field being updated and does not throw any errors.

$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'));


Code

$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'); ?>">


The file is 261 lines so I've included the relevant segments and can add full file if needed.

Answer

These lines at the end of your file:

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

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

are throwing that undefined variable notice because it's placed outside conditionals and as soon as the page is loaded.

That's what I make out of all this.

I posted this, since I made a comment much earlier about it.

Therefore, use a conditional statement/ternary operator.

It will do its job as soon as there is a value for it.