Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. if($tablename=='trips')
  2. {
  3. // insert any blank dates
  4. $datemax = mysql_result(mysql_query("select max(date) from trips",$con),0);
  5. $dategiven = $_POST['date'];
  6. $period = new DatePeriod(
  7. new DateTime($datemax),
  8. new DateInterval('P1D'),
  9. new DateTime($dategiven)
  10. );
  11. foreach ($period as $date) {
  12. if($date->format('Y-m-d') != $dategiven AND $date->format('Y-m-d') != $datemax) {
  13. mysql_query("INSERT into trips (date, kms) VALUES (\"".$date->format('Y-m-d')."\",0)",$con);
  14. };
  15. }
  16. }
  17. // insert the form data
  18.  
  19. mysql_query('DELETE FROM '.$tablename.' WHERE date = "'.$_POST['date'].'"',$con); // delete any existing data on that date to prevent multiple entries
  20.  
  21. $col_name = ""; $newvalue = "";
  22. foreach ($_POST as $key=>$value) { // this creates the list for the sql insert string
  23. if($key!="formID")
  24. {
  25. $col_name .= $key.",";
  26. if ($value != '')
  27. {
  28. $newvalue .= chr(39).$value.chr(39).",";
  29. }
  30. else
  31. {
  32. $newvalue .="NULL,";
  33. }
  34. }
  35. }
  36.  
  37.  
  38. $col_name = substr($col_name,0,-1); $newvalue = substr($newvalue,0,-1); // removes the last comma
  39.  
  40. $sql = "INSERT INTO $tablename ($col_name) VALUES ($newvalue)";
  41.  
  42. if (!mysql_query($sql,$con))
  43. {
  44. die('Error: ' . mysql_error());
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement