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
Related
I want to complete user sign up.
firstly user will write username and password, table called users, I have id as primary key.
secondly user will write address, phone number and zipcode, table called userdata, I have userid as index(key).
I have did a relation between id from table user and userid from table userdata.
So now I want to insert data from php code I did two forms one for user and it has two input for username and password it works well and data inserted.
in the second form I have select option has id from table users and it works well.
then in the same form I have three inputs phone number, address and zipcode.
so the question is how I can insert the data to userdata by the same to the same id. so userid in table user will be the same id in table userdata.
I need sql code.
I used that :
"INSERT INTO userdata(address, phone, zipcode) VALUE (:address, :phone, :zip) SELECT 'id' FROM 'users';"
First :
$q = "NSERT INTO users(username, password) VALUE ('Adam', '12345678')";
$r = $connect->query($q);
//get the last id u inserted
$last_id = mysqli_insert_id($connect);
Second :
$q = "NSERT INTO userdata(address, phone, zipcode,user_id) VALUE ('California', '12345678', '1111','$last_id')";
$r = $connect->query($q);
and if you want to make the (id) not the (userid) the same just :
$q = "NSERT INTO userdata(id, address, phone, zipcode) VALUE ('$last_id','California', '12345678', '1111')";
$r = $connect->query($q);
sealife_customer:
sealife_trip
My query for inserting data from the HTML form (trip_id, email, pay_num, pay_expiry, pay_cvv) and getting data from the sealife_customer and sealife_trip table:
$Query = "insert into sealife_booking values ('$trip_id', '$email', depart_date, first_name, last_name, address, phone, '$pay_num', '$pay_expiry', '$pay_cvv'')
SELECT t1.email, t2.depart_date, t1.first_name, t1.last_name, t1.address, t1.phone
FROM sealife_customer t1
LEFT JOIN sealife_trip t2
WHERE t1.email = '$email'";
So I'm unsure how I combine both the HTML form variables, and the already established data in the customer and trip tables to insert into the new table sealife_booking. The email is the key that links the customer to the booking.
How can I combine both tables and HTML form data to insert in the new table?
Thank you.
My problem is that the value "Arvin", "Tarrega", "Rizal", "Math", "Male" comes from another table which is "student". The value that I have there in the status and date column field comes from a user input. I want to put a statement query which will combine this two into one. Please help me. Btw, the other table doesn't have the status and date field. Only the attendance table has that 2 fields.
table name: attendance
Here is the code I'm using to get that result:
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_POST[status]');
INSERT into attendance(fname, lname, subject, section, gender) SELECT fname, lname, subject, section, gender from student;";
What I would do is to use the fact that you can "select" string in an sql query.
e.g.:
select 'hello' from any_table
In your case, I would do :
$sql = "INSERT into attendance(date, status, fname, lname, subject, section, gender) SELECT '$_POST[set_date]','$_POST[status]',fname, lname, subject, section, gender from student;";
This way you can have all the information you need to insert in one sql query.
I have this error message:
Column 'group_ID' in field list is ambiguous:
Below is my code:
$query = "INSERT INTO members(email, name, gender, dob, profile, password, group_ID)
SELECT group_ID
FROM operations
JOIN members
WHERE operations.group_ID = members.group_ID" ;
$result = $db->query($query);
Trying to insert data into members table, group_ID is from operations but it is a column in the members table.
Your insert won't work, you should learn insert query and join query first, for reference :JOIN and INSERT and then do something like this:
$query = "INSERT INTO members(email, name, gender, dob, profile, password, group_ID)
VALUES(email_value,name_value,gender_value,dob_value,profile_value,password_value,(
SELECT members.group_ID
FROM operations
JOIN members
ON operations.group_ID = members.group_ID) where condition=some_condition)" ;
$result = $db->query($query);
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