Create trigger in MySQL or not (via PHP)? - php

Table in MySQL have 1 problem column: creation_date.
During inserting a new row through PHP, I thought that there would be correct to insert the date directly in the query, MySQL has to do it himself.
Do I need to do the trigger, or it would be better to use PHP for this, as intended?
How would you have done to?
PS: MySQL: How to create trigger for setting creation date for new rows

Use TIMESTAMP DEFAULT CURRENT_TIMESTAMP field
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

If you create a trigger for your table (or adopt the suggestion above) you won't have to remember to do it in your PHP. This advantage will show when someone else creates another PHP to insert into the same table.

Related

How to restore the right date if it was inserted lately

If i have ZKTime machine to register the attendance of the employees .
Sometimes this machine insert bulk of transactions in sql server db with wrong later date like
8-2103 instead of 11-2016
What are the possible causes of this problem and how to restore to the right date if i can't detect the problem ?
I've looked at the vendor link you supplied and it does not help in this case. I'm afraid we won't be able to answer this due to items outside of SQL Server. I believe you will need to contact Vendor Support for this.
The questions you will need to find out are:
How does the time machine calculate the CheckTime data?
How does the time machine store the CheckTime data?
How does the machine create the file to export to SQL Server?
This appears to be either an issue with how the system records the CheckTime data or in how it either exports / writes the data to SQL server.
As far as correcting the issue a basic update statement will fix it, but since there are different dates you will need to write a unique update for each case.
One possible solution is to make use of a Trigger to validate the date and update the date accordingly. Assuming the table has the Primary Key as id, if a newly inserted row has a date beyond today, it can be reset to the current datetime since employees' attendance record can't be in future.
CREATE TRIGGER CorrectTheDate on Config
FOR INSERT
AS
DECLARE #CT DateTime
DECLARE #id int
SELECT #CT = i.CheckTime FROM inserted i;
SELECT #id= i.id FROM inserted i;
if(#CT >= DATEADD(dd,1,getdate()))
UPDATE MyTable SET CheckTime=getdate() WHERE id=#id
GO

PHP MyAdmin After Update Trigger not working

I am using PHP MyAdmin Version 4.1.12.
I am trying to create a simple trigger that, after an update, sets 'dateModified' in table 'person' to CURRENT_TIMESTAMP. dateModified is of type TIMESTAMP. The way in which the update occurs to person is the setting of a single attribute in a single record through a X-Editable enabled grid view on a web page. After performing validation against the model with the updated attribute, a new database command is created with the relevant update SQL and executed. So each update only ever modifies a single row within 'person'.
Here is the SQL I wrote to create the trigger:
DELIMITER |
CREATE TRIGGER PERSON_AUPD AFTER UPDATE ON person
FOR EACH ROW BEGIN
SET #dateModified = CURRENT_TIMESTAMP ;
END;
|
DELIMITER ;
After performing updates, I see that the trigger hasn't fired, and the timestamp remains unchanged from the one they were created with (the default for dateModified, and dateCreated, are both CURRENT_TIMESTAMP, so they get set automatically on insert).
I have looked around for answers, and even looked into alternate methods to getting the update (the alternate method was calling a model's afterupdate method and performing separate SQL there on dateModified). I would prefer to exhaust every opportunity to use the triggers, before I go putting more code into my model.
Any help would be greatly appreciated.
Thanks to #juergend, the solution was the following:
Set trigger type to before update because after update cannot update attributes, and are best used to insert new records in related audit tables, etc.
Add NEW. to the front of the attribute you wish to modify.

Is it possible to pull a table's create syntax via mysql?

I'm looking to pull a table's create syntax via mysql and php. Is it possible?
I need it for a file that creates table_x automatically every 10 days. Since I update the site constantly and create new fields I'd like the file to be dynamic and use the previous table (instead of me updating it manually each time).
Yes you can - use
SHOW CREATE TABLE tablename;
Actually if you want to 'reset' your database I would use TRUNCATE instead
TRUNCATE TABLE tablename
SHOW CREATE TABLE table_x;
...will output one record with the CREATE syntax. Is that what you're looking for?

PHP MySQL foreign-key auto insert

So basically what I am trying to do is when a user of my site creates a new account on our register page, I'd like the primary key from the newly created row on the User table (basic info table, email, password, etc.) to be inserted into a new row on the Profile table (more descriptive info, about me, display name, etc.)
I'd like to do this in PHP and any help would be appreciated.
if you are using mysqli look at:
http://www.php.net/manual/en/mysqli.insert-id.php
Get the id after your first insert and then use this in your next insert.
If doing it "in php" isn't really a requirement, then you can use MySQL's built in Trigger mechanism to do this update.
Triggers cause something to happen AFTER or BEFORE an event(INSERT, UPDATE,DELETE)
So your trigger would be:
CREATE TRIGGER thistrigger AFTER INSERT
ON User FOR EACH ROW
UPDATE PROFILE SET "whatever"
On Triggers: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
I think there isn't a really elegant way in MySQL, basically because INSERT doesn't return anything. PostgreSQL does allow for an INSERT ... RETURNING clause, but that's an extension.
That said, if you're using the mysql_* functions in PHP, you can use mysql_insert_id, which might suffice for your needs (i.e. if your primary key is an AUTO INCREMENT integer).
If you are using a mysql database, you could alternatively do another query call from php with the following query:
"SELECT LAST_INSERT_ID();"
More info about it here: http://www.jpgtutorials.com/mysql-last_insert_id-function
It is connection specific. Concurrent inserts from different connections won't affect the current connection.

MySQL Insert date log

I am trying to find out when something was added to a database I maintain but the script that adds the date was working.
Is there a way to retrieve the date of the original INSERT command?
Something like this?
http://dev.mysql.com/doc/refman/5.0/en/query-log.html
make a new table with the key of the table to watch create a after insert trigger that inserts a new line into the watchout table with the id and the time inserted

Categories