•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 402,911 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,097 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2529 | Replies: 109
![]() |
•
•
•
•
I guess we go with option B then. Delete data and import your csv. The date column should be like this, STR_TO_DATE('13/09/2007', '%d/%m/%Y') . Where do I use that?
Is it when I am loading the data into the database from the csv file?........
php Syntax (Toggle Plain Text)
if(count($_FILES) > 0) { $ext = ""; $ext = substr(trim($_FILES["file"]["name"]), -4); $allowedext = array(".txt", ".csv", ".sql"); if(in_array($ext, $allowedext)) { $filename = $_FILES['file']['tmp_name']; $handle = fopen($filename, "r"); while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE) { //print_r($data); //echo("<br />"); $value1 = $data[0]; $value2 = $data[1]; $value3 = $data[2]; $value4 = $data[3]; $value5 = $data[4]; $value6 = $data[5]; $value7 = $data[6]; $value8 = $data[7]; $value9 = $data[8]; $value10 = $data[9]; $value11 = $data[10]; $value12 = $data[11]; //echo("<br />"); $query = "INSERT INTO testtsttbills (account,service,billDate,time,timebandtype,destination,callednumber,mins,airtimecharge,tollcharge,amt,privatenumber) VALUES ('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')"; $qry = mysql_query($query,$conn) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno($conn) . ") " . mysql_error($conn)); } fclose($handle); echo "<div align='center'><font color='red'>The file was uploaded.</font></div>"; } else { echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>"; } }
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
~ Confucius, The Confucian Analects
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 601
Reputation:
Rep Power: 2
Solved Threads: 53
You should be able to do it one of two ways:
Number 1: (adjust '%d/%m/%Y' to match up to the format that is on the csv, it is currently looking for "dd/mm/yyyy")
Number 2: (adjust list($dy, $mo, $yr) to match up to the format that is on the csv, it is currently looking for "dd/mm/yyyy")
Number 1: (adjust '%d/%m/%Y' to match up to the format that is on the csv, it is currently looking for "dd/mm/yyyy")
php Syntax (Toggle Plain Text)
if(count($_FILES) > 0) { $ext = ""; $ext = substr(trim($_FILES["file"]["name"]), -4); $allowedext = array(".txt", ".csv", ".sql"); if(in_array($ext, $allowedext)) { $filename = $_FILES['file']['tmp_name']; $handle = fopen($filename, "r"); while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE) { //print_r($data); //echo("<br />"); $value1 = $data[0]; $value2 = $data[1]; $value3 = $data[2]; $value4 = $data[3]; $value5 = $data[4]; $value6 = $data[5]; $value7 = $data[6]; $value8 = $data[7]; $value9 = $data[8]; $value10 = $data[9]; $value11 = $data[10]; $value12 = $data[11]; //echo("<br />"); $query = "INSERT INTO testtsttbills (account,service,billDate,time,timebandtype,destination,callednumber,mins,airtimecharge,tollcharge,amt,privatenumber) VALUES ('$value1','$value2',STR_TO_DATE('$value3', '%d/%m/%Y'),'$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')"; $qry = mysql_query($query,$conn) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno($conn) . ") " . mysql_error($conn)); } fclose($handle); echo "<div align='center'><font color='red'>The file was uploaded.</font></div>"; } else { echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>"; } }
Number 2: (adjust list($dy, $mo, $yr) to match up to the format that is on the csv, it is currently looking for "dd/mm/yyyy")
php Syntax (Toggle Plain Text)
if(count($_FILES) > 0) { $ext = ""; $ext = substr(trim($_FILES["file"]["name"]), -4); $allowedext = array(".txt", ".csv", ".sql"); if(in_array($ext, $allowedext)) { $filename = $_FILES['file']['tmp_name']; $handle = fopen($filename, "r"); while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE) { //print_r($data); //echo("<br />"); $value1 = $data[0]; $value2 = $data[1]; list($dy, $mo, $yr) = explode("/", $data[2]); $value3 = $yr."-".$mo."-".$dy; $value4 = $data[3]; $value5 = $data[4]; $value6 = $data[5]; $value7 = $data[6]; $value8 = $data[7]; $value9 = $data[8]; $value10 = $data[9]; $value11 = $data[10]; $value12 = $data[11]; //echo("<br />"); $query = "INSERT INTO testtsttbills (account,service,billDate,time,timebandtype,destination,callednumber,mins,airtimecharge,tollcharge,amt,privatenumber) VALUES ('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')"; $qry = mysql_query($query,$conn) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno($conn) . ") " . mysql_error($conn)); } fclose($handle); echo "<div align='center'><font color='red'>The file was uploaded.</font></div>"; } else { echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>"; } }
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Hey R0bb0b,
I tried your first method, and it worked perfectly....
the date in the database looks like "2007-11-06"
I tried your first method, and it worked perfectly....
the date in the database looks like "2007-11-06"
Last edited by maydhyam : Jul 24th, 2008 at 3:56 pm.
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
~ Confucius, The Confucian Analects
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 601
Reputation:
Rep Power: 2
Solved Threads: 53
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 601
Reputation:
Rep Power: 2
Solved Threads: 53
Just relieved to see that the data is consistent. You never really know with a varchar since there really is no structure to keep it in the correct format, you could have had a lot of work ahead of you.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
•
•
•
•
Just relieved to see that the data is consistent. You never really know with a varchar since there really is no structure to keep it in the correct format, you could have had a lot of work ahead of you.
Ok, Well now that the data is consistent, the testGCR.php still doesn't work....
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
~ Confucius, The Confucian Analects
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 601
Reputation:
Rep Power: 2
Solved Threads: 53
testGCR.php
gcrFunction.php
php Syntax (Toggle Plain Text)
<?php error_reporting(E_ALL ^ E_NOTICE); include("./gcrFunction.php"); if(count($_POST['btnSubmit']) == 0) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Test Document</title> <script language="javascript" type="text/javascript" src="datetimepicker.js"> //Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com //Script featured on JavaScript Kit (http://www.javascriptkit.com) //For this script, visit http://www.javascriptkit.com </script> </head> <body> <form method="post" target="_blank"> <table> <tr> <td width="747"> <p align="left"><span class="style4 style25 style28 style6">Generate Chargeback Report</span></p> <p align="left"><span class="style21 style29 style5">The Chargeback Reporter works in 2 steps.</span></p> </td> </tr> <tr><td><p> </p></td></tr> <tr> <td> <table width="648" border="0" align="center" cellpadding="1" cellspacing="0" bordercolor="#FFFFFf"> <tr> <td bgcolor="#99bebe" colspan="4"> <p align="left" class="style8 style7 style32"><strong> <span class="style21 style33 style11">Step 1: Select Start and End Date</span></strong></p> </td> </tr> <tr> <td width="100" bgcolor="#99bebe" scope="row"> <div align="left" class="style8 style7 style31 style29 style13"> <div align="right"><strong>Start Date: </strong></div> </div> </td> <td width="220" bgcolor="#99bebe"> <input name="sDate" id="sDate" type="text" size="25"> <a href="javascript:NewCal('sDate','ddmmyyyy')"> <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a Start Date"> </a> </td> <td width="100" bgcolor="#99bebe" scope="row"> <div align="left" class="style8 style7 style31 style29 style13"> <div align="right"><strong>End Date: </strong></div> </div> </td> <td width="220" bgcolor="#99bebe"> <input name="eDate" id="eDate" type="text" size="25"> <a href="javascript:NewCal('eDate','ddmmyyyy')"> <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick an End Date"> </a> </td> </tr> <tr> <td bgcolor="#99bebe" colspan="4"> <p align="left" class="style5"> <span class="style21 style30 style11">Step 2: Generate the Chargeback Report </span></p> </td> </tr> <tr> <td bgcolor="#99bebe"><p> </p></td> <td bgcolor="#99bebe" colspan="3"> <div align="left"> <strong> <input type="submit" value="Generate" name="btnSubmit" /> </strong> </div> </td> </tr> </table> </td> </tr> </table> </form> </body> </html> <? } validateData(); ?>
gcrFunction.php
php Syntax (Toggle Plain Text)
<? include('./connect.php'); function validateData() { global $conn; $sDate=$_POST['sDate']; $eDate=$_POST['eDate']; //echo $sDate; //echo $eDate; $btn=$_POST['btnSubmit']; if(isset($btn)) { if(!$sDate) { echo '<center><font face=Verdana color=Red size="2"><b>Start Date field is empty!</b></font></center></td> </tr><tr bgcolor="#ffffff"><td> <table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf"> <tr><td width="760" bgcolor="#336666" scope="col"> </td></tr></table></td></tr><tr bgcolor="#ffffFF"> <td><p align="center" class="style30 style4" >©ITT Department. </p></td></tr></table>'; die("Problem 1"); } else if(!$eDate) { echo '<center><font face=Verdana color=Red size="2"><b>End Date field is empty!</b></font></center></td> </tr><tr bgcolor="#ffffff"><td> <table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf"> <tr><td width="760" bgcolor="#336666" scope="col"> </td></tr></table></td></tr><tr bgcolor="#ffffFF"> <td><p align="center" class="style30 style4" >©ITT Department. </p></td></tr></table>'; die("Problem 2"); } else { function parseCSVComments($comments) { // First off escape all " and make them "" $comments = str_replace('"', '""', $comments); if(eregi(",", $comments) or eregi("\n", $comments)) { // Check if I have any commas or new lines return '"'.$comments.'"'; // If I have new lines or commas escape them } else { return $comments; // If no new lines or commas just return the value } } // Start our query of the database $sql = mysql_query("SELECT * FROM tsttbills where DATE_FORMAT(date, '%d-%m-%Y') >= '$sDate' and DATE_FORMAT(date, '%d-%m-%Y') <= '$eDate'", $conn); echo $sql; $numberFields = mysql_num_fields($sql); // Find out how many fields we are fetching if($numberFields) { // Check if we need to output anything for($i=0; $i<$numberFields; $i++) { // Create the headers for each column, this is the field name in the database $head[] = mysql_field_name($sql, $i); } $headers = join(',', $head)."\n"; // Make our first row in the CSV while($info = mysql_fetch_object($sql)) { print_r($info); echo "<br />"; } /*while($info = mysql_fetch_object($sql)) { foreach($head as $fieldName) { // Loop through the array of headers as we fetch the data $row[] = parseCSVComments($info->$fieldName); } // End loop $data .= join(',', $row)."\n"; // Create a new row of data and append it to the last row $row = ''; // Clear the contents of the $row variable to start a new row } */ // Start our output of the CSV /*$filename = $file."_".date("Y-m-d_H-i",time()); header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=".$filename.".csv"); header("Pragma: no-cache"); header("Expires: 0");*/ echo $headers.$data; } else { // Nothing needed to be output. Put an error message here or something. echo ("There is no data to create a CSV file."); } } //close the else }//close the if(isset($btn)) statement } ?>
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
~ Confucius, The Confucian Analects
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
age amd avatar backup blue gene breach business c++ chips daniweb data data protection database development dos economy energy enterprise europe government hacker hardware hp ibm ibm. news it linux medicine memory microsoft mmorpg mysql news ogre open source openoffice pc pirate ps3 recession red hat security server sun supercomputer supercomputing technology trends ubuntu x86
- Previous Thread: create a graph using mysql/php
- Next Thread: run php file as services in linux


Linear Mode