I have something weird about my system. Currently, I developed a system that enables users to update the photo by choose from files and upload to the database.
The problem is after I click "Please select" to select the photo and click "Upload", an alert will show "Photo updated!" when I click "OK" on that alert, it will display:
PHP Notice: Undefined index: report_id in C:\inetpub\wwwroot\tgotworker_testing\pages\dashboard\engineer\view_task\view_task.php on line 7
PHP Notice: Undefined variable: report_date in C:\inetpub\wwwroot\tgotworker_testing\pages\dashboard\engineer\view_task\view_task.php on line 178
PHP Notice: Undefined variable: ot_start in C:\inetpub\wwwroot\tgotworker_testing\pages\dashboard\engineer\view_task\view_task.php on line 182
PHP Notice: Undefined variable: ot_end in C:\inetpub\wwwroot\tgotworker_testing\pages\dashboard\engineer\view_task\view_task.php on line 185
.....
But, when I check at the database, the photo updated! Can I know why?
Below is my code.
<?php
require_once "../../../../config/configPDO.php";
require_once "../../../../config/check.php";
$team_n = $_SESSION['team_n'];
$report_id = $_GET['report_id'];
$sql = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid LEFT JOIN ot_team ON ot_team.team_id = ot_users.team_id WHERE report_id = :report_id";
$query = $conn->prepare($sql);
$query->execute(array(':report_id' => $report_id));
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$report_id = $row["report_id"];
$report_date = $row["report_date"];
$task_name = $row["task_name"];
$photo_before = $row["photo_before"];
$photo_after = $row["photo_after"];
$ot_start = $row["ot_start"];
$ot_end = $row["ot_end"];
$time_photo_before = $row["time_photo_before"];
$time_photo_after = $row["time_photo_after"];
$report_status = $row["report_status"];
}
?>
<table align="center" width="100%">
<tr>
<td width="20%"><b>Date Report</b></td>
<td width="80%" colspan='3'><?php echo $report_date; ?></td>
</tr>
<tr>
<form action="update_task_name.php" method="POST">
<td><b>Task Name</b></td>
<td colspan = '2'><input type="text" class="form-control" name="task_name" value="<?php echo $task_name; ?>"/></td>
<input type="hidden" name="report_id" value="<?php echo $report_id ?>">
<td><input type="submit" class="btn btn-primary btn-block" value = "Save" onclick="confirm('Are you sure?')"></td>
</form>
</tr>
<form action="update_photo_before.php" method="post" id="form" enctype="multipart/form-data">
<tr>
<td rowspan = '3'><b>Before Task</b></td>
<?php
if(!isset($photo_before) || empty($photo_before)) {
echo "<td colspan='2' rowspan='2'><img src='../../../../images/faces/noimage.png'/></td>";
}else{
echo "<td colspan='2' rowspan='2'><img src='data:image/jpeg;base64,".$photo_before."'/></td>";
}
?>
<td colspan ='2'><input type="file" name="uploadFile" value="" /></td>
</tr>
<tr>
<input type="hidden" name="report_id" value="<?php echo $report_id ?>">
<td><input type="submit" name="submitBtn" value="Upload" onclick="confirm('Are you sure?')"/></td>
</tr>
</form>
<tr>
<?php
if($time_photo_before == null) {
$time_photo_before = " -";
}else{}
?>
<td colspan='3'><?php echo "Time: <b>" .$time_photo_before."</b>"; ?></td>
<tr>
<td rowspan = '3'><b>After Task</b></td>
<?php
if(!isset($photo_after) || empty($photo_after)) {
echo "<td colspan='2' rowspan='2'><img src='../../../../images/faces/noimage.png'/></td>";
}else{
echo "<td colspan='2' rowspan='2'><img src='data:image/jpeg;base64,".$photo_after."'/></td>";
}
?>
<td colspan ='2'></td>
</tr>
<tr>
<td></td>
</tr>
</tr>
<tr>
<?php
if($time_photo_after == null) {
$time_photo_after = " -";
}else{}
?>
<td colspan='3'><?php echo "Time: <b>" .$time_photo_after."</b>"; ?></td>
</tr>
<tr>
<td><b>OT - From</b></td>
<td><?php echo $ot_start; ?></td>
<form action="update_ot_start.php" method="POST">
<td width ='35%'>
<select class="form-control" id="exampleFormControlSelect1" name="ot_start">
<option value="12:00" <?php echo $ot_start == '12:00'? 'selected': '';?> >12:00</option>
<option value="12:30" <?php echo $ot_start == '12:30'? 'selected': '';?> >12:30</option>
<option value="13:00" <?php echo $ot_start == '13:00'? 'selected': '';?> >13:00</option>
</select>
<input type="hidden" name="report_id" value="<?php echo $report_id ?>">
</td>
<td width ='30%'><input type="submit" class="btn btn-primary btn-block" value = "Save" onclick="confirm('Are you sure?')"></td>
</form>
</tr>
<tr>
<td><b>OT - To</b></td>
<td width ='35%'><?php echo $ot_end; ?>
</td>
<form action="update_ot_end.php" method="POST">
<td width ='35%'>
<select class="form-control" id="exampleFormControlSelect1" name="ot_end">
<option value="15:30" <?php echo $ot_end == '15:30'? 'selected': '';?> >15:30</option>
<option value="16:00" <?php echo $ot_end == '16:00'? 'selected': '';?> >16:00</option>
</select>
<input type="hidden" name="report_id" value="<?php echo $report_id ?>">
</td>
<td width ='30%'><input type="submit" class="btn btn-primary btn-block" value = "Save" onclick="confirm('Are you sure?')"></td>
</form>
</tr>
<tr>
<td><b>Report Status</b></td>
<?php
if($report_status=="Pending")
{
$color="color:blue";
}
else
{
$color="color:green";
}
echo "<td style='$color' colspan='3'><strong>" . $report_status ."</strong></td>";
?>
</tr>
</table>
update_photo_before.php
<?php
require_once '../../../../config/configPDO.php';
if(isset($_FILES['uploadFile']['name']) && !empty($_FILES['uploadFile']['name'])) {
//Allowed file type
$allowed_extensions = array("jpg","jpeg","png","gif");
//File extension
$ext = strtolower(pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION));
//Check extension
if(in_array($ext, $allowed_extensions)) {
//Convert image to base64
$encoded_image = base64_encode(file_get_contents($_FILES['uploadFile']['tmp_name']));
$encoded_image = $encoded_image;
$query = "UPDATE ot_report SET photo_before = '$encoded_image' WHERE report_id = :report_id";
$sql = $conn->prepare($query);
$sql->bindParam(':report_id', $_POST['report_id']);
$sql -> execute();
echo "File name : " . $_FILES['uploadFile']['name'];
echo "<br>";
if($sql->rowCount() > 0 ) {
echo "
<script>alert('Photo updated!')</script>
<script>window.location = 'view_task.php'</script>
";
$last_insert_id = $conn-> lastInsertId();
} else {
echo "
<script>alert('Fail to updated')</script>
<script>window.location = 'view_task.php'</script>
";
}
} else {
echo "File not allowed";
}
}
?>