myristate Posted January 4, 2009 Share Posted January 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/139382-inserting-pre-determined-date-into-datetime-via-mysqlphp/ Share on other sites More sharing options...
DarkWater Posted January 4, 2009 Share Posted January 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139382-inserting-pre-determined-date-into-datetime-via-mysqlphp/#findComment-729013 Share on other sites More sharing options...
myristate Posted January 4, 2009 Author Share Posted January 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/139382-inserting-pre-determined-date-into-datetime-via-mysqlphp/#findComment-729015 Share on other sites More sharing options...
DarkWater Posted January 4, 2009 Share Posted January 4, 2009 Change your querying to: $query = "INSERT INTO raids (RaidNum, RaidDate, RaidLength) VALUES ('$newRaidNum', FROM_UNIXTIME('$varRaidDate'), '{$_POST["Length"]}')"; echo "Query: $query"; $result = mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/139382-inserting-pre-determined-date-into-datetime-via-mysqlphp/#findComment-729017 Share on other sites More sharing options...
myristate Posted January 4, 2009 Author Share Posted January 4, 2009 It only seems to add the date when i use the echo "Query: $query"; if i comment it out it sets it to NULL why is that i dont understand and i dont actually want it to print anything to the screen. Quote Link to comment https://forums.phpfreaks.com/topic/139382-inserting-pre-determined-date-into-datetime-via-mysqlphp/#findComment-729022 Share on other sites More sharing options...
myristate Posted January 4, 2009 Author Share Posted January 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139382-inserting-pre-determined-date-into-datetime-via-mysqlphp/#findComment-729034 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.