Transfer data from one table to another on insert

Samuel

I'm currently making some internet forums using php and mysql, and I have hit a roadblock... I don't know how to implement the constraints in need for this, or even how to implement it into my site exactly (sql trigger or procedure or php script?).

So anyways, Ihave a db named blog, with two tables: posts and threads. In the posts table there are two columns that would be involved in this, reply_id and parent_id, on each insert into the posts table, I need the just inserted row's reply_id to be set as the threads' table topic_order, where topic_id=parent_id...

The page that displays the topics, sorts them by highest topic_order, thus higher reply_id would mean a higher topic_order, and would bump a thread to the top of the list.

$result_t = mysqli_query($con,"SELECT * FROM threads WHERE board_id='$row_b[board_id]' ORDER BY topic_order DESC");

This is currently being displayed http://fenster.myftp.org/blog/forums/board.php?id=1

An sql trigger was what I needed and through trial and error I found that I could use reply_id to find which row was just inserted and then limit it to only that row's data

CREATE TRIGGER `bump` AFTER INSERT ON `posts`
 FOR EACH ROW UPDATE threads
SET topic_order=(SELECT reply_id FROM posts ORDER BY reply_id DESC LIMIT 1)
WHERE topic_id=(SELECT parent_id FROM posts ORDER BY reply_id DESC LIMIT 1)
echo_Me

replace this

    WHERE board_id='$row_b[board_id]'

by

   WHERE board_id='".$row_b['board_id']."'
                            ^--------^--------you need quotes here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

insert data from one table into another using a condition mysql

How to insert the data from one column into another table

How to insert data from one table to another in array format?

Copy and then insert data from one cell in a table to another cell in that table

Retrieve data from one table and insert/update in another table in mysql

Insert Data from one table to another leaving the already existing rows

Procedure to insert data from one column into two columns in another table

Procedure to insert a data from one table to another table after calculations

want to insert data from one table into another in php?

Insert data from a table to another one in MySQL with where condition

How to transfer data from one table of a database to a table in another database

Insert Data From One Table to Another Table with default values

Transfer data from one database to another with Datamapper

Insert Data From One Table to Another Table and Add New Values

insert data from one table to a single column another table with no relation

Insert Data From One Table To Another - MySQL

insert data from one table to another - Not Working - possible bug?

Laravel insert data from one table to another table error

Insert one table data into another table

How to insert data from one table into another as PostgreSQL array?

Insert bulk of data from one table to another MySQL Python

Transfer from one table to another

Query to transfer data from one database to another

How to insert data from one table to another in laravel?

find and extract data from one table and insert it into another in php and mysql

Transfer files from one table to another in impala

Transfer data from one table to another identical table

MySql: Insert data from one table to another

How To Insert Data With Matching Parameter Values From One Table to Another?