phpnewbie432 Posted January 22, 2009 Share Posted January 22, 2009 I have a silly problem in PHP im trying to move a date filled in on a form into mysql. My problem is in getting php not to mess up my date Here is where it goes wrong: $date = $_POST['date'] The post value contains a date in this format YYYY-MM-DD and whenever i do the above transfer it subtracts the months and the days from the year help Quote Link to comment Share on other sites More sharing options...
gevans Posted January 22, 2009 Share Posted January 22, 2009 try $date = (string) $_POST['date']; Quote Link to comment Share on other sites More sharing options...
phpnewbie432 Posted January 22, 2009 Author Share Posted January 22, 2009 try $date = (string) $_POST['date']; it continues to subtract Quote Link to comment Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 try $date = (string) $_POST['date']; it continues to subtract Then there is something up with your form or your server. Anything coming in from an html form or get/post data should be stored as a string and not acted on literally. Post the form code for us and more of the page where that is happening; Something else is going on. Quote Link to comment Share on other sites More sharing options...
phpnewbie432 Posted January 22, 2009 Author Share Posted January 22, 2009 The naming in this is all shortend from dutch but you should be able to make out where i send and get the values from. I highlighted the date related parts in red. The get values have no effect ive changed the value completely without any change to the outcome. The server im running this on is Local XAMPP default install no changes made. Form: $snr = $_GET['snr']; $soknr = $_GET['son']; $thnr = $_GET['t']; $kwart = $_GET['k']; $goedk = $_GET['g']; $score = $_GET['s']; $datbeh = $_GET['d']; $ec = $_GET['e']; if ($goedk == -1) { $a = SELECTED; } else { $b = SELECTED; } echo " <form method=post action=func_edit_sok.php> <table class=tabel1> <input type=hidden name=snr value=$snr> <tr> <td>SOK NR</td><td><input READONLY type=text class=input3 name=sok value=$soknr></td> </tr> <tr> <td>Thema NR</td><td><input READONLY type=text class=input3 name=thema value=$thnr></td> </tr> <tr> <td>Kwartaal NR</td><td><input type=text class=input2 name=kwart value=$kwart></td> </tr> <tr> <td>Goekgekeurd</td><td><select name=goedk> <option value=-1 $a>Ja</option> <option value=0 $b>Nee</option> </select></td> </tr> <tr> <td>Score</td><td><input type=text class=input2 name=score value=$score></td> </tr> <tr> <td>Datum behaald</td><td><input type=text class=input2 name=dat value=$datbeh> (JJJJ-MM-DD)</td> </tr> <tr> <td>EC</td><td><input type=text class=input2 name=ec value=$ec></td> </tr> <tr> <td><a href=javascript:history.back(-1)>Terug</a></td><td><input type=submit value=Opslaan class=button1></td> </tr> </table> </form>"; Handler: include "dbcon.php"; if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) { //toekennen waardes voor controle gemak + html php tags verwijderen. $goedgk = $_POST['goedk']; $score = $_POST['kwart']; $snr = $_POST['snr']; $datbeh = (string) $_POST['dat']; $ec = $_POST['ec']; $thema = $_POST['thema']; $kwart = $_POST['kwart']; $klopt = 0; $sok = $_POST['sok']; $res = mysql_query("SELECT * FROM thema WHERE themanr=$thema"); $rel = mysql_fetch_array($res); if($rel['1ekwartgegeven'] == -1 && $kwart == 1){ $klopt = 1; } elseif($rel['2ekwartgegeven'] == -1 && $kwart == 2){ $klopt = 1; } elseif($rel['3ekwartgegeven'] == -1 && $kwart == 3){ $klopt = 1; } elseif($rel['4ekwartgegeven'] == -1 && $kwart == 4){ $klopt = 1; } else { echo "Dit thema is niet beschikbaar in kwartaal $kwart de wijziging van het kwartaal kan niet worden doorgevoerd drup op Terug<br />"; echo "<a href=javascript:history.back(-1)>Terug</a>"; } if($klopt == 1){ mysql_query("UPDATE SOK SET Kwartaalnr=$kwart, Goedgekeurd=$goedgk, Score=$score, DatumBehaald=$datbeh, EC=$ec WHERE SOKnr=$sok") or die (mysql_error()); header("Location: index.php?page=ov_s&snr=$snr"); } } Quote Link to comment Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 I would use quotes in your html around the attributes. <td>Datum behaald</td><td><input type='text' class='input2' name='dat' value='$datbeh'> (JJJJ-MM-DD)</td> Try that out and see if that maybe works. I am still unsure on why it would be subtracting each other, if that does not work try to change this part also: $datbeh = (string)$_GET['d']; And see if that works or not. Also you should really use single quotes around your mysql data. mysql_query("UPDATE SOK SET Kwartaalnr='$kwart', Goedgekeurd='$goedgk', Score='$score', DatumBehaald='$datbeh', EC='$ec' WHERE SOKnr='$sok'") or die (mysql_error()); Give those a try and see if that fixes the issue. Quote Link to comment Share on other sites More sharing options...
phpnewbie432 Posted January 22, 2009 Author Share Posted January 22, 2009 I would use quotes in your html around the attributes. <td>Datum behaald</td><td><input type='text' class='input2' name='dat' value='$datbeh'> (JJJJ-MM-DD)</td> Try that out and see if that maybe works. I am still unsure on why it would be subtracting each other, if that does not work try to change this part also: $datbeh = (string)$_GET['d']; And see if that works or not. Also you should really use single quotes around your mysql data. mysql_query("UPDATE SOK SET Kwartaalnr='$kwart', Goedgekeurd='$goedgk', Score='$score', DatumBehaald='$datbeh', EC='$ec' WHERE SOKnr='$sok'") or die (mysql_error()); Give those a try and see if that fixes the issue. doesnt change anything. I changed the form to a GET form and the values all get sent correctly in the URL &dat=2009-11-11 but when they get moved over from the GET to the normal variable it treats it like calculation and makes it 1987 Quote Link to comment Share on other sites More sharing options...
Philip Posted January 22, 2009 Share Posted January 22, 2009 Put this at the top: printf("%s\n", $_GET['dat']); What does that output? Quote Link to comment Share on other sites More sharing options...
phpnewbie432 Posted January 22, 2009 Author Share Posted January 22, 2009 Put this at the top: printf("%s\n", $_GET['dat']); What does that output? it give me the correct value i filled in 2009-11-11 Quote Link to comment Share on other sites More sharing options...
phpnewbie432 Posted January 22, 2009 Author Share Posted January 22, 2009 i just tried echo on the $datbeh value after the get had been applied and it worked fine so its not the variable reasignment thats making it subtract. Could it be the "UPDATE" command in mysql that is doing the subtracting? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 22, 2009 Share Posted January 22, 2009 Date values are strings and must be quoted so that they are not teated as a math expressions. The query code that premiso posted does have single-quotes around the date value, which should work. Did you use his query code? Quote Link to comment Share on other sites More sharing options...
phpnewbie432 Posted January 22, 2009 Author Share Posted January 22, 2009 i used some of the sollutions posted here and i got it to work, thank you all for posting i very much appreciate your help. you may consider this topic solved. Quote Link to comment 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.