0

I am a beginner in php. I am trying to create table column with month and year name, but unfortunately my code is giving me a error. The same value came again.

ex:,January_2015,March_2015,March_2015,April_2015

It left out February and gave march 2 times. So the same value supplied in to table creation. My sql said "Could not create table: Duplicate column name 'March_2015'".

It happens when I set 29th day of any month. If I set local date 1-28 it works well. Here is my code:

{
include("conn1.php");  
$random =trim($_POST['uid']);
$qP = "SELECT * FROM mayureg WHERE memail='$random'";   
//echo $qP;  
$rsP = mysql_query($qP);   
$row = mysql_fetch_array($rsP);   
extract($row);   
$id = $row['id'];   
$mname = $row['memail'];   

mysql_close();  


$kow=$mname;  
echo $kow;  
//echo "arun $tid";
  1. I am starting here to count month from current month
$date=date('Y-m');   
$d0=DateTime::createFromFormat('Y-m', $date);    
$d0 = $d0->format('F_Y');   

$d1 = DateTime::createFromFormat('Y-m', $date);   
$d1->add(new DateInterval('P1M'));   
$d1 = $d1->format('F_Y');   
$d2 = DateTime::createFromFormat('Y-m', $date);   
$d2->add(new DateInterval('P2M'));    
$d2 = $d2->format('F_Y');   
$d3 = DateTime::createFromFormat('Y-m', $date);   
$d3->add(new DateInterval('P3M'));   
$d3 = $d3->format('F_Y');   
$d4 = DateTime::createFromFormat('Y-m', $date);   
$d4->add(new DateInterval('P4M'));   
$d4 = $d4->format('F_Y');   
$d5 = DateTime::createFromFormat('Y-m', $date);   
$d5->add(new DateInterval('P5M'));   
$d5 = $d5->format('F_Y');   
$d6 = DateTime::createFromFormat('Y-m', $date);   
$d6->add(new DateInterval('P6M'));   
$d6 = $d6->format('F_Y');   
$d7 = DateTime::createFromFormat('Y-m', $date);   
$d7->add(new DateInterval('P7M'));   
$d7 = $d7->format('F_Y');   
$d8 = DateTime::createFromFormat('Y-m', $date);   
$d8->add(new DateInterval('P8M'));   
$d8 = $d8->format('F_Y');   
$d9 = DateTime::createFromFormat('Y-m', $date);   
$d9->add(new DateInterval('P9M'));   
$d9 = $d9->format('F_Y');  
$d10 = DateTime::createFromFormat('Y-m', $date);   
$d10->add(new DateInterval('P10M'));   
$d10 = $d10->format('F_Y');   
$d11 = DateTime::createFromFormat('Y-m', $date);   
$d11->add(new DateInterval('P11M'));   
$d11 = $d11->format('F_Y');   
$d12 = DateTime::createFromFormat('Y-m', $date);   
$d12->add(new DateInterval('P12M'));   
$d12 = $d12->format('F_Y');   

$dbhost = 'localhost';   
$dbuser = 'root';   
$dbpass = '';   

$conn = mysql_connect($dbhost, $dbuser, $dbpass);   
mysql_select_db('phholder');   
if(! $conn )    
{   
  die('Could not connect: ' . mysql_error());   
}
  1. I am using variables to create table
echo 'Connected successfully';   
$sql = 'CREATE TABLE`'.$kow.'`(    
 '.'uid VARCHAR(255),    
 '.'sal VARCHAR(255),    
'.'phnam VARCHAR(255),    
'.'phdob VARCHAR(255),    
'.'gen VARCHAR(255),    
'.'emptype VARCHAR(255),    
'.'occu VARCHAR(255),    
'.'annu VARCHAR(255),    
'.'tax VARCHAR(255),   
'.'phmob VARCHAR(255),    
'.'phmob1 VARCHAR(255),    
'.'phemail VARCHAR(255),    
'.'eduq VARCHAR(255),    
'.'mstat VARCHAR(255),    
'.'pnam VARCHAR(255),    
'.'ptab VARCHAR(255),    
'.'pterm VARCHAR(255),    
'.'psum VARCHAR(255),    
'.'ppre VARCHAR(255),    
'.'pdoc VARCHAR(255),    
'.'pmode VARCHAR(255),    
'.'pmatu VARCHAR(255),    
'.'pnom VARCHAR(255),    
'.'prela VARCHAR(255),    
'.'pnage VARCHAR(255),    
'.'pstat VARCHAR(255),    
'.'pdist VARCHAR(255),    
'.'pcity VARCHAR(255),    
'.'parea VARCHAR(255),    
'.'padd1 VARCHAR(255),    
'.'padd2 VARCHAR(255),    
'.'ppcode VARCHAR(255),    
'.'pndue VARCHAR(255),    
'.'pexp VARCHAR(255),    
'.''.$d0.' VARCHAR(255) not null default 0,   
'.'pay'.$d0.' VARCHAR(255)  not null default 0,   
'.''.$d1.' VARCHAR(255) not null default 0,   
'.'pay'.$d1.' VARCHAR(255)  not null default 0,   
'.''.$d2.' VARCHAR(255) not null default 0,    
'.'pay'.$d2.' VARCHAR(255) not null default 0,   
'.''.$d3.' VARCHAR(255) not null default 0,   
'.'pay'.$d3.' VARCHAR(255) not null default 0,   
'.''.$d4.' VARCHAR(255) not null default 0,   
'.'pay'.$d4.' VARCHAR(255) not null default 0,   
'.''.$d5.' VARCHAR(255) not null default 0,   
'.'pay'.$d5.' VARCHAR(255) not null default 0,   
'.''.$d6.' VARCHAR(255) not null default 0,   
'.'pay'.$d6.' VARCHAR(255) not null default 0,   
'.''.$d7.' VARCHAR(255) not null default 0,   
'.'pay'.$d7.' VARCHAR(255) not null default 0,   
'.''.$d8.' VARCHAR(255) not null default 0,   
'.'pay'.$d8.' VARCHAR(255) not null default 0,   
'.''.$d9.' VARCHAR(255) not null default 0,   
'.'pay'.$d9.' VARCHAR(255) not null default 0,   
'.''.$d10.' VARCHAR(255) not null default 0,   
'.'pay'.$d10.' VARCHAR(255) not null default 0,   
'.''.$d11.' VARCHAR(255) not null default 0,   
'.'pay'.$d11.' VARCHAR(255) not null default 0,   
'.''.$d12.' VARCHAR(255) not null default 0,   
'.'pay'.$d12.' VARCHAR(255) not null default 0,   

 PRIMARY KEY (`uid`),   
  UNIQUE KEY `uid` (`uid`)   
)';
  1. MySQL execution for table
    
$retval = mysql_query( $sql, $conn );   
if(! $retval )   
{   
  die('Could not create table: ' . mysql_error());   
}   
echo $kow;   
echo "Table  created successfully\n";   
mysql_close($conn);    

}
  1. Error given in this code on every month 29th
ljacqu
  • 2,132
  • 1
  • 17
  • 21
  • It'll work correctly one year in every four – Mark Baker Aug 29 '14 at 08:35
  • 1
    What do you actually expect to happen if you add 1 month to 29th January? – Mark Baker Aug 29 '14 at 08:36
  • i am adding next 12 months from current month to create month wise column in mysql to store every month data – team.flashindia Aug 29 '14 at 08:42
  • 1
    Rather than answer the actual question can I suggest that you go on a Database design course. That is a horrible way to hold the data you are trying to store. **REDESIGN YOUR DATABASE** If you have to create a new table to hold a years worth of data. **YOU DID IT WRONG** – RiggsFolly Aug 29 '14 at 08:46
  • 1
    Use the 1st of the month, not the 29th, because not every month has 29 days.... if you'd used the 31st of the month as the base date for this, you'd have problems with February, April, June, September and November – Mark Baker Aug 29 '14 at 08:47
  • 2
    Your code is also screaming out that you should use an array when you have "numbered" variables – Mark Baker Aug 29 '14 at 08:52
  • Why are you trying to do database query when you clearly don't know how to write simple PHP code by itself. Walk before you run. – NDM Aug 29 '14 at 09:17

2 Answers2

0

To simplify generating your list of dates, use an array and a loop

$d = array();
$date = new DateTime('first day of this month');
$interval = new DateInterval('P1M');
for ($i = 1; $i <= 12; $i++) {
    $d[] = $date->format('F_Y'); 
    $date->add($interval);
}
var_dump($d);

And because we're working from the 1st day of the month, you won't have any problems with 29th of February in non leap years, or with 31st April or any other days that don't have 31 days

But you should also normalize your database design so that you don't have months as columns, but a separate months table

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

i found the answer. here is my code

<?php 
function add($date_str, $months)
{
    $date = new DateTime($date_str);
    $start_day = $date->format('j');

    $date->modify("+{$months} month");
    $end_day = $date->format('j');

1. check here start and end date is equal. if not take last day of month

    if ($start_day != $end_day)
        $date->modify('last day of last month');

    return $date;
}


$date=date('Y-m');

$d0 = add($date, 0);  
$d0 = $d0->format('F_Y');
echo $d0;
echo "<br>";

$d1 = add($date, 1);  
$d1 = $d1->format('F_Y');
echo $d1;
echo "<br>";

$d2 = add($date, 2);  
$d2 = $d2->format('F_Y');
echo $d2;
echo "<br>";

$d3 = add($date, 3); 
$d3 = $d3->format('F_Y');
echo $d3;
echo "<br>";
$d4 = add($date, 4); 
$d4 = $d4->format('F_Y');
echo $d4;
echo "<br>";
$d5 = add($date, 5);  
$d5 = $d5->format('F_Y');
echo $d5;
echo "<br>";
$d6 = add($date, 6);  
$d6 = $d6->format('F_Y');
echo $d6;
echo "<br>";


$d7 = add($date, 7);  
$d7 = $d7->format('F_Y');
echo $d7;
echo "<br>";
$d8 = add($date, 8);  
$d8 = $d8->format('F_Y');
echo $d8;
echo "<br>";
$d9 = add($date, 9); 
$d9 = $d9->format('F_Y');
echo $d9;
echo "<br>";
$d10 = add($date, 10);  
$d10 = $d10->format('F_Y');
echo $d10;
echo "<br>";
$d11 = add($date, 11); 
$d11 = $d11->format('F_Y');
echo $d11;
echo "<br>";
$d12 = add($date, 12); 
$d12 = $d12->format('F_Y');
echo $d12;
echo "<br>";
$d13 = add($date, 13); 
$d13 = $d13->format('F_Y');
echo $d13;
echo "<br>";
$d14 = add($date, 14); 
$d14 = $d14->format('F_Y');
echo $d14;
echo "<br>";

thanks for every ones help...

credit goes to Mr. Crend King Profile id [a link] https://stackoverflow.com/users/153133/crend-king

[a link]PHP: Adding months to a date, while not exceeding the last day of the month

Community
  • 1
  • 1