0

DETAIL ABOUT MY GOAL:

So I made a little box on the side of my site that lets choose what we want to see, but I'm having trouble with that box. It sends the information in Get, basically there is nothing in the Get so the site displays all the images regardless of the tag it has then once we have chosen a tag with the small box, that send the tag in Get then reload the page which is now supposed to display only the images which have the chosen tag. When I echo my var it works, I can see the right word, but It seems to have a problem with the select statement.

ACTUAL RESULT:

Actually, it simply shows nothing, except the echo of my var to see if it works,, my code isn't displaying anything. Yes in my DB there are items with the tag i'm looking for because when I display every images, so without wanting only one tag I can see the tag and they are there.

WHAT I'VE ALREADY READ TO TRY TO FIX THE PROBLEM:

Strange behavior with SELECT WHERE A="$var" SQL

PHP SQL where statement with loop variable

PHP SQL and use of 'WHERE' with a variable

PHP & SQL WHERE (changing variable)

SQL query with variable "Where"

sql pdo php where in variable

add PHP variable in SQL WHERE

WHAT I'VE ALREADY TRIED:

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = $tag ORDER BY pic_id DESC";

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = '$tag' ORDER BY pic_id DESC";

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = ' . $tag . ' ORDER BY pic_id DESC";

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = :$tag ORDER BY pic_id DESC";

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = ':$tag' ORDER BY pic_id DESC";

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = "$tag" ORDER BY pic_id DESC";

'SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = ' . $tag . ' ORDER BY pic_id DESC';

'SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = &tag ORDER BY pic_id DESC';

'SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = &$tag ORDER BY pic_id DESC';

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = ". $tag . " ORDER BY pic_id DESC";

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = ($tag) ORDER BY pic_id DESC";

"SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = {$tag} ORDER BY pic_id DESC";

I've even tried with the $_GET instead of the my var

HERE IS THE MINIMUM CODE SHOWING HOW IT GET AND WHERE IT SELECT:

$tag = $_GET['tagChoosen'];
        if(isset($_GET["tagChoosen"]))
        {
          echo "<br/> $tag";
          $result = "SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = ':$tag' ORDER BY pic_id DESC";
          $results = $pdo->query($result);
        }
        else
        {
          $result = "SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid ORDER BY pic_id DESC";
          $results = $pdo->query($result);
        }

        while ($data = $results->fetch())
        {
          $resultat = "SELECT username FROM tblClients WHERE client_id = ".$data['client_id'];
          $resultats = $pdo->query($resultat);
          while ($datas = $resultats->fetch())
          {
            $clientUsername = $datas['username'];
          }
          echo "
            <div class='imgContainer'>
              <div class='img'>
                <img class='theImg' src='{$data['pic_path']}' onclick='window.open(this.src)'>
                <div id='infoContainer'>

                  <div id='sousInfoContainer1'>
                    <div id='info1'>
                      <div class='info'>
                        <p>Date de publication: {$data['date_posted']}</p>
                      </div>
                    </div>
                    <div id='info2'>
                      <div class='info'>
                        <p>Tag: {$data['tag']}</p>
                      </div>
                    </div>
                  </div>

                  <div id='sousInfoContainer1'>
                    <div id='info3'>
                      <div class='info'>
                        <p>Publieur: {$clientUsername}</p>
                      </div>
                    </div>
                  </div>

                </div>
                <a href='{$data['pic_path']}' download='{$data['pic_path']}'>
                  <button class='btn'><i class='fa fa-download' alt=''></i> Télécharger</button>
                </a>
                <hr class='botrow'>
              </div>
            </div>
          ";
        }
      ?>
    </div>
      <div id="rightContainer">
        <form id='CatChoice' action='' method='get'>
                <label><br/>Choisissez la catégorie que vous voulez voir</label>
                <select name='tagChoosen'>
                  <option value='Drôle'>Drôle</option>
                  <option value='Dark'>Dark</option>
                  <option value='Informatique'>Informatique</option>
                  <option value='Programmation'>Programmation</option>
                  <option value='Gaming'>Gaming</option>
                  <option value='Animaux'>Animaux</option>
                  <option value='Film'>Film</option>
                  <option value='Série'>Série</option>
                  <option value='18+'>18+</option>
                  <option value='École'>École</option>
                  <option value='Travail'>Travail</option>
                  <option value='Soviet'>Soviet</option>
                </select>
                <input type='submit' value='Choisir' name='submit'>
              </form>
      </div>
    </div>

I tried too, to put the entire while in both of if and else, but it doesn't work

I don't really like to use stackoverflow because I always get ban from posting question, cause people think my questions are useless, but I'm just learning, no question is useless if she helps someone. But i'm really stuck. I've been stuck there for now 3 days.

Dolotboy
  • 74
  • 7
  • Two whiles while quering does not work $results->fetch() and $results->fetch(). Should be split up or create two pdo objects. The first query should work. – Wimanicesir Sep 08 '20 at 13:04
  • Split up like this ? if(isset($_GET["tagChoosen"])) { echo "
    $tag"; $result = "SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = ($tag) ORDER BY pic_id DESC"; } else { $result = "SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid ORDER BY pic_id DESC"; } $results = $pdo->query($result); while ($data = $results->fetch()) {
    – Dolotboy Sep 08 '20 at 13:07
  • There are a lot of different options to do this. I linked another stackoverflow here above. Why persons don't like your questions here is mostly because you are a beginner. And almost all beginner questions are answered here somewhere. So don't take it personal. However I'm not checking the comment full of code. If you have still question I will post a full answer to help you :) – Wimanicesir Sep 08 '20 at 13:09
  • Last comment :p You should JOIN tblClients on Clientid :) – Wimanicesir Sep 08 '20 at 13:11
  • So insteand of my WHERE client_id = "$...... I should JOIN the table, but why ? Isn't the same ? – Dolotboy Sep 08 '20 at 13:13
  • So if I understang the link you sent, I should PDOFetchALl to put them in an array (table) and be able to get them when I want without need an other SQL request to my DB ? Am I wrong ? – Dolotboy Sep 08 '20 at 13:26
  • That is something you could do. Anyhow i posted a solution that would be most fit to your problem i think. – Wimanicesir Sep 08 '20 at 13:29

2 Answers2

-1

Because of the follow up questions, I created a solution that might work. But it could be that some adjustments should be made.

I joined your table because the client id will be the same. Now you have a more complete dataset to work with.

<?php
    $tag = $_GET['tagChoosen'];
    $result = "SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid INNER JOIN tblClients ON tblMemovid.client_id = tblClients.client_id";

    if(isset($_GET["tagChoosen"]))
    {
        result .= " WHERE tag = '$tag'";
    }
    // Let's join the tblClients into our selection.
    $result .= " ORDER BY pic_id DESC";
    $results = $pdo->query($result);

    // In this loop we already know the username because we joined the tabel tblClients
    while ($data = $results->fetch())
    {
        echo "
        <div class='imgContainer'>
            <div class='img'>
            <img class='theImg' src='{$data['pic_path']}' onclick='window.open(this.src)'>
            <div id='infoContainer'>
                <div id='sousInfoContainer1'>
                <div id='info1'>
                    <div class='info'>
                    <p>Date de publication: {$data['date_posted']}</p>
                    </div>
                </div>
                <div id='info2'>
                    <div class='info'>
                    <p>Tag: {$data['tag']}</p>
                    </div>
                </div>
                </div>
                <div id='sousInfoContainer1'>
                <div id='info3'>
                    <div class='info'>
                    <p>Publieur: {$data['username']}</p>
                    </div>
                </div>
                </div>
            </div>
            <a href='{$data['pic_path']}' download='{$data['pic_path']}'>
                <button class='btn'><i class='fa fa-download' alt=''></i> Télécharger</button>
            </a>
            <hr class='botrow'>
            </div>
        </div>
        ";
    }
    ?>
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30
  • I don't understand why you JOIN the table, because this is to show under the image who posted it, when I JOIN the table it simply shows the current user username – Dolotboy Sep 08 '20 at 13:30
  • The reason I recommend JOIN is because the dataset would be more than just the username. Kind of the oppositie what you are saying here. Did you try the code? Did you have an error? – Wimanicesir Sep 08 '20 at 13:33
  • I tried it and it simply display nothing but when I remove this line $result .= "INNER JOIN tblClients ON tblMemovid.client_id = tblClients.client_id"; images reappear – Dolotboy Sep 08 '20 at 13:35
  • Here is some pictures if it could help you understand the thing the loop while is to put every pic of the databse in a column on the site, like instagram ofr example https://ibb.co/mqB8zgh https://ibb.co/4pcTQQ9 – Dolotboy Sep 08 '20 at 13:39
  • Ok I made a mistake I see now. The query has a mistake I will edit. – Wimanicesir Sep 08 '20 at 13:45
  • I moved the ORDER BY. Can u check again? – Wimanicesir Sep 08 '20 at 13:46
  • Nope, still a white page, without any img, even thebox disapeared https://ibb.co/rmh9RHG The JOIN line is again the problem – Dolotboy Sep 08 '20 at 13:51
  • Hmm it's hard to guess your query. Can u query in a program like PhpMyAdmin? Aren't PHP errors displayed? Can you var_dump($data);exit;) in the beginning of the loop and show me what's inside? – Wimanicesir Sep 08 '20 at 14:01
  • array(10) { ["pic_id"]=> string(1) "5" [0]=> string(1) "5" ["pic_path"]=> string(11) "photo/5.jpg" [1]=> string(11) "photo/5.jpg" ["date_posted"]=> string(19) "2020-09-03 13:29:49" [2]=> string(19) "2020-09-03 13:29:49" ["tag"]=> string(6) "Drôle" [3]=> string(6) "Drôle" ["client_id"]=> string(1) "1" [4]=> string(1) "1" } – Dolotboy Sep 08 '20 at 14:08
  • The is no query problem in PhpMyAdmin when I SELECT what I want with my WHERE, as I said i'm sure that the problem in my PHP code is the WHERE with a var – Dolotboy Sep 08 '20 at 14:10
  • Hmm why use : before $tag? This should work: "SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag = '$tag' ORDER BY pic_id DESC"; – Wimanicesir Sep 08 '20 at 14:12
  • I removed the : as well. – Wimanicesir Sep 08 '20 at 14:12
  • You also dumped the array without the join right? – Wimanicesir Sep 08 '20 at 14:13
  • You should learn about prepared statements – Your Common Sense Sep 08 '20 at 14:18
  • @YourCommonSense, a downvote because no prepared statements? :p In that case he also should learn about a db class, depency injection and probaly a good front end framework (but ajax will suffice). Anyhow this person just wants a current solution. You don't have to go from 0 to 100 always. There is much more to improve in this code than 'prepared statements' – Wimanicesir Sep 08 '20 at 14:20
  • This is exaclty what I said, I found the solution, it cost nothing to leave it there if someone find this post it could help too, because the duplicate you gave doesn't answer the same type of thing, they answer for when the var is at the end, not in the middle, this is what I had difficulties for, so my answer got removed and my reputation too, I'll not be able to post again if I get more negativ and i'll be in the obligation to create another account (AGAIN) This is a question and every question is good if it helps someone – Dolotboy Sep 08 '20 at 14:21
  • Hey @Elzardi, I gave the join one more chance. Can you check the code? – Wimanicesir Sep 09 '20 at 09:38
  • Oh, yeah thx it works, now to code look better :D – Dolotboy Sep 09 '20 at 18:14
-1

I found the problem, as I said, the problem was the WHERE with a var, all I had to do is this:

  $result = "SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag ='".$tag."' ORDER BY pic_id DESC";

Instead of everything I tried

Thx Wimanicesir for your help, i'll try to JOIN to make it better

Dolotboy
  • 74
  • 7
  • To be clear, I didn't downvote. However I refuse to believe that it doesn't work without concatenating. "SELECT pic_id, pic_path, date_posted, tag, client_id FROM tblMemeovid WHERE tag ='$tag' ORDER BY pic_id DESC" has to work the same. – Wimanicesir Sep 08 '20 at 14:17
  • I know it's strange, but I've already tried this and it doesn't work, this is the first thing I tried – Dolotboy Sep 08 '20 at 14:24
  • This code is no different from your second attempt. Nevertheless, all your attempts are wrong, as you ought to use prepared statements instead of adding variables to SQL directly – Your Common Sense Sep 08 '20 at 14:24
  • I still don't know what's the utily of prepared statement, when, where and how to use it, there is no bible on internet that says DO THIS instead of THIS. I learn as I can and try to follow some tutorial. Simply doing my best and I get put down because I don't know things, how am I supposed to learn like this, anyways i'm done, thx for your advice, next time i'll look for 6 days instead of 3, maybe it gonna help – Dolotboy Sep 08 '20 at 14:29
  • Hey @Elzardi, Don't listen to common sense. Just do it one step at a time :) I was thinking, the join doesn't work because it has to become before the where :) – Wimanicesir Sep 09 '20 at 09:34