Jump to content

Date inserts 00


Go to solution Solved by Psycho,

Recommended Posts

<?php
  $dbhost = "localhost";
  $dbname = "mydb";
  $dbusername = "xxxx";
  $dbpassword = "mypass";

if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
    $pdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $pdo->prepare("INSERT INTO xphias (pastorsname, ministriesname, xstartdate,clientname, url) VALUES (?,?,?,?,?)");

    for ($i = 0; $i < count($_POST['pastorsname']); $i++)
        {
        $stmt->execute(array(
       $pastorname   =       $_POST['pastorsname'][$i],
       $ministryname =       $_POST['ministriesname'][$i],
       $startdate =    "'".date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['xstartdate'][$i])))."'",
       $clientsname =     $_POST['clientname'][$i],
       $video_url =     $_POST['url'][$i],

        ));
        }
        echo "Success!";
    }
?>

Greetings again,

 

The following code is fairly working except that date field is inserting 0000-00-00

 

Any ideas why?

 

Please see attached code.

 

Thanks in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/305601-date-inserts-00/
Share on other sites

MySQL has a number of different operational modes.

 

The default mode is permissive when it comes to invalid Dates. MySQL will create an internal warning, and accept the insert or update with the invalid date, but will store a date value of '0000-00-00'.

 

I don't know what type of column you are using, but if you want to include a time component, you need to use a Datetime or a Timestamp.

 

It's a bit hard from your code to tell what the actual string is that you are generating, but it seems to be invalid in some way.

Link to comment
https://forums.phpfreaks.com/topic/305601-date-inserts-00/#findComment-1553593
Share on other sites

  • Solution

This 'array' makes no sense. At least I have never seen assignments used within an array like that. Since you would be overriding the assignments on each iteration of the loop it makes even less sense why you would have assignments (since they are not used).

$stmt->execute(array(
       $pastorname   =       $_POST['pastorsname'][$i],
       $ministryname =       $_POST['ministriesname'][$i],
       $startdate =    "'".date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['xstartdate'][$i])))."'",
       $clientsname =     $_POST['clientname'][$i],
       $video_url =     $_POST['url'][$i],
 
        ));

But, part of your problem is that you are appending a single quote mark to the beginning and end of the date string. You don't quote the values that will be used in prepared statements. Try this:

for ($i = 0; $i < count($_POST['pastorsname']); $i++)
{
    //Create date string
    $startdate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['xstartdate'][$i])));
    echo "Input: {$_POST['xstartdate'][$i]}, Start date: {$startdate}<br>\n"; //For debugging
    //Create values array
    $values = array(
        $_POST['pastorsname'][$i],
        $_POST['ministriesname'][$i],
        $startdate,
        $_POST['clientname'][$i],
        $_POST['url'][$i]
    );
    //Execute statement
    $stmt->execute($values);
}

Does it work (without the extra quotes)? If not, what does the output show?

 

By the way, you should create your fields something like this

name="record[1][pastorsname]"

Then, in your code you would just do

foreach($_POST['record'] as $record)
{
  //Fields are then referenced like this
  //$record['pastorsname']
  //$record['ministriesname']
}
Edited by Psycho
Link to comment
https://forums.phpfreaks.com/topic/305601-date-inserts-00/#findComment-1553597
Share on other sites

                    <td><INPUT TYPE="TEXT" NAME="pastorsname[]" SIZE="14"></td>
                    <td><INPUT TYPE="TEXT" NAME="ministriesname[]" SIZE="14"></td>
                    <td><INPUT TYPE="TEXT" NAME="xstartdate[]" SIZE="10"></td>
                    <td><INPUT TYPE="TEXT" NAME="clientname[]" SIZE="14"></td>
                    <td><INPUT TYPE="TEXT" NAME="url[]" SIZE="14"></td>

but you are suggesting to have them like below?

                    <td><INPUT TYPE="TEXT" NAME="record[1][pastorsname]" SIZE="14"></td>
                    <td><INPUT TYPE="TEXT" NAME="record[2][ministriesname]" SIZE="14"></td>
                    <td><INPUT TYPE="TEXT" NAME="record[3][xstartdate]" SIZE="10"></td>
                    <td><INPUT TYPE="TEXT" NAME="record[4][clientname]" SIZE="14"></td>
                    <td><INPUT TYPE="TEXT" NAME="record[5][url]" SIZE="14"></td>

Thank you very much psycho for such detailed explanations.

 

Initially, I didn't have the assignments.

 

I added them because I was looking for a way to format the date field.

 

Just to clarify, this is my current markup:

Link to comment
https://forums.phpfreaks.com/topic/305601-date-inserts-00/#findComment-1553603
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.