2

EDIT: So I looked up PDO Insert statements. Still kind of confusing. so now my code reads:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test14.php</title>
</head>
<?php
$servername = "server";
$username = "username";
$password = "password";
$dbname   = "dbname";

 //Setup for endDate value 
date_default_timezone_set("America/Denver");
$startDate = new DateTime('12:00:00');
  
  echo "<p>Today is: {$startDate->format( 'l Y-m-d g:i:s' )}</p>";
  
  // N gives a numeric day to the days of the week
  // 2 means Tuesday
  while ( $startDate->format( 'N' ) != 2 )

  $startDate->modify( '+1 days' );
  
  echo "<p>Start date is: {$startDate->format('l Y-m-d g:i:s')}</p>";

  //Set endDate at Tuesday
  $endDate = $startDate ->modify("+2 Weeks");
  echo "<p>End date is: {$endDate->format('l Y/m/d g:i:s')}</p>";


try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
     // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = $conn->prepare("INSERT INTO wp_awpcp_ads (ad_enddate)  
VALUES (:end_date)
");
  $sql->bindParam(':end_date',strtotime($endDate), PDO::PARAM_STR);
        // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>
<body>
</body>
</html>

I'm getting 2 warnings:

Warning: strtotime() expects parameter 1 to be string, object given in [site_url]/test14.php on line 40

Warning: PDO::exec() expects parameter 1 to be string, object given in [site_url]/test14.php on line 42

New record created successfully

and I'm still getting all zeros in my table. Does it matter if it's a wordpress table? I'm not having any issues with the now() functions just DateTime()functions. Is there another way to setup a date like this?


I have been trying to find a way to add a custom end date value to a mysql database field with the type "DateTime"

Specifically, the customer wants all ads to be "ended" on a tuesday at noon following 2 weeks of activity. so far I've been helped at phphelp forums and have this snippet:

    <?php
    $servername = "server";
    $username = "username";
    $password = "password";
    $dbname = "dbname";

//Setup for endDate value 
date_default_timezone_set("America/Denver");
$startDate = new DateTime('12:00:00');
  
  echo "<p>Today is: {$startDate->format( 'l Y-m-d H:i:s' )}</p>";
  
  // N gives a numeric day to the days of the week
  // 2 means Tuesday
  while ( $startDate->format( 'N' ) != 2 )

$startDate->modify( '+1 days' );
  
  echo "<p>Start date is: {$startDate->format('l Y-m-d H:i:s')}</p>";

  //Set endDate at Tuesday
  $endDate = $startDate ->modify("+2 Weeks");
  echo "<p>End date is: {$endDate->format('l Y/m/d H:i:s')}</p>";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
     // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "INSERT INTO wp_awpcp_ads 
    (
    ad_contact_name,
    ad_contact_email,
    ad_contact_phone,
    ad_title,
    ad_category_id,
    ad_item_price,
    ad_details,
    disabled,
    disabled_date,
    ad_postdate,
    ad_startdate,
    ad_last_updated,
    ad_enddate
  )  
VALUES
  (
  '$_POST[name]',
  '$_POST[email]',
  '$_POST[phone]',
  '$_POST[title]',
  '$_POST[category]',
  '$_POST[price]',
  '$_POST[details]',
  1,
  now(),
  now(),
  now(),
  now(),
  $endDate->format('Y-m-d H:i:s'))";
        // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;

This works fine except my ad_enddate row shows 0000-00-00 00:00:00 and i'm at wit's end to solve this issue. I've changed my code to msqli to pdo, and nothing. I'm completely new to php and need this by last week.

Community
  • 1
  • 1

2 Answers2

0

this enter link description here can help You with date problem, I think it's due to $end_date format.

And this is great answer about SQL Injections in PHP :) enter link description here

Community
  • 1
  • 1
0

hey everyone thanks for the help. I was able to get it working after rewriting my code. the key I was missing to make it work is: '".$tuesday."'

here's my final code:

//----------------------------------------------------
//today is Tuesday
if (2 == date('N')){
$tuesday = time('12:00:00');
}else{
$tuesday = strtotime('last Tuesday');
}

$tuesday = strtotime('+2 week', $tuesday);
//echo date('d-m-Y', $tuesday) . '<br>';
 $tuesday = date("Y-m-d H:i:s", $tuesday);
//----------------------------------------------------

if(isset($_REQUEST['submit_ad']))
{

 // prepare and bind
$stmt = $conn->prepare("INSERT INTO wp_awpcp_ads
(
ad_contact_name,
ad_contact_email,
ad_contact_phone,
ad_category_id,
ad_title,
ad_details,
disabled,
disabled_date,
ad_postdate,
ad_startdate,
ad_enddate
)  
VALUES
(?,?,?,?,?,?,1,now(),now(),now(),'".$tuesday."')");
$stmt->bind_param("ssssss",$name,$phone,$email,$category,
$adtitle,$addetails);

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$category = $_POST['category'];
$adtitle = $_POST['adtitle'];
$addetails = $_POST['addetails'];

$stmt->execute();
echo "New ads created successfully";

}
$conn->close();  
?>