Okay so I don't think it's possible but i'm asking to be sure. Let's say I have a table called wp_users and forum_users. In wp_users there's a username, email, and md5 hash column. Same thing goes with forum_users. Is there a way to syncronize the two so that way something like
INSERT INTO wp_users (username, password, email)
VALUES ('$_POST[username]', '$_POST[password]', '$_POST[email]');
would automatically do
INSERT INTO forum_users (username, password, email)
VALUES ('$_POST[username]', '$_POST[password]', '$_POST[email]');
Same with ALTER etc, etc.
Use transactions
BEGIN
INSERT INTO wp_users (username, password, email)
VALUES ('$_POST[username]', '$_POST[password]', '$_POST[email]');
INSERT INTO forum_users (username, password, email)
VALUES ('$_POST[username]', '$_POST[password]', '$_POST[email]');
COMMIT;
You can utilize MySQL TRIGGERS to automate this.
CREATE TRIGGER after_insert_wp_users
AFTER INSERT ON wp_users FOR EACH ROW
BEGIN
INSERT INTO forum_users (username, password, email)
VALUES (NEW.username, NEW.password, NEW.email);
END
You can do the same for UPDATE and other queries.
EDITED 1:
In case, both tables are required to be updated automatically, i.e. if a row is inserted in the table wp_users, it is automatically inserted in forum_users and vice versa, writing the second set of triggers to update wp_users table from forum_users will introduce recursive loop.
To circumvent this, we can use a status variable to check if secondary insert should happen or not.
Trigger 1:
CREATE TRIGGER after_insert_wp_users
AFTER INSERT ON wp_users FOR EACH ROW
BEGIN
IF #__disable_trigger_users = 1 THEN
SET #__disable_trigger_users = NULL;
ELSE
SET #__disable_trigger_users = 1;
INSERT INTO forum_users (username, password, email)
VALUES (NEW.username, NEW.password, NEW.email);
END IF;
END
Trigger 2:
CREATE TRIGGER after_insert_forum_users
AFTER INSERT ON forum_users FOR EACH ROW
BEGIN
IF #__disable_trigger_users = 1 THEN
SET #__disable_trigger_users = NULL;
ELSE
SET #__disable_trigger_users = 1;
INSERT INTO wp_users (username, password, email)
VALUES (NEW.username, NEW.password, NEW.email);
END IF;
END
EDITED 2
You can also achieve the same using MySQL Store Procedure.
DROP PROCEDURE IF EXISTS proc_insert_user
GO
CREATE PROCEDURE proc_insert_user
(
IN p_username VARCHAR(15),
IN p_password VARCHAR(15),
IN p_email VARCHAR(15)
)
BEGIN
INSERT INTO wp_users (username, password, email)
VALUES (p_username, p_password, p_email);
INSERT INTO forum_users (username, password, email)
VALUES (p_username, p_password, p_email);
END
GO
CALL proc_insert_user('$_POST[username]', '$_POST[password]', '$_POST[email]')
GO
Related
I try to create normalized db.
When I insert values FIRST time, everything is working perfectly, but (I think cause of set values as UNIQUE) it do not want to add.
This is my queries ( I can show database as well if needed )
$query = "INSERT INTO userMore (userEmailG, userGenderG, userAboutG, userBirthdayG, userLanguageG) values ('$userEmailG','$userGenderG', '$userAboutG', '$userBirthdayG', '$userLanguageG');";
$query .= "INSERT INTO userBasic (id_uM, userNameG, userEmailG) values ((SELECT id_uM FROM userMore WHERE userEmailG='$userEmailG'),'$userNameG', '$userEmailG');";
$query .= "INSERT INTO dvlaInfoMore (sixMonthRate, twelveMonthRate, wheelPlan, revenueWeight, typeApproval, taxStatus, taxed, taxDetails, mot, motDetails) values ('$sixMonthRate', '$twelveMonthRate', '$wheelPlan', '$revenueWeight', '$typeApproval', '$taxStatus', '$taxed', '$taxDetails', '$mot', '$motDetails');";
$query .= "INSERT INTO dvlaInfoBasic (id_uDInfoM, plateNumber, make, model, yearOfManufacture, cylinderCapacity, dateofFirstRegistration, co2Emissions, fuelType, colour, vin, transmission)
values (LAST_INSERT_ID(), '$plateNumber', '$make', '$model', '$yearOfManufacture', '$cylinderCapacity', '$dateofFirstRegistration', '$co2Emissions', '$fuelType', '$colour', '$vin', '$transmission');";
$query .= "INSERT INTO userLocation (latitude, longitude, postCode) values ('$latitude', '$longitude', '$postCode');";
$query .= "INSERT INTO userChioce (doWithCar) values ('$doWithCar');";
$query .= "INSERT INTO userStatus (userIdG) values ('$userIdG');";
$query .= "INSERT INTO userMain (userIdG, id_uB, id_uDInfoB, id_uChoice, id_uLoc, id_uStat) values ('$userIdG', (SELECT id_uB FROM userBasic WHERE userEmailG='$userEmailG'), (SELECT id_uDInfoB FROM dvlaInfoBasic WHERE plateNumber ='$plateNumber'), LAST_INSERT_ID(), (SELECT id_uLoc FROM userLocation WHERE postCode='$postCode'),(SELECT id_uStat FROM userStatus WHERE userIdG='$userIdG'))";
So, first time userMore added then userBasic then dvlaInfoMore then dvlaInfoBasic and so one
I set in userBasic column userNameG as UNIQUE, so IF inserted email which already exist, it do not want to go further, BUT if not, everything added correctly.
ALL what I want that it will continue inserting different cars and then by using existing email display result in table userMain.
P.S. if I try to add different car from by using same email address it doesn't work., basically do not add anything.
As you suspected yourself, INSERT with an existing key defined as UNIQUE will fail.
One workaround is to use the REPLACE INTO statement instead of INSERT (see manual).
I have a table which contains pageid, department, position and active. While updating, I have to update active column if pageid is already exists, if not I will insert a new row with the pageid.
I am getting pageid values through checkbox and my update query is like this
$sql = "INSERT INTO access_level (page_id, department, position, active)
VALUES (".$sn.", ".$department.", ".$position.", 1)
ON DUPLICATE KEY UPDATE
department=VALUES(".$department."),
position=VALUES(".$position."),
active=VALUES(1)";
But everything is getting inserted twice in this case.
Where I am doing wrong? Can somebody guide me?
This should probably work for you:
$sql = "INSERT INTO access_level (page_id, department, position, active)
VALUES (".$sn.", ".$department.", ".$position.", 1)
ON DUPLICATE KEY UPDATE
id=LAST_INSERT_ID(id),
active=VALUES(1)";
The query I have so far is :
create table users_csv_import
(
username varchar(255),
reg_link varchar(255)
);
insert into users_csv_import (username, reg_link)
values ('kns2184', 'NULL');
insert into users_csv_import (username, reg_link)
values ('kns2185', 'NULL');
insert into users_csv_import (username, reg_link)
values ('kns2186', 'NULL');
insert into users_csv_import (username, reg_link)
values ('kns2187', 'NULL');
insert into users_csv_import (username, reg_link)
values ('kns2188', 'NULL');
update users_csv_import
set reg_link = md5(username in row should go here?)
where reg_link='NULL';
So what I would like to do is for every row generate md5 hash from username in that row. I do not want to use a php loop, I would like to do this through mysql because I assume it will be much faster then something like an array of generated hashes via php.
http://sqlfiddle.com/#!2/da389/1
Just do what you say in your post (almost):
update users_csv_import
set reg_link = md5(username)
where reg_link IS NULL;
Hi I working on simple newsletter script, I got two groups there with two different tables for each group.
I want to move subscribers from one group to another, I put this query and It works:
$sql = "INSERT INTO newsletter_subscribers2 SELECT * FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
However after moving some previous subscribers I got error with duplicated entry key, as I supose query is moved with ID, so I want just move, name, lastname and email.
$sql = "INSERT INTO newsletter_subscribers2 VALUES (firstname, lastname, email) SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname, newsletter_subscribers.email FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
However this part of code doesnt seemst to work as I got syntax error, can anyone help me with this I will be very happy.
Thanks for reply
You have to remove values
$sql = "INSERT INTO newsletter_subscribers2(firstname, lastname, email)
SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname,
newsletter_subscribers.email
FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
Remove the VALUES
$sql = "INSERT INTO newsletter_subscribers2 (firstname, lastname, email)
SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname,
newsletter_subscribers.email
FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
Edit: And always use mysql_real_escape_string to prevent SQL-injection:
$sql = "... WHERE id='".mysql_real_escape_string($_GET['nid'])."'";
Try this:
insert into newsletter_subscribers2 (col1, col2, col3, etc) select col1, col2, etc from newsletter_subscribers2 where 1 = 1
I am trying to insert data into a table (table1) based on another (table2), the only problem is that table1 contains fields set to not allow null values. Do I need to create these fields in table2 where I am pulling the data from and populate them with a value?
Example not null field: password
If I do not include this in my query then I get an error and the record is not inserted however if I create the field in table2 then insert into my query it works fine. This seems a bit out of the ordinary. Example query below:
Example 1 (no password field in table 2):
$insert_new_records_query = "INSERT INTO table1 (first_name, last_name, email_address) ".
"SELECT firstname, lastname, email FROM table2";
This generates an error saying that I must include the password field.
Example 2 (password field in table 2):
$insert_new_records_query = "INSERT INTO table1 (first_name, last_name, password,
email_address) ".
"SELECT firstname, lastname, password = 'password1', email FROM table2";
This allows the record to be created. The problem is that I have many more fields that are not null in table 1 and I don't think I need to create them in table 2 as blank fields and insert them into my query with values just to create a record. Is there a better way to do this?
You don't have to create fields, you can simply 'select' default values. Try this instead:
INSERT INTO table1 (first_name, last_name, password, email_address)
SELECT firstname, lastname, 'password1', email FROM table2