PHP Function Will Not Insert Into MySql

Can someone please tell me why my php function isn’t inserting into Mysql?

[php]<?php

// post data
$jobname = $_POST[‘jobname’];
$begin = $_POST[‘begin’];
$end = $_POST[‘end’];
$customer = $_POST[‘customer’];
$zip = $_POST[‘zip’];
$city = $_POST[‘city’];
$workdesc = $_POST[‘workdesc’];
$phone = $_POST[‘phone’];
$email = $_POST[‘email’];

// date loop
function dates() {
$begin = $_POST[‘begin’];
$end = $_POST[‘end’];
$begin2 = new DateTime( $begin );
$end2 = new DateTime( $end );
$end2 = $end2->modify( ‘+1 day’ );

$interval = new DateInterval(‘P1M1D’);
$daterange = new DatePeriod($begin2, $interval ,$end2);

foreach($daterange as $date){
echo $date->format(“m/d/Y”) . “
”;
}

}

// mysql connection

$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “test”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = “INSERT INTO data (promiseddate)
VALUES (’”.dates()."’)";

if ($conn->query($sql) === TRUE) {
echo “New record created successfully”;
} else {
echo "Error: " . $sql . “
” . $conn->error;
}

$conn->close();

?>[/php]

It should insert multiple dates from the date interval function. I’m lost!!

Never mind everyone I got it, just in case anyone else needs it this is what I did without the function,

[php]<?php

// post data
$jobname = $_POST[‘jobname’];
$begin = $_POST[‘begin’];
$end = $_POST[‘end’];
$customer = $_POST[‘customer’];
$zip = $_POST[‘zip’];
$city = $_POST[‘city’];
$workdesc = $_POST[‘workdesc’];
$phone = $_POST[‘phone’];
$email = $_POST[‘email’];

// date loop
$begin2 = new DateTime( $begin );
$end2 = new DateTime( $end );
$end2 = $end2->modify( ‘+1 day’ );

$interval = new DateInterval(‘P1M1D’);
$daterange = new DatePeriod($begin2, $interval ,$end2);

foreach($daterange as $date){

// mysql connection

$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “test”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = “INSERT INTO data (promiseddate)
VALUES (’”.$date->format(‘m/d/Y’)."’)";

if ($conn->query($sql) === TRUE) {
echo “New record created successfully”;
} else {
echo "Error: " . $sql . “
” . $conn->error;
}
}

$conn->close();

?>[/php]

You are storing your dates in the wrong format which also means your column type is wrong. Should be DATE column in format YYYY-MM-DD.

Real Linux developers prefer this format, 1450848655.

:stuck_out_tongue:

You mean the ones that want to throw out all the built in date functions?

I guess you missed the joke…

Ineedhelp, You should take your connection to the database and closing of it outside the loop. It just wastes
time opening and closing it for each record you are inserting! Instead, open the connection, loop thru all the
dates inserting them and then close it. One open and close for the entire function.

Are you sure you got it to work the way you want it to? For I was playing around with your code and came up with this ->
[php]<?php
$begin = “2015-12-01”;
$end = “2015-12-31”;
// date loop
$date_start = new DateTime( $begin, new DateTimeZone(“America/Detroit”) );
$date_end = new DateTime( $end, new DateTimeZone(“America/Detroit”) );
$date_end = $date_end->modify( ‘+1 day’ );

$interval = new DateInterval(‘P1D’); // Basically the only difference:
$date_range = new DatePeriod($date_start, $interval ,$date_end);

echo “

” . print_r($date_range, 1) . “
”;
foreach ($date_range as $dt) {
echo $dt->format(“Y-m-d H:i:s”) . “
\n”;
}[/php]
I hate when I dream about code in my sleep. >:( ;D Anyways it got me thinking that it’s probably the way you want it, but if you want it for everyday between the dates then it would have to be the way I do it.
Sponsor our Newsletter | Privacy Policy | Terms of Service