Hi I have some code shown below, which inserts date_time and date in to the mysql fields using the now() command.
However the date_time field updates but the date field doesn't and I can not seem to work out why?
my mysql fields are datetime and date
INSERT INTO vistordetails1
(ipaddress, client_id, type, date_time, company_name, location, search_term, trafic_source, no_of_pages, date, country_code) VALUES('$ip_address', '$client_id', '$type', now(),'$fields[11]','$fields[6]', '$keyword', '$referer','1', now(), '$country_code')
Table Structure
`id` int(11) NOT NULL AUTO_INCREMENT,
`client_id` int(11) NOT NULL,
`master_id` int(11) NOT NULL,
`type` tinyint(1) NOT NULL ,
`date_time` datetime NOT NULL,
`company_name` varchar(250) NOT NULL,
`location` varchar(250) NOT NULL,
`no_of_pages` int(11) NOT NULL,
`trafic_source` varchar(250) NOT NULL,
`search_term` varchar(250) NOT NULL,
`is_repeater` tinyint(1) NOT NULL,
`classification` int(11) NOT NULL,
`owner` int(11) NOT NULL,
`alert_for_repeat_visit` tinyint(1) NOT NULL DEFAULT '0',
`is_hot_list` enum('0','1') NOT NULL DEFAULT '0',
`ipaddress` varchar(50) NOT NULL,
`country_code` varchar(10) NOT NULL,
`date` date NOT NULL,
Try using CURDATE() instead of NOW() for the date field.
$INSERT = "INSERT INTO vistordetails1
(ipaddress,
client_id,
type,
date_time,
company_name,
location,
search_term,
trafic_source,
no_of_pages,
date,
country_code)
VALUES(
'".mysql_real_escape_string(trim($ip_address))."',
'".mysql_real_escape_string(trim($client_id))."',
'".mysql_real_escape_string(trim($type))."',
now(),
'".mysql_real_escape_string(trim($fields[11]))."',
'".mysql_real_escape_string(trim($fields[6]))."',
'".mysql_real_escape_string(trim($keyword))."',
'".mysql_real_escape_string(trim($referer))."',
'".mysql_real_escape_string(trim(1))."',
CURDATE(),
'".mysql_real_escape_string(trim($country_code'))."')";
Also as a previous user mentioned you seriously need to make sure your data is escaped, or use mysql prepare.
If you need to convert a PHP date into a valid MySql date, then the only thing you need to do is
ensure that it is formatted correctly.
The default date/datetime format for Mysql is: yyyy-mm-dd and yyy-mm-dd hh:mm:ss
Something like this will work.
$phpDate = date('Y-m-d H:i:s', time());
However
Because you are creating a new date, why use PHP at all? Within MySql you can include the current date using NOW() function.
$sql = "INSERT INTO xyz (a, b, c, datetime) VALUES ('a', 'b', 'c', NOW());"
Additionally
You could apply a default value of CURRENT_TIMESTAMP to the table column. Meaning you wouldn't even need to pass any date
as the database will take care of it.
i.e ALTER TABLE test CHANGE datecolumn datecolumn DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
Related
When I insert a timestamp into my database the value is about 10 hours back from my local time. The database is hosted on Godaddy's server.
I have changed my php5.ini file and added date.timezone = 'Europe/Istanbul'
(it was america/phoenix as default) when I checked with phpinfo();
Then I ran this code:
if (date_default_timezone_get()) {
echo 'date_default_timezone: '. date_default_timezone_get() .'<br />';
}
if (ini_get('date.timezone')) {
echo 'date.timezone: ' . ini_get('date.timezone');
echo date("d/m/y : H:i:s", time());
}
It's OK also same as my local time
But when i insert data it's get America/Phoenix timestamp which is -10 hours from my local time
Here is my insert code
$insertSQL = sprintf("INSERT INTO cepbank (username, trans_id, bank, tutar, operator) VALUES ('$username', '$trans_id', '$bank', '$tutar', '$operator')");
and here is my MySql Table
CREATE TABLE `cepbank` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(11) collate utf8_turkish_ci NOT NULL,
`trans_id` int(11) NOT NULL,
`bank` varchar(11) collate utf8_turkish_ci NOT NULL,
`tutar` decimal(10,2) NOT NULL,
`operator` varchar(32) collate utf8_turkish_ci NOT NULL,
`tarih` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci COMMENT='CepBank Gelen' AUTO_INCREMENT=143 ;
It happens because you use default CURRENT_TIMESTAMP for tarih field. And it uses the unadjusted server date. Get the adjusted timestamp in php:
$timestamp = date('Y-m-d H:i:s', time ());
then add it into the query:
INSERT INTO cepbank
(tarih, username, trans_id, bank, tutar, operator)
VALUES
('$timestamp','$username', '$trans_id', '$bank', '$tutar', '$operator'
I have a mysql query runnning in php .
$result = mysql_query("SELECT status,initiated_by,create_timestamp,extension_id,
product_code,prospect_name,prospect_email1,prospect_phone1,prospect_title,
prospect_company,prospect_message,track_id
FROM prospect_requests
WHERE subscriber_id = '$subscriberid'
AND create_timestamp >='$start_date'
AND create_timestamp <= '$end_date'");
There is two attributes names $start_date and $end_date
My question is why above query is not returning the information of $end_date
Here is scenario .
I have selected $start_date as 01-02-2013 and $end_date as 09-2-2013 .
But the above query is not returning the details of 9 Feburary .
Please help me out .
Update
When i am placing date as 10 feb it is returning me the details of 9 feb
Table structure
CREATE TABLE `prospect_requests` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`customer_id` varchar(255) NOT NULL,
`subscriber_id` varchar(255) NOT NULL,
`extension_id` varchar(255) DEFAULT NULL,
`vendor_number` varchar(255) DEFAULT NULL,
`product_code` varchar(255) DEFAULT NULL,
`prospect_email1` varchar(255) DEFAULT NULL,
`prospect_phone1` varchar(255) DEFAULT NULL,
`prospect_email2` varchar(255) DEFAULT NULL,
`prospect_phone2` varchar(255) DEFAULT NULL,
`prospect_title` varchar(255) DEFAULT NULL,
`prospect_company` varchar(255) DEFAULT NULL,
`prospect_name` varchar(255) DEFAULT NULL,
`prospect_message` text,
`create_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_timestamp` datetime DEFAULT NULL,
`status` int(11) DEFAULT '0',
`track_id` varchar(255) NOT NULL,
`initiated_by` varchar(255) DEFAULT NULL,
`subscriber_email` varchar(255) DEFAULT NULL,
`vendor_email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=142 DEFAULT CHARSET=latin1;
Input : $start_time = 2013-02-01
$end_time = 2013-02-09
The condition <= '$end_date' is basically the same as <= '$end_date 00:00:00'.
If you need the day itself, you could query like this:
... create_timestamp <= '$end_date 23:59:59'
create_timestamp BETWEEN '$start_date' AND '$end_date 23:59:59' would work too.
Alternative
Add one day programmatically and use <:
$end_date = date('Y-m-d', strtotime("$end_date +1 day"));
... create_timestamp < '$end_date'
Update
You can also cast the timestamp to a date first:
DATE(create_timestamp) BETWEEN '$start_date' AND '$end_date'
Dates should be in the format YYYY-MM-DD.
Also, you can use WHERE create_timestamp BETWEEN '...' AND '...' as a more efficient condition for bounding a value.
for compare dates you should your BETWEEN Operator in mysql Query like Following
$result = mysql_query("SELECT
status,initiated_by,create_timestamp,extension_id,product_code,prospect_name,
prospect_email1,prospect_phone1,prospect_title,prospect_company,prospect_message,
track_id from prospect_requests WHERE subscriber_id = '$subscriberid' AND
create_timestamp BETWEEN '$start_date' AND '$end_date'");
Please full description of between operator at w2school
if still not working it then please update your question with table structure
i see your update question you are using timestamp field type that cause problem please see following link that might be helpfull Comparing timestamp with date variable (MySQL and PHP)
I'm trying to put a bit of dummy data directly into a table I have created:
$sql = "CREATE TABLE customers
(
customer_id int(11) NOT NULL AUTO_INCREMENT,
customer_joindate DATE NOT NULL,
customer_title varchar(8) NOT NULL,
customer_firstname varchar(20) NOT NULL,
customer_surname varchar(20) NOT NULL,
customer_dob DATE,
customer_email varchar(60),
customer_phone varchar(14),
customer_lastupdate TIMESTAMP,
PRIMARY KEY (customer_id)
)TYPE = INNODB;";
I would like the join date to be the current date and I believe the timestamp will handle itself? So I tried the following insert:
mysql_query("INSERT INTO customers (customer_joindate, customer_title, customer_firstname, customer_surname, customer_dob, customer_email, customer_phone)
VALUES (CURDATE(), 'Mr', 'John', 'Smith', '1985-11-01', 'john.smith#gmail.com', ''+4477665434')");
That doesn't seem to be inserting anything, could someone please tell me where I have gone wrong?
Try using 'ENGINE' instead of 'TYPE' :
CREATE TABLE 'customers'
(
'customer_id' int(11) NOT NULL AUTO_INCREMENT,
'customer_joindate' DATE NOT NULL,
'customer_title' varchar(8) NOT NULL,
'customer_firstname' varchar(20) NOT NULL,
'customer_surname' varchar(20) NOT NULL,
'customer_dob' DATE,
'customer_email' varchar(60),
'customer_phone' varchar(14),
'customer_lastupdate' TIMESTAMP,
PRIMARY KEY ('customer_id')
) ENGINE=INNODB;
For more information check the MySql Manual.
As far as I know 'TYPE' is deprecated for newer versions of MySql.
as suggested I used die(mysql_error()) to find the problems:
mysql_query("INSERT INTO customers (customer_joindate, customer_title, customer_firstname, customer_surname, customer_dob, customer_email, customer_phone)
VALUES (CURDATE(), 'Mr', 'John', 'Smith', '1985-11-01', 'john.smith#gmail.com', '+447766543498')")or die(mysql_error()); ;
I'm working on a codeigniter project and I'm trying to troubleshoot a sql issue. I have a query that updates a date field in my table and it's not updating it at all.
I have this table
CREATE TABLE `Customer` (
`customer_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(55) NOT NULL DEFAULT '',
`last_name` varchar(55) NOT NULL DEFAULT '',
`city` varchar(255) DEFAULT '',
`state` char(2) DEFAULT '',
`zip` int(5) DEFAULT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`image` varchar(255) DEFAULT '',
`blurb` blob,
`end_date` date DEFAULT NULL,
`goal` int(11) DEFAULT NULL,
`paypal_acct_num` int(11) DEFAULT NULL,
`progress_bar` enum('full','half','none') DEFAULT NULL,
`page_views` int(11) NOT NULL DEFAULT '0',
`total_pages` int(11) NOT NULL DEFAULT '0',
`total_conversions` int(11) NOT NULL DEFAULT '0',
`total_given` int(11) NOT NULL DEFAULT '0',
`conversion_percentage` int(11) NOT NULL DEFAULT '0',
`avg_contribution` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
and when I run this query to insert data, it runs fine and sets the date to 2012-11-01
INSERT INTO `Customer` (`first_name`, `last_name`, `end_date`) VALUES ('John', 'Smith2', '2012-11-01');
Then I get the customer_id and try to run this query
UPDATE `Customer` SET `end_date` = '2012-14-01' WHERE `customer_id` = '18';
and it sets the date end_date field to 0000-00-00.
Why is it changing the end date to 0000-00-00 rather than 2012-14-01?
2012-14-01 is first day of fourteenth month :)
(so its invalid date, thus casted to 0000-00-00 and Data truncated for column 'end_date' at row 1 warning was returned by mysql, which you can see by querying SHOW WARNINGS to mysql immediately after badly behaving query)
2012-01-14 is 14th of January.
use this:
UPDATE `Customer` SET `end_date` = date('Y-m-d') WHERE `customer_id` = '18';
Use date function to update this field.
I'm integrating an RSS feed unto my website and i need to create a table:
CREATE TABLE `stories` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`description` text NOT NULL,
`when` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)
i know how to insert id, title, description but i do not know how to insert the time of the post (user submits them). I used the script given here.
mysql_query("insert into stories (title,description) VALUES ('$_POST[title]','$_POST[description]') ");
For when use the date function:
date('Y-m-d h:i:s')
By default the date function uses the current date/time if you don't supply one as the second argument.