Column 'group_ID' in field list is ambiguous - php

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);

Related

How to insert value into FK column using PHP

Just a heads up, I'm quite new to this but I'd be really thankful if you can drop some knowledge.
I have SQL tables:
Table 'companies' which has the 'company_id' column as primary key.
Table 'users' which has the 'company_id' column as foreign key, referencing the 'companies' table, of course.
Upon signup, two types of users are created.
A manager - who also creates a company
A regular user - who joins the company
I want the regular user to have the company_id of the company he registered to. The user decides to which company he enrolls using a unique company_code.
The code looks like this, so far:
$sql = "SELECT company_id FROM companies WHERE company_code='$company_code';";
$result=mysqli_query($conn, $sql);
$row=mysqli_fetch_assoc($result);
$result=$row["company_id"];
$sql = "INSERT INTO users (firstName, lastName, email, user_uid, user_pwd, role, company_id) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd', '$role', '$result');";
mysqli_query($conn, $sql);
header("Location: ../signup.php?singup=success");
exit();
Everything seems to work just fine, but not the company_id column which is always NULL. Any idea what I'm doing wrong here?
I've already though about assigning the values of the last created user_id and the company_id that matches the company_code to variables and run a query similar to this:
UPDATE `users` SET `company_id` = '$company_id' WHERE `users`.`user_id` = $user_id;
but it really feels like over complicating my existence.
Thanks a lot!
You named your variable $result on line 4.
$result=$row["company_id"];
It should be:
$real_id = $row["company_id"];

PHP MySQL Insert Query From Multiple Tables into two tables

I've table like this :
tb_users
id, name, pin_number
tb_attendance
pin, date_time, user_id
i've created simple query for tb_attendace like this :
$sql = "INSERT INTO tb_attendance
( pin, date_time)
values
('$PIN', '$DateTime')";
i want to insert colum user_id from tb_users where tb_users.pin_number = tb_attendance.pin
in mysql command i've success run this :
INSERT INTO tb_attendance (pin, date_time, entry_by)
SELECT pin, date_time, tb_users.id
FROM tb_attendance , tb_users
WHERE tb_attendance.pin = tb_users.pin_number
but i don't know how to create this query into php script.
can some one help me to complete the php script ?
I'm not sure why you need both the pin and user id, if you can just use JOIN to get the PIN.
The query that you want looks something like this:
INSERT INTO tb_attendance (pin, date_time, entry_by)
SELECT $PIN, $DATE_TIME, u.id
FROM tb_users u
WHERE u.pin_number = $PIN;
I would advise you to use query parameters, and not to insert the parameter values directly into the SQL string. That is dangerous -- both in terms of creating a SQL syntax error and in terms of security.

php mysql query - Document is empty when adding more fields

I have this mysql query which runs fine:
$query = "SELECT
USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO
FROM FRIENDLIST, USER, USER_PROFILE
WHERE FRIENDLIST.FRIEND_ID = ANY (
SELECT FRIEND_ID FROM FRIENDLIST WHERE USER_ID=".$userID."
)
AND APPROVED='YES'
AND USER.USER_ID=FRIENDLIST.FRIEND_ID
AND USER.USER_ID=USER_PROFILE.USER_ID
UNION
SELECT
USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO FROM FRIENDLIST, USER, USER_PROFILE
WHERE FRIENDLIST.FRIEND_ID = " . $userID . "
AND APPROVED = 'YES'
AND USER.USER_ID=FRIENDLIST.USER_ID
AND USER.USER_ID=USER_PROFILE.USER_ID";
When I add a few more fields in the query I get a "Document is empty" error.
$query = "SELECT
USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO, gender, cposition, rinterest, about
FROM FRIENDLIST, USER, USER_PROFILE
WHERE FRIENDLIST.FRIEND_ID = ANY (
SELECT FRIEND_ID FROM FRIENDLIST WHERE USER_ID=" . $userID . "
)
AND APPROVED='YES'
AND USER.USER_ID=FRIENDLIST.FRIEND_ID
AND USER.USER_ID=USER_PROFILE.USER_ID
UNION
SELECT
USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO
FROM FRIENDLIST, USER, USER_PROFILE
WHERE FRIENDLIST.FRIEND_ID = ".$userID."
AND APPROVED='YES'
AND USER.USER_ID=FRIENDLIST.USER_ID
AND USER.USER_ID=USER_PROFILE.USER_ID";
The new fields belong to USER_PROFILE table.
You're using UNION so both SELECT queries must have the exact same number of columns. Otherwise your query will fail and that could give errors like the one you see. This error message probably also indicates that the query result is not checked for errors or failures.
This would be much easier to spot if the code were formatted properly. It is allowed (and advised) to use enters inside a SQL query.

MySQL insert from another table where field(s) are not null

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

Mysql select from a table multiple ids and insert them into another table

I have a mysql table called
jos_users_quizzes with the following columns:
id
quiz_i
duser_id
I have a second table called jos_users with this columns
id
name
username
department
the user_id on first table is linked with the id of second table so quiz_id = id (jos_users)
How can build a query to multiple insert the ids of a selected department into the jos_users_quizzes table... IN ONE CLICK
I am thinking a sub query or a loop will do , but no sure how to contruct the query.
I need to select all user ids from selected department. For example have a list of departments, and once the department is selected , select all ids pertaining that department and insert all the Ids into the other table (quizid , (alldepartment ids)
Thanks in advance!
Code from and ASP.NET form to insert ....
string quizidselected = DropDownList1.SelectedValue;
string deptselected = ListBox2.SelectedValue;
//OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (quiz_id,user_id) VALUES (' " + quizidselected + " ',677)");
OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id) SELECT id, ' " + quizidselected + " ' FROM jos_users WHERE department = ' " + deptselected + " '");
Based on my interpretation of what you want...
INSERT INTO jos_users_quizzes (user_id, quiz_id)
SELECT id, :new_quiz_id
FROM jos_users
WHERE department = :department
If you set the id using auto increment, then you can do something like this
insert into jos_users_quizzes (quiz_i) select id from jos_users;
It is easy if you know keyword like email address, department id or department name.
For example:
$depname = "Logistics"; // PHP // department name
$quizid = "Quiz-12"; // PHP // quiz name
Then make insert query:
<?php
$query = "INSERT INTO `to_table` (user_id, quiz_id)
SELECT id, '$quizid' FROM `from_table`
WHERE department = '$depname'";
?>
For more compatibility you can use Lower case if you obtained values from web page, like:
<?php
$query = "INSERT INTO `to_table` (user_id, quiz_id)
SELECT id, LOWER('%$quizid%')
FROM `from_table`
WHERE department like LOWER('%$depname%')";
?>
Use addslashes command for protecting database when you insert data from web page:
<?php
$query = "INSERT INTO `to_table` (user_id, quiz_id)
SELECT id, LOWER('%".addslashes($quizid)."%')
FROM `from_table`
WHERE department like LOWER('%".addslashes($depname)."%')";
?>

Categories