There is a table table_products and it has a login column. How to make a request for the user to see what ads he submitted?

This code displays all products of all users. This code is not correct, but I think it should just be improved, but I don’t know how

 session_start(); if ($_SESSION['auth'] == 'yes_auth') { include("include/config.php"); include("functions/functions.php"); $all_products = mysql_query("SELECT * FROM table_products WHERE login='$login'",$connect); $result_count = mysql_num_rows($all_products); 

enter image description here

In general, here, there are 10 ads 9 for admin 1 for Eugene, if I go under admin, then all ads are shown both by admin and Eugene, and if under Eugen, then both admina and Eugen are shown. And I need each login to show their ads. That seems well described the problem

  • First, determine where the login is taken from in the table_products table. Then figure out how you php-script should understand what exactly the user came to him. Without these two conditions, the answer is impossible to give in principle - rjhdby

3 answers 3

 $login_products = mysql_query("SELECT * FROM table_products WHERE login='$login'",$connect); 

Where $ login - user login

  • The trouble is that there are a lot of users, and I need everyone to show their own - Eugene
  1. Do not use the mysql extension, it is outdated, not supported and does not develop. Use mysqli or PDO

  2. Read a little about SQL. You need to specify the selection condition in the request

In your case

 $login = "логин пользователя"; $all_products = mysql_query("SELECT * FROM table_products WHERE login='$login'",$connect); $result_count = mysql_num_rows($all_products); 

According to the mind.

 $login = "логин пользователя"; $db = new mysqli(данные для коннекта); $stmt = $db->prepare("SELECT * FROM table_products WHERE login=?"); $stmt->bind_param('s', $login); $stmt->execute(); $result_count = $stmt->get_result()->num_rows; 
  • There are a lot of users, for each I am tormented by writing) - Eugene
  • @ Eugene, if you are talking about $login , then where do you come from? I added it in order that it would be clear what should be in it. If about $db = new mysqli(данные для коннекта); , then initialization is done once at the very beginning, and then used as needed. - rjhdby

In order to extract all records from the table table_products corresponding to the user with login 'user', the following WHERE condition should be added to the SQL query

 SELECT * FROM table_products WHERE login = 'user' 

PS Please note that the mysql extension you use is deprecated and has already been excluded from PHP 7. It’s better to focus on the mysqli or PDO extensions.