I have a function called updateCatalog($conn, $date) and with this I hope to update a column with date as type on a table in the database.
Problem I'm having is when I do, it sets the date to 0000-00-00 and show -0001-11-30 on my index.php page.
What should I do?
Index.php:
$sql = "SELECT * FROM catalog";
$result = $conn->query($sql);
echo '
<table id = "catalog-table">
<tr class = "column-name">
<th> YEAR </th>
<th> MONTH </th>
<th> TITLE </th>
<th class = "butt-column"> </th>
</tr>
';
while ($row = mysqli_fetch_array($result)) {
$month = date('F', strtotime(str_replace('.', '-', $row['dates'])));
$year = date('Y', strtotime(str_replace('.', '-', $row['dates'])));
echo "<tr>";
echo '<td class = "cent-col">' . $year . '</td>';
echo '<td class = "cent-col">' . $month . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '
<td class = "cent-col">
<form action="editcat.php" method="POST">
<input type = "hidden" name = "id" value = "' . $row['id'] . '"/>
<input type = "hidden" name = "date" value = "' . $row['dates'] . '"/>
<input type = "hidden" name = "title" value = "' . $row['title'] . '"/>
<input class = "edit-butt" type = "submit" name = "submit-butt" value = "edit">
</form>
</td>
';
echo "</tr>";
}
echo "</table>";
editcat.php:
<section class = "form-sec" id = "editcat-form-sec">
<div class = "form-div">
<form id="editcat-form" action="includes/editcat.inc.php" method="post">
<?php
if (isset($_POST['id'])) {
$id = $_POST['id'];
$date = $_POST['date'];
$title = $_POST['title'];
echo '<input type="hidden" name="id" value="' . $id . '"/>';
echo '<input id = "date-in" type="date" name="date" value="' . $date . '" placeholder="' . $date . '">';
echo '<input id = "title-in" type="text" name="title" value="' . $title . '" placeholder="' . $title . '">';
}
?>
<button type="submit" name="submit">Update</button>
</form>
</div>
</section>
includes/editcat.inc.php:
<?php
if (isset($_POST['submit'])) {
$id = $_POST['id'];
$date = $_POST['date'];
$title = $_POST['title'];
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
updateCat($conn, $id, $date, $title);
header("location: ../index.php");
exit();
}
else {
header("location: ../index.php");
exit();
}
includes/functions.inc.php:
function updateCat($conn, $id, $date, $title) {
$sql = "UPDATE catalog SET dates = $date, title = \"$title\" WHERE id = $id;";
if ($conn -> query($sql) === TRUE) {
header("location: ../index.php");
exit();
}
else {
header("location: ../index.php");
exit();
}
}
What I have tried:
I tried putting these in the sql function instead of $date
$newdate = date($date);
$newdate2 = date('Y-m-d',strtotime($row['dates']));
$newdate3 = STR_TO_DATE($row['$date'], '%m/%d/%Y');