Jump to content

Inserting pre-determined date into datetime via mysql/php


myristate

Recommended Posts

I apologise if this is in the wrong forum, It's php and mysql so wasn't entirely sure. Anyways lemme get to the point.

 

I have a web form that allows the user to input a date which i get and process via

 

$myRaidDate=$_POST["Year"]."-".$_POST["Month"]."-".$_POST["Day"]." 00:00:00";

 

The idea was to format it to look like datetime in mysql i.e YYYY-MM-DD HH:MM:SS

 

I then attempted to insert the values into the database with:

 

$varRaidDate = $myRaidDate;
mysql_query("INSERT INTO raids (RaidNum, RaidDate, RaidLength) VALUES ('".$newRaidNum."','".$varRaidDate."','".$_POST["Length"]."')");

 

The values

 

$newRaidNum

and

$_POST["Length"]

are both inputting correctly. To be honest i didn't think this approach of formatting $myRaidDate would work and it doesn't it just inputs a default of 0000-00-00 00:00:00 . I assume it needs to be inserted using a strtotime or something but i really don't know how to do it. Could anybody show me how to use the values i get in

$_POST["Year"]."-".$_POST["Month"]."-".$_POST["Day"]

and input it into the datetime the hours, mins and secs aren't important at this stage so I'm just looking for them to be set to 00:00:00

 

Also if i did not mention it already the filed "RaidDate" has a type value of "datetime" in mysql

 

Anyways thanks for your time

 

Link to comment
Share on other sites

Try something like:

 

$myRaidDate = strtotime("{$_POST['Year']}/{$_POST['Month']}/{$_POST['Day']}");

Then, in your query:

mysql_query("INSERT INTO raids (RaidNum, RaidDate, RaidLength) VALUES ('$newRaidNum', FROM_UNIXTIME('$varRaidDate'), '{$_POST["Length"]}')");

 

You don't need all that crazy quoting and concatenation and stuff.  Variables interpolate into double quoted strings.

Link to comment
Share on other sites

Try something like:

 

$myRaidDate = strtotime("{$_POST['Year']}/{$_POST['Month']}/{$_POST['Day']}");

Then, in your query:

mysql_query("INSERT INTO raids (RaidNum, RaidDate, RaidLength) VALUES ('$newRaidNum', FROM_UNIXTIME('$varRaidDate'), '{$_POST["Length"]}')");

 

You don't need all that crazy quoting and concatenation and stuff.  Variables interpolate into double quoted strings.

 

I Appreciate your help DarkWater but after trying that an echo print out before the insert quesry shows the date to be 1199404800 which seems correct for unix time however when i check the database the datetime has actually inserted a NULL into the field. Any suggestions

Link to comment
Share on other sites

Seems the NULL part was my fault i was trying to run the file directly which ran the insert sql part without any valid details. i was only then when i ran it through the web form did it work correctly.

 

This brings me onto another question how to you stop someone directly viewing an include file and causing execution of code as ive done. I know you can use define or somethign at the top but ive only glanced at it before i have no iea how to implement.

Link to comment
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.