I am working on a payroll system. The admin is able to successfully save these data but when an employee ID, which is already in the database, is selected, it is not added to the database. Here is my code:
$sql = "INSERT INTO payroll VALUES('$id','$period','$paidDays','$hourlyRate','$hoursworked','$overtime','$overtimePay','$undertime','undertimePay','$sss','$philHealth','$pagibig','$OtherDeductions','$tax','$grossPay','$netPay')";
I need to solve this to be able to do the Salary History of the employees.
PLEASE NOTE that this code is working. It's just that when a similar employee is inputted, it is not added to the database. Thanks!
EDITED: THIS is the table structure
Look at your table structure, Id must be PRIMARY KEY that is why it can't be added when you select an existing id.
As primary key can not be duplicated, so if you want to have that process, the structure have to be changed.
I assume that '....similiar employee...' is defined by ID, and this might give you a hints :
Insert Into Payroll
Select id,period,paidDays,hourlyRate,hoursworked, overtime,overtimePay,
undertime,undertimePay,sss,philHealth,pagibig,OtherDeductions,
tax,grossPay,netPay
FROM (
select '$id' AS ID,'$period' AS period,'$paidDays' AS paidDays,
'$hourlyRate' AS hourlyRate, '$hoursworked' AS hoursworked,
'$overtime' AS overtime,'$overtimePay' AS overtimePay,
'$undertime' AS undertime,'undertimePay' AS undertimePay,
'$sss' AS sss,'$philHealth' AS philHealth,'$pagibig' AS pagibig,
'$OtherDeductions' AS OtherDeductions,'$tax' AS tax,
'$grossPay' AS grossPay,'$netPay' AS netPay, 0 AS JUM
UNION ALL
select id,period,paidDays,hourlyRate,hoursworked, overtime,
overtimePay, undertime,undertimePay,sss,philHealth,pagibig,
OtherDeductions,tax,grossPay,netPay, 1 AS JUM
from [YourTable]
Where [ID] = [EmployeeID]) AS T
GROUP BY id,period,paidDays,hourlyRate,hoursworked, overtime,
overtimePay, undertime,undertimePay,sss,philHealth,pagibig,
OtherDeductions,tax,grossPay,netPay
HAVING SUM(JUM) <0
Related
EDIT: I think I found a way to do it. It is not pretty and I know that I need to use prepared statements. (I'm working my way up to understanding them.)
// look up utilization_id
$sql_utilization_id = "SELECT id FROM system_utilization ORDER BY id
DESC LIMIT 1";
$query_utilization_id = mysqli_query($link, $sql_utilization_id);
$result_utilization_id = mysqli_fetch_assoc($query_utilization_id);
$util_id=implode($result_utilization_id);
$new_util_id=intval($util_id)+1;
$sql = "INSERT INTO system_utilization (date_used, bunker_id, user_id, use_id, hours_used, activity_description) VALUES ('$date', '$bunker_id', '$user_id', '$use_id', '$hours_used', '$activity_description');
INSERT INTO system_meters (image_hours, treatment_hours, power_on_hours, gantry_revolutions, kv_energy_delivered, kv_image_hours, kv_amp_seconds, kv_num_exposures) VALUES ('$image_hours', '$treatment_hours', '$power_on_hours', '$gantry_revolutions', '$kv_energy_delivered', '$kv_image_hours', '$kv_amp_seconds', '$kv_num_exposures');
INSERT INTO hw_modifications (subsystem_id, component_id, manufacturer_id, new_pn, new_sn, new_fw, new_rev_id, old_pn, old_sn, old_fw, old_rev_id) VALUES ('$subsystem_id', '$component_id', '$manufacturer_id', '$new_pn', '$new_sn', '$new_fw', '$new_rev_id', '$old_pn', '$old_sn', '$old_fw', '$old_rev_id');
INSERT INTO failures_and_interrupts (component_id, error_message, actions_taken, utilization_id) VALUES ('$component_id_fail', '$error_message', '$actions_taken','$new_util_id')";
I am very new to SQL and coding in general. I am trying to learn as I go on this task. Please forgive me if I am ignorant of a simple solution.
I have been tasked with creating an equipment log book in a database. I am teaching myself PHP to create the code. I am using MySQL for my database.
I have several tables that may or may not have data inserted into them if they are not needed.
Tables:
system_meters
system_utilization
hw_modifications
sw_modifications
failures_and_interrupts
system_meters and system_utilization will be filled out on every use of the equipment. system_utilization has a column 'id' that will be used as a key 'utilization_id' on all of the other tables. 'id' will auto-increment whenever the table has a new row inserted.
Is there a way to have 'id' populate into 'utilization_id' whenever one of the other tables is updated?
Example:
I change a piece of hardware. I fill out the necessary columns in system_utilization, system_meters, and hw_modifications. How would I have the 'id' from system_utilization inserted into the 'utilization_id' of the other 2 tables?
I need to port some data from tables on a development database into identical tables on the production database, but the production already has records with primary keys that match the dev database so I can't dump the data in with primary keysbundleRenderer.renderToStream
In this case, item_id is the primary key in the parent record, which is used to relate child records to it. Doing the insert of parent records will create a new primary key, so I need child inserts to also have the newly created primary key so that the relationship is maintained on the production databasebundleRenderer.renderToStream
my script so far:
<?php
$DB2connPROD = odbc_connect("schema","user", "pass");
$DB2connDEV = odbc_connect("schema","user", "pass");
//Get itemt records from dev
$getDevitems = "
select item_id,item_typet_id,item_identifier,expiration_timestamp
from development.itemt where item_typet_id in (2,3)
";
//$getDevitems will get records that have a primary key item_id which is used to get the records in the following select queries
foreach($getDevitems as $items){
//Get all comments
$getComments = "
select tc.item_id, tc.comment, tc.comment_type_id from development.item_commentt tc
inner join development.itemt t on tc.item_id = t.item_id
where t.item_id = {item_id_from_getDevitems}
";
$insertitem = "INSERT into production (item_identifier,expiration_timestamp)
values (item_identifier,expiration_timestamp)";
$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)
values (item_id, comment, comment_type_id)";
}
?>
So if $getDevitems returns
item_id | item_typet_id | item_identifier | expiration_timestamp
----------------------------------------------------------------------------
123 1 544 '2020-03-01 12:00:00'
I would want it to now run the comment select with 123 as the ID in the where clause:
select tc.item_id, tc.comment, tc.comment_type_id from development.item_commentt tc
inner join development.itemt t on tc.item_id = t.item_id
where t.item_id = 123
Now for my legacy parent record I have all of the parent data and all of the relational child data. so I want to insert the new parent record into the database, creating the new ID, and inserting the child record with the newly created primary key/ID. So for the new parent record I would do:
$insertitem = "INSERT into production (item_identifier,expiration_timestamp)
values (544,'2020-03-01 12:00:00')";
Let's say that creates the new record with item_id = 43409. I want my comment insert to be:
$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)
values (43409, comment, comment_type_id)";
Bottom LIne: I need to take relational data (all based on item_id) from a development database, and insert these into a new database which creates a new primary key but I need to keep the relationship.
How can I properly finish this to do what I need and make sure I maintain the full relationship for each originally selected item?
Given that your inserts are:
$insertitem = "INSERT into production (item_identifier,expiration_timestamp)
values (item_identifier,expiration_timestamp)";
$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)
values (item_id, comment, comment_type_id)";
It looks like you are using an identity column for item_id. You can retrieve the most recent generated identity value using the IDENTITY_VAL_LOCAL() function so the second insert should be:
$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)
values (IDENTITY_VAL_LOCAL(), comment, comment_type_id)";
I cannot help with PHP but with DB2 for IBMi you have different solutions :
If i understand it correctly item_id is a GENERATED ALWAYS as IDENTITY column
You can get newly created item_id using
select item_id from final table (
INSERT into production (item_identifier,expiration_timestamp)
values (544,'2020-03-01 12:00:00')
)
Or you can force value of item_id with dev value or your own increment
INSERT into production (idtem_id, item_identifier,expiration_timestamp)
values (<your value>, 544,'2020-03-01 12:00:00')
OVERRIDING SYSTEM VALUE
In this case you will have to set next value for item_id by issueing
alter table production alter column item_id restart with <restart value>
I'm doing a program where I’m comparing in a database (Mysql-Workbench) 2 columns (difficulty, difficulty_student) of 2 different tables (EXERCISES, ANSWERS) in a column difficulty_choice of the table ANSWERS.
This is what I mean:
I’m comparing both tables using a VARCHAR (YES or NO). If the user has changed the difficulty of the exercise, the cell will be 'YES', if it has not been changed, the cell will be 'NO'.
These are my tables:
CREATE TABLE exercises (
exercise_id INT,
difficulty VARCHAR(30),
PRIMARY KEY(exercise_id)
);
CREATE TABLE answers(
exercise_id_fk INT,
student_id INT,
difficulty_change VARCHAR(3),
difficulty_student VARCHAR(30),
FOREIGN KEY(exercise_id_fk) REFERENCES exercises(exercise_id)
);
My problem is that the rows of the ANSWERS table don’t exist until the user presses the SUBMIT button in the program. So I have only managed to compare the columns that are in the table using the commands below in Mysql-Workbench.
What I need is to compare the columns in difficulty_change when the user presses SUBMIT. Can you help me do it? I can not get it.
I have managed to compare the columns using the following codes but I want them to be called from the program so that I do not have to go to Mysql-Workbench every time to execute them.
SELECT e.difficulty, a.difficulty_student,
case when e.difficulty = a.difficulty_student then 'NO' else 'YES'
END as difficulty_change
FROM exercises e
INNER JOIN answers a on e.exercise_id=a.exercise_id_fk;
UPDATE answers a
INNER JOIN exercises e on e.exercise_id=a.exercise_id_fk
set a.difficulty_change = case
when e.difficulty = a.difficulty_student then 'NO' else 'YES' END
where e.exercise_id=a.exercise_id_fk;
This is my PHP, it might help:
<?php
if (isset($_POST['submit'])) {
$user_id = $_SESSION['user_id'];
$user_check_query = "SELECT * FROM users WHERE id='$user_id'";
if(isset($_POST['choice'], $_POST['choose'])){
$choice_answer=$_POST['choice'];
$difficulty=$_POST['choose'];
// */$user_id = $_SESSION['user_id'];*/
$query = "INSERT INTO answers (exercise_id_fk, student_id, difficulty_student, choice_answer) VALUES ('$id','$user_id', '$difficulty', '$choice_answer')";
$sql=mysqli_query($conn,$query);
}
}
?>
Try:
$query = "INSERT INTO answers (exercise_id_fk, student_id, difficulty_change, difficulty_student, choice_answer) VALUES ('$id','$user_id', (SELECT IF(difficulty='$difficulty','NO','YES') FROM exercises WHERE exercise_id=$id), '$difficulty', '$choice_answer')";
Use your submit step to only update the db. Add code to query the db again to retrieve the data. Add a line to call your display page again (your display page calls itself) to display your updated table.
I have 4 mySQL tables with the following entries:
user
-user_id PK,AI
-user_name
-user_mobil
-user_passw
-user_email
bookingdetails
-booking_id PK,AI
-booking_date
-booking_time
-person_number
booking
-booking-_id FK
-restaurant_id CK
-user_id CK
restaurant
-restaurant_id PK
-restaurant_name
-restaurant_address
-restaurant_description
I would like to make a booking, I insert all the bookingdetails data, which gives me a AI booking_id, and after I would like to make my booking table and insert the restaurant_id and the user_id With the same booking_id which was given by the bookingdetails table.
I made the following code for achieve that in php on a localserver:
$booking_date=$_POST["booking_date"];
$booking_time=$_POST["booking_time"];
$number_of_place=$_POST["number_of_place"];
$customer_id=$_POST["customer_id"];
$restaurant_id=$_POST["restaurant_id"];
$res;
$sql_query = "INSERT INTO bookingdetails(booking_date, booking_time, number_of_place) VALUES ('$booking_date','$booking_time', '$number_of_place')";
$sql_query2 = "INSERT INTO `booking`(`booking_id`, `customer_id`, `restaurant_id`) SELECT booking_id, '$customer_id', '$restaurant_id' FROM bookingdetails ORDER BY booking_id DESC LIMIT 1 ;";
if(mysqli_query($con,$sql_query))
{
}
else
{
}
if(mysqli_query($con,$sql_query2))
{
}
else
{
}
?>
Is that a legit solution on a server which joining to an Android app? Is there any case, that i don't get the good id on the second query? What would be a better solution?
Answer given in comment by #Mark Ng
Use last insert id, criteria is that your pk has to be AI.
The mysqli_insert_id() function returns the id (generated with AUTO_INCREMENT) used in the last query.
Source: w3schools.com/php/php_mysql_insert_lastid.asp
To elaborate
you have to execute the query from which you need the last inserted id, then you can access that by using
$last_id = $conn->insert_id;
which in turn you can use for your following query.
Note:
I see you use a query to use the results for your insert query, but your syntax is incorrect (your missing values)
Good day!I really need your help guys. Can someone teach me what statements I need to save data into 2 tables. I have 2 tables namely sales and sales_item. The relationship bet. them is the srfno where in sales table it is the PK, so in sales_item table it is the FK. My php form has srfno, date, clientid, clientname, address, contactperson, contactno, returnreason, explanation, refno; these data should be saved in sales table which I already did and it's working brilliantly. My problem now is that qty, serial, desc also include in the form but these should be saved in sales_item table. When I execute the page, it saves the data in sales table and the qty, serial and desc can't save to sales_item table and it didn't get the srfno from sales table.
here's my code for saving qty, serial and desc to sales_item table which is being ignored by my sql statement when saving.
$retitem= "Select `srfno` from sales_item";
$psql= mysql_query($retitem,$con);
$reti = mysql_num_rows($psql);
$reti = $reti + 1;
while($return = mysql_fetch_assoc($psql))
{
if(isset($return['srfno']))
{
$srfno=$return['srfno'];
}
}
$addretex="Insert into `sales_item` (`sitemid`,`srfno`, `retqty`, `retdesc`,`retserial`, `exqty`, `exdesc`,`exserial`,)
VALUES (' ','$srfno', '$qty', '$desc', '$serialno', ' ', ' ', ' ')";
$ret=mysql_query($addretex);
easily you can use LAST_INSERT_ID to insert your last 3 values into table sales_item with related sales id.
INSERT INTO sales (srfno,date,clientid,clientname,address,contactperson,contactno, returnreason,explanation,refno)
VALUES('value1', 'value2','','',...);
INSERT INTO sales_item (sales_id,qty,serial,desc) //sales_id is foreign key came from sales table and its value will be insert automatically by using LAST_INSERT_ID.
VALUES(LAST_INSERT_ID(),'value_1', 'value_2','value_3');
Before Inserting into 'sales_item', you need to check whether $srfno has valid value using
echo "srfno=".$srfno;exit;
if not, you can easily get value of $srfno after inserting into sales table, just do following after inserting into sales table
$srfno=mysql_insert_id();
You can also check error of insert query of sales_item by mysql_error function
mysql_query($addretex) or die("$addretex ".mysql_error());