Hello ! Guys help in my first endeavors in writing your admin

1) There is a table with projects: (portfolio)

2) A table is also created with all employees (sotrudniki)

enter image description here

3) In the administration panel when adding a new job, the employees who participated in creating the project are attached. Data stored in a table (post_tags)

postid = ID of the project, tagid = ID of the employee I managed to write all the data to the database, all the data is correctly distributed on the tablets. But with the conclusion on the project page, the problem. In my case, the four entries are only the same. PHP code for output

<?php $result = mysql_query("SELECT * FROM portfolio WHERE id=$id"); $myrow = mysql_fetch_array($result); $resultworkers = mysql_query("SELECT * FROM posts_tags",$db); $myrowworkers = mysql_fetch_array($resultworkers); $resultsupport= mysql_query("SELECT * FROM sotrudniki",$db); $myrowsupport = mysql_fetch_array($resultsupport); $count = mysql_num_rows($resultworkers); do { if ($myrow['id'] == $myrowworkers ['postid']) { printf ("<div class='team_block'> <img src='images/%s' alt='Pixwhite'> <div class='desc'> <span>%s</span> <p>%s</p> </div> </div>",$myrowsupport["sotrundikimg"],$myrowsupport["dolgnost"],$myrowsupport["fio"]); } else { } } while ($myrowworkers = mysql_fetch_array($resultworkers)); ?> 

  • 1. Learn joins. 2. Throw out in the trash mysql_query and use PDO. For the name of the posts_tags table in which the connections of employees with projects lie, you will be kicked for a long time and with pleasure when they are caught. - Ipatiev
  • And, the company do..while from the popov video courses. Well, then nothing will help you. - Ipatiev
  • I waited for comments for Popov. Slowly, the whole code is corrected, but to my regret it all began with his course. - RazorXL
  • phpforum.su/index.php?showtopic=32799 For popvschiny there is a rehabilitation course :) - ilyaplot
  • Koterov and Kostarev are now my rehabilitation) - RazorXL

1 answer 1

For this task you must use a JOIN.
For your SQL structure, the query will look something like this:

 SELECT * FROM sotrudniki as s INNER JOIN post_tags pt on s.id = pt.tagid INNER JOIN portfolio p on p.id = pt.postid; 

Try to execute this query in PHPMyAdmin and see that the results of the three tables are combined. But in your place I would call the names of tables and fields normal names in the Latin alphabet without translit.

I advise you to read about JOIN in MYSQL, table synonyms (select from teble as t ) . Instead of SELECT * list the required fields for the selection (by the way, the as construct also works).

  • In my example, you will not have employees without a portfolio or portfolio without employees. If necessary, do not use INNER before JOIN - ilyaplot
  • If you do not use INNER before JOIN, then nothing will change - Ipatiev
  • Everything is well displayed, only on all pages of projects. Just why all the records in all the works. How to supplement the code so that each project has its own employees? $ resultworker = mysql_query ("SELECT sotrundikimg, dolgnost, fio FROM sotrudniki as s JOIN portfolio_workers pt on s.id = pt.tagid JOIN portfolio p on p.id = pt.postid", $ db); - RazorXL
  • If each job has its own id which is registered in the url. For example ? Id = 67 - RazorXL
  • Add a condition to the query. where p.id = нужный id - ilyaplot