简体   繁体   中英

mysql CREATE TABLE with JOIN multiple tables … php automatically update the new table when data is added to the tables that are joined

At my book site that I am developing I display records located in a mysql db. I connect and interact with PDO.

The db consists of several tables. Users (including myself) can insert data and also links to images (which are uploaded and stored in a folder on the server, not the db).

Everything has been functional, effective, efficient and I paginate the table rows on a few different pages at my book site.

I recently decided that we should have (in one table) usernames and book locations (in one table) displayed along with other book data (in one table) all in one single table which I paginate at the site. Visitors at the site can then search (I use PDO prepared statements) by username (among other criteria) to see what a specific user has contributed and also to see the location of the book.

I created a new table by join the three tables and I paginate the rows on my book site. Everything appeared to function well for the first day.

However, I soon noticed that when a user (including myself) inserts data into one (or more) of the three tables which are joined, the new table that I created is not updated with the new data.

Here is mysql:

CREATE TABLE flyingpig as
SELECT cow.super_imagelinks, cow.super_pass, cow.super_title, 
cow.super_copyright_year, cow.super_author, cow.super_link, cow.super_date, 
pass_stamps.stamp_username, pass_stamps.stamp_country, users.user_name
enter code here
FROM cow
JOIN pass_stamps ON super.cow_pass = pass_stamps.super_pass
JOIN users ON cow.super_username = users.user_id

Here is a segment of my php:

            <tr>             
               <td><img src="<?php echo $row['super_imagelinks']; ?>" width="194" height="272"></td>
               <td><?php echo $row['super_pass']; ?></td>
               <td><?php echo $row['super_title']; ?></td>
               <td><?php echo $row['super_copyright_year']; ?></td>
               <td><?php echo $row['super_author']; ?></td>
               <td><?php echo $row['user_name']; ?></td>
               <td><?php echo $row['stamp_country']; ?></td>
               <td><?php echo $newDate = date("F j, Y, g:i a", strtotime($row['super_date'])); ?></td>
               <td><a href="<?php echo $row['super_link']; ?>"><span style="color: #417DC1;">view</span></a></td>
           </tr>

Its works but when a user inserts new data in one (or more) of the original three tables the new table that I created does not update.

I've been learning php, PDO and mysql since last year. I don't know a lot but I am diligently searching for a solution for updating the new table (which consists of three joined tables).

If anyone reading here has time can you please point me in the correct direction to where I may be able to figure out a solution? I would be very grateful. Thanks

Based on what you just described you actually want a View and not a new table.

If your PHP code inserts into tables Cow and Users and not in flyingpig and your select statement where you pull data to display (which you haven't included) is from flyingpig obviously you don't see new data. You will only see data until the flyingpig table was created.

If my understanding is correct you can simply create a View with the SQL statement which you are using to create a table ( flyingpig ). Take a look at this

https://dev.mysql.com/doc/refman/5.0/en/create-view.html

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM