Solved

problem in trigger

Posted on 2009-06-27
5
207 Views
Last Modified: 2012-05-07
hi

am using xampp.....working in php and mysql...

i created the following trigger

CREATE TRIGGER dte
before INSERT ON temp_billing
FOR EACH
ROW
BEGIN
SET NEW.t_dt = new.t_updtime;
END


temp_billing table has the following attributes....

test_id        mediumint
product      varchar(40)
t_dt             datetime
t_updtime    timestamp

the trigger query is working fine when i insert manually through phpmyadmin

but when i insert through php 0000-00-00 00:00:00 is getting stored in t_dt.......


my insert query is


$sql="insert into temp_billing(testid,product,t_dt) values ('$id','$prod','0000-00-00')";
$result=mysql_query($sql);


pls help
0
Comment
Question by:whspider
  • 3
5 Comments
 
LVL 14

Expert Comment

by:racek
Comment Utility
CREATE TRIGGER dte
before INSERT ON temp_billing
FOR EACH
ROW
BEGIN
SET NEW.t_dt = NOW();
END
0
 

Author Comment

by:whspider
Comment Utility
will this now() value differ from current time stamp value.....
0
 
LVL 14

Accepted Solution

by:
racek earned 500 total points
Comment Utility
no, because t_updtime  is  timestamp. If you want to be sure, use

CREATE TRIGGER dte
before INSERT ON temp_billing
FOR EACH
ROW
BEGIN
SET NEW.t_dt = NOW(), t_updtime = NOW();

END
0
 
LVL 33

Expert Comment

by:snoyes_jw
Comment Utility
The reason it works in phpMyAdmin and not through your own script is that phpMyAdmin fills in the values for anything you don't edit, so the query it sends is something like this:

INSERT INTO temp_billing(testid, product, t_dt, t_updtime) VALUES ('$id', '$prod', '0000-00-00', CURRENT_TIMESTAMP)

If you did the same, then your original trigger would work. However, using racek's version instead allows you to be lazy and still have it work like you expect.
0
 
LVL 14

Expert Comment

by:racek
Comment Utility
Mz version make logic hidden in the trigger - it can be another advantage :-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now