selecting data from table one table and insert into another table - php

I have two tables; result and total.
result(student_code,mark)
total(student_code,total)
The total column should contain total marks of a student.
So I have written an SQL statement to insert data from result table into total table by using a SELECT statement which should take a specific student_code and his/her marks as a summation and then store it in the total table.
But, whenever I try, the query either fails or just takes only one student
When I use this query, it works but for only one student, whose student_code is 1, but I need to take all the students and their marks as total
<?php
// copy student student_code and summation of his/her mark from result table into total table
$query = 'INSERT INTO total
(student_code, total)
SELECT student_code, SUM(mark)
FROM
result
WHERE
student_code = 1';
$sql = mysql_query($query) or (mysql_error());
?>
And when I use a student code as a variable I get nothing
<?php
// copy student student_code and summation of his/her mark from result table into total table
$query = 'INSERT INTO total
(student_code, total)
SELECT student_code, SUM(mark)
FROM
result
WHERE
student_code = "' . $student_code . '"';
$sql = mysql_query($query) or (mysql_error());
?>

You must perform two different queries first query selects the data and then store it to a php variable and then later insert it into another table using the second query
UPDATE----
Try this code
<?php
for($i = 1; $i < 102; $i++)
{
$query = 'INSERT INTO total (student_code, total) SELECT student_code, SUM(mark) FROM result WHERE student_code = "$i"';
$sql = mysql_query($query) or (mysql_error());
}
?>
It must work

Related

pull data from two tables with one query/ select statement

I am pulling one column from the USERS table but now I wanna pull columns from the MONEY table. How do I accomplish this?
sample database
USERS TABLE
userID = 33
nestEgg = 600000
MONEY TABLE
userID = 33
monthlyContributions = 500, 250, 300
totalContributions =
<?php
include 'inc/connect.php';
$query = "SELECT * FROM USERS WHERE userID = '$userID'";
$result = mysqli_query($link,$query);
$row = mysqli_fetch_assoc($result);
?>
If I'm not mistaken, you're asking to create a column in MONEY which is the sum of nestEgg and monthlyContributions. I'm a total newbie, but I think getting the sum, followed by the insert, should look like:
SELECT (SELECT SUM(nestEgg) FROM USERS) + (SELECT SUM(monthlyContributions) FROM MONEY)
INSERT INTO MONEY(totalContributions)
If you only want the sum from the monthly contributions, then it should just be:
SELECT SUM(monthlyContributions) FROM MONEY
INSERT INTO MONEY(totalContributions)
If I'm not mistaken.

How to get total inserted row count from a table after bulk insert query mysql

I am using a bulk insert query using php to insert into a mysql table. Here i want to know the total inserted records from this bulk insert query. After completion of the bulk insert i am using below code to get total inserted count.
//here bulk insert complete.Next i am calling this
$row_cnt = mysql_query("SELECT ROW_COUNT()");
echo "Affected:rows:".$inbound_cnt = mysql_num_rows($row_cnt);
Here i am getting Affected:rows:1.And i want last insert id also from this bulk insert.
echo "current_id:". $current_id = mysql_insert_id();
I am getting '0' ;
How can i find total inserted count? Any help would be greatly appreciated.
// my insert query
$data4 = "";
$inbound_csv_cnt = count($csv_array);
for($i=0;$i<$inbound_csv_cnt;$i++)
{
$data4.="('".$csv_array[$i][0]."','".$csv_array[$i][1]."','".$csv_array[$i][2]."'),";
}
$data4 = substr($data4,0,-1);
$ins_sql = "INSERT INTO whv_inbound_response(`whv_id`, `rqv_id`, `whv_type`) values $data4" ;
$res = mysql_query($ins_sql);
You can get total records before insert with this
SELECT COUNT(id) FROM table_name; // save output in $old
Then Insert your data and again count total records now as $new
Now find the difference
$total_inserted_record = $new-$old;
While using loop, you can also set a counter to count the number of rows, even before insert. Also, to get last insert ID, for mysqli procedural you may use
$last_id = mysqli_insert_id($conn_string);
You already have number of records being insert in $inbound_csv_cnt

Add MySQL Count Column to PHP/HTML Table

I'm developing a website using HTML, PHP and MySQL to access a database. On one page I present a table with data from that database. This is some of the code I'm using:
$sql1 = "SELECT * FROM MyTable ORDER BY ID ASC";
$rs1 = mysqli_query($link,$sql1);
(...)
while($row1 = mysqli_fetch_assoc($rs1)) {
echo "<tr><td>".$row1['ID']."</td><td>".$row1['Field1']."</td><td></td><td>".$row1['Field2']."</td><td>".$row1['Field3']."</td></tr>\n" ;
}
Notice the empty <td></td>? That's because I want to have there the number of time a given ID appears on two other tables (there are foreign keys involved, obviously). I have sorted out the code I need for that:
$sql2 = "SELECT (SELECT COUNT(*) FROM MyTable2 WHERE ID2=$row1['ID'])+(SELECT COUNT(*) FROM MyTable3 WHERE ID2=$row1['ID']) AS total";
However, I'm struggling with figuring out a way to add this result to the other table. Any help?
try with this.. it inserts the total to an table after selecting the count.
"INSERT INTO total_table (total)
SELECT (SELECT COUNT(*) FROM MyTable2 WHERE ID2=$row1['ID'])+(SELECT COUNT(*) FROM MyTable3 WHERE ID2=$row1['ID']) AS total
WHERE cid = 2"

How can I insert the score into one table as an average of values from another table?

This my first query to insert the scores from the form:
$query1 = "INSERT INTO Category(timing, musicality, technique, difficulty, performance_id)
VALUES ('{$timing}', '{$musicality}', '{$technique}', '{$difficulty}','{$performance}')"
;
This works and it places the scores into a individual scores table, now im trying to take the average of these scores and put them into a table where the contestants information is located, I left a column blank for the scores to go into and heres the query I tried for that:
$query2 = "Insert into Contestants (score) select
sum(timing + musicality + technique + difficulty)/(select count(timing)* 4 from Category where performance_id = '{$performance}')
From Category
WHERE performance_id = '{$performance}';";
Whats happening is, it is inserting the correct average score into the table that I want it to, in the correct field, but instead of updating the row based on the performance_id, it creates a new row. How can I fix this?
Try this:
$query2 = "UPDATE Contestants SET score = (select
sum(timing + musicality + technique + difficulty)/(select count(timing)* 4 from Category where performance_id = '{$performance}')
From Category
WHERE performance_id = '{$performance}')
WHERE performance_id = '{$performance}';";

Changing a query to count number of unique values rather than total rows

For the query below, some rows have the same value for a field called "zip." How can I count how many unique values for "zip" rather than total rows?
$presult = mysql_query("SELECT COUNT(*) from submission ") or die(mysql_error());
SELECT COUNT(DISTINCT zip) AS unique_zip_cnt
FROM submission

Categories