I have a result management system for a school. I have three table in database.
Table1 name mark
Table2 name gpa
Table3 name absent
Last 2 year i could insert data in one query but some days i cannot insert data properly using same code i used before. When i execute command sometimes no data insert into mark table, sometimes no data insert into gpa or absent table. what can i do ?
here is my code below.
mysql_query("
insert into `mark`(`stid`,`roll`,`name`,`section`,`exam`,`year`,`ban1`,`ban2`,`eng1`,`eng2`,`mat`,`ssci`,`sci`,`tmark`)
values('$stid','$roll','$name','$section','$exam','$year','$ban1','$ban2','$eng1','$eng2','$mat','$ssci','$sci','$tmark')");
mysql_query("
insert into `stgpa`(`stid`,`exam`,`year`,`ban1gp`,`ban2gp`,`eng1gp`,`eng2gp`,`matgp`,`sscigp`,`scigp`,`tgpa`)
values('$stid','$exam','$year','$ban1grade','$ban2grade','$eng1grade','$eng2grade','$matgrade','$sscigrade','$scigrade','$avggpa')");
mysql_query("
insert into `absent`(`stid`,`exam`,`year`,`wday`,`abs`)
values('$stid','$exam','$year','$wday','$abs')");
echo"<code>Input successfully</code>";
Related
there is my code sample that deletes data from queue_db,I need code which inserts queue_db values to staff_db table.enter image description here
You can use SQL query
Copy all record one table to another
insert into staff_db select * from queue_db
This will transfer the data one table to another, later you can delete records conditionally from first table
If the columns names are different then
Copy all record one table to another with column name alias
insert into staff_tb(column1, column2, column3) select c as column1, b as column2, d as column3 from queue_db
where c,b,d are the columns in the second table
If you want to filter it with condition then add where at the end of the query with <tablename.columnname> = <value>
Copy all record one table to another without duplicates
To prevent duplicates, you can use this answer Prevent Duplicate I don't want repeat the same answer here
If you want copy those data into fresh new table like doing a backup then
Copy all record one table to a fresh new table
select * into newtable from queue_db
Then you run delete query for table which you copied from
Delete all records from a table
delete from queue_db
hy i am using Mysql (phpmyadmin) with php and i have two tables table1(id,name) and table2(id,date,name)
now i want to copy all rows of table1 to table2 and in table2 in date column should store current date of system, for this am trying following query which is giving error
'INSERT INTO table2 (id, Date, name)SELECT id,'.date("Y-m-d",mktime()).'nae FROM table1 WHERE id=2'
can you correct my query ? i tried different ways but not working any method. there is no error in connection with database all are ok, i tested connection its fine
How about this:
'INSERT INTO table2 (id, Date, name) SELECT id,"'.date("Y-m-d",mktime()).'",name FROM table1 WHERE id=2'
I am newbie to sql advance and i am trying to insert rows to one table by selecting from another one. But some bug is coming out.
Here is my problem
So i have two tables
table1 having id(autoincrement), names
table2 having id(autoincrement), names
Now at starting table1 is empty and table2 having 2 rows
1,'myself'
2,'yourself'
So the problem starts here
When i execute following query
Insert into table1 (names) select (names) from table2
So now both rows of table2 must be copied to table1
Ya its working fine.
But what about autoincrement value of id?
By till now table1 autoincrement id should store 3 since next row to be inserted should have id 3
But its not working like expected so table1 autoincrement id stores 4 i.e, 1(current id value)+2*(no of rows inserted)-1
So next time when i execute same query it inserts row with id 4. Skips id=3.
This is problem hope you all got what i am talking about.
Thanks for helping in advance.
Try this
INSERT INTO TABLE1 SELECT * FROM TABLE2
Go ahead an copy your data over, and then you can modify the starting auto_inc to whatever you choose.
ALTER TABLE table AUTO_INCREMENT = 1000;
Alter Table will take around 1 sec as it first creates shadow of the table then the query is processed and if the no. of rows of table goes above then it may take around 20-30 sec. so I dont think that alter is good answer for this problem. Hope someone will help you with insert command such as:
Insert into table1 set id = 1; etc...
i'm trying to insert some rows from one table to another and add last inserted id to first table:
INSERT INTO tableA (fooA, fooA2) SELECT fooB, fooB2 FROM tableB;
And now i want to add last inserted id into tableB for each row
UPDATE tableB set tableA_id = LAST_INSERT_ID();
But for multiple records it's wrong. Any idea how to update tableB after each insertion into tableA? Is it possible to do with MySql query, or just write some PHP script?
Try to get it done with trigger .
I want to insert data partially from select statement..
For example..
I have one table named movie..here's the fields :
-id_movie
-tittle
-studio
-time
And another table named reservation.. here's the fields :
-id_reservation
-tittle
-studio
-time
-seat_1
-seat_2
So, I want to insert value of tittle, studio, and time into reservation table that getting from select statement from movie table. But I'm a little bit confuse about another field in reservation table (id_reservation, seat_1, seat_2).. They're actually don't exist in the first table (movie table)
so how to write the query?
any simple example would be very helpful
thank you
$query = "INSERT INTO reservation(tittle,studio,time)
SELECT tittle,studio,time FROM movie";
INSERT INTO reservation (
tittle,
studio,
time
)
SELECT tittle,
studio,
time
FROM movie;
But you better not have NOT NULL on the columns you leaving without values.
If you want mysql query then:
"INSERT INTO reservation(tittle,studio,time)
SELECT tittle,studio,time from movie";
First do a SELECT from "movie" to obtain the fields you need from that table.
You will have the fields you need stored in 3 variables, say $title, $studio, $time.
As per the table reservation, the field id_reservation should be the PK so set it to autoincrement. You don't have to insert that value as it does it automatically in every insert.
The fields seat_1 and seat_2 will depend on other facts than the data you get from the first table. You should obtain the free seats (I guess there is another table with the total amount of seats), and select 2 using some kind of logic (for example, that they are next to each other)
So in the end, the INSERT query for the table reservation should look like
"INSERT INTO reservation (title, studio, time, seat_1, seat_2) VALUES('$title', '$studio', '$time', $seat_1, $seat_2)");