Hi all,
I plan on deleting records which are older than one week from my MySQL database.I do not have access to cron on my current server so I plan on using cronless.com to schedule a request on the following php script.
<?php
$cron_id = $_GET['comm'];
if($cron_id == 20){
require_once ('db_connect.php');
$stmt = mysqli_prepare($conn, "DELETE FROM dt_table WHERE CreatedOn < DATE_SUB(NOW(), INTERVAL 1 WEEK");
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
echo"success";
}
else{
echo"fail";
}
?>
Basically, the script above is working fine, but from my knowledge it is not safe as I am not preparing the statement correctly using variables. Whenever I tried the following, statement below, it deleted all the records
<?php
$cron_id = $_GET['comm'];
if($cron_id == 20){
require_once ('db_connect.php');
$q = "DATE_SUB(NOW(), INTERVAL 1 WEEK)";
$stmt = mysqli_prepare($conn, "DELETE FROM dt_table WHERE CreatedOn < ?");
mysqli_stmt_bind_param($stmt, "s", $q);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
echo"success";
}
else{
echo"fail";
}
?>
Can anyone let me know what im doing wrong?
Thanks you for your time.
Ryan