0

I have searched and tried a variety of solutions but don't seem to be getting this. I have an event which happens every week on the same day of the week. I want to be able to add it to the MySQL database so that the same exact info goes in for 16 different weeks, meaning only the eventDate field will change.

Currently the form to add the event has a checkbox labelled 'recurring' and a date field for the initial event date. I am then trying to insert the initial record with the date entered before looping through another 15 inserts with the date set to be 1 week later each time but the rest of the data is as originally entered.

I have assigned variables from the form submission and then am doing the insert according to whether the 'recurring' field is set to 1. I am not getting an error but the event is being entered with a date of 0000-00-00. It works fine when I remove the 'recurring' test and simply do a straight insert.

$id=$_POST["eventid"];
$eventHeadingTemp = $_POST["eventHeading"];
$eventHeading = mysql_escape_string($eventHeadingTemp);
$eventTypeId = $_POST["eventTypeId"];
$eventInfoTemp = $_POST["eventInfo"];
$eventInfo = mysql_escape_string($eventInfoTemp);
$eventDate = $_POST["eventDate"];
$recurring = $_POST["recurring"];
$eventStartTime = $_POST["eventStartTime"];
$eventImagesDelete=$_POST["eventImagesDelete"];

if ($id==0) {
    if ($recurring==1) {

    $SQL="INSERT INTO events (eventHeading, eventTypeId, eventInfo, eventDate, recurring, eventStartTime, eventImage, eventThumb) VALUES ('$eventHeading', '$eventTypeId', '$eventInfo', '$eventDate', '$recurring', '$eventStartTime', '$eventImage', '$eventThumb')";

        for ($i=0; $i<16; $i++) {

        $eventDate = strtotime('+1 week' , $eventDate);

        $SQL="INSERT INTO events (eventHeading, eventTypeId, eventInfo, eventDate, recurring, eventStartTime, eventImage, eventThumb) VALUES ('$eventHeading', '$eventTypeId', '$eventInfo', '$eventDate', '$recurring', '$eventStartTime', '$eventImage', '$eventThumb')";
        }
    }       

    else {
    $SQL="INSERT INTO events (eventHeading, eventTypeId, eventInfo, eventDate, recurring, eventStartTime, eventImage, eventThumb) VALUES ('$eventHeading', '$eventTypeId', '$eventInfo', '$eventDate', '$recurring', '$eventStartTime', '$eventImage', '$eventThumb')";
    }

}

UPDATE: I see this question being viewed occasionally but not answered just yet so want to add that if I didn't clearly present the problem please feel free to say so and ask for more / better information. Thanks so much...

tadman
  • 208,517
  • 23
  • 234
  • 262
  • **WARNING**: If you're just learning PHP, please, do not learn the obsolete `mysql_query` interface. It's awful and is being removed in future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). A guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. Always be absolutely **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will have severe [SQL injection bugs](http://bobby-tables.com/). – tadman Jan 27 '15 at 19:18
  • OK well thanks for the head's up on that - doesn't seem like that will change the issue i'm having though... – GlobalVillage Jan 27 '15 at 19:21
  • What does `strtotime('+1 week' , $eventDate)` return? That could be a garbage date and will ruin your query. – tadman Jan 27 '15 at 19:22
  • It returns a numeric string - 606815. Without the strtotime line it returns a proper date as per the initially entered date field. – GlobalVillage Jan 27 '15 at 19:30
  • If that's in UNIX-time format as the documentation for [`strtotime`](http://php.net/manual/en/function.strtotime.php) suggests, then that's in the 1970s. You'll probably need to use that function correctly, and then call [`strftime`](http://php.net/manual/en/function.strftime.php) to format it for insertion in the database. It's worth noting that none of this would be an issue if you were using a [development framework](http://codegeekz.com/best-php-frameworks-for-developers/) like [Laravel](http://laravel.com/) that does all of this for you automatically. – tadman Jan 27 '15 at 19:34
  • It appears the issue is that I'm not using it correctly and that's where my question comes in, so telling me I probably need to use the function correctly is probably exactly right - I just don't know what I'm doing wrong... – GlobalVillage Jan 27 '15 at 19:41
  • Both those functions are well documented with many examples in the links I provided. You haven't shown what your input data looks like so I can only guess about possible solutions unless you provide more specifics. I don't know what you're doing wrong because you aren't giving all the details. – tadman Jan 27 '15 at 19:50
  • Sorry - I feel like we're going in circles somewhat here - I appreciate you taking the time to help but I'm not sure what info to provide differently. I have looked at examples of the how to use the function and seem to be not getting it correct. I said that when I don't use the strtotime function it returns the properly entered date but when I do it returns the string 606815 and I also mentioned that it inserts the record with a 0000-00-00 date when I enter a record with the 'Recurring' field checked off. If 'Recurring' is not checked off the record inserts properly. – GlobalVillage Jan 27 '15 at 19:57
  • Think of it from the perspective of someone who's just read your question and has no other information. For example, what is `$eventDate`? I have no idea and no way of finding out. It could be a date. It could be a number. I think your problem stems from that. – tadman Jan 27 '15 at 20:22

0 Answers0