Date / time condition

I have this code and I want to execute an sql query only if the requested operation is performed on a specific date. (for example, if I set the date on 05/04/2015, the request will be made only on that day, if I try to make a request on 05/05/2015, nothing happens. Thanks in the advice

$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);

      

+3


source to share


3 answers


$date = "2015-05-05";
$today = date('Y-m-d');

if ($date == $today){

$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);

}

      



+3


source


You will want to wrap your code in a statement that actually checks if the view date is valid:

$check_date = (some date as a string);
$check_date = date('Y-m-d', strtotime($check_date));
$current_date = date('Y-m-d');

if ($check_date == $current_date) {
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);
}

      



Be aware that this example assumes $ check_date is originally a string.

+1


source


Use PDO. Then check the date like numbers and LPB1l.

$box = $_GET['box3'];
$number = $_GET['nr'];

try {
    $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
    $conn->exec("SET CHARACTER SET utf8");  
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "
    UPDATE `nume`   
    SET `venit3` = :box,    
    WHERE `nr` = :number
    "; 

    $statement = $conn->prepare($sql);

    $statement->bindValue(":box", $box);
    $statement->bindValue(":number", $number);   

    $count = $statement->execute();    

}
    catch(PDOException $e) {
    echo $e->getMessage();
}

      

0


source







All Articles