MySql, loop (while foreach)

I have a big problem. I am getting data from a database. I want to display them in bookmarks. My method is a loop within a loop. I'll show you the code. My problem is that the result (second loop) outputs all data.

The first loop displays each username with the same ID on its own tab. it works. the second circuit works on the same principle. but the second loop does not filter.

(!) The first loop shows me correctly for each username with the same id of its own tab. but the sec on loop shows me all event_names, not just user event names. Summary: I have 3 users. every user. user 1 has his own tab. (2 events) user 2 has his own tab. (1 event) user 3 has his own tab. (1 event) in the second loop, I ask the events of each user. user 1 tab should show 2 events on user 2 tab, 1 event should appear, and 1 event should appear on user 3 tab. BUT, 4 events appear on each tab (user 1,2 and 3). all user events ... this is a looping issue, isn't it? (!)

Can anyone help me?

Here are some pictures of the table and output:

My table users

enter image description here

code:

<?php    
$db = mysqli_connect(DB_HOST, DB_BENUTZER, DB_password, DB_NAME);
mysqli_set_charset($db, "utf8");
$sqli2 = "SELECT username, premium FROM users WHERE id =  $id   ";  
$result2 = mysqli_query($db, $sqli2);   
foreach ($result2 as $row2)     
        {   
           echo '<div class="tabs">';

      

My table:

enter image description here

code:

$db = mysqli_connect(DB_HOST, DB_BENUTZER, DB_password, DB_NAME);
mysqli_set_charset($db, "utf8");
$sqli = "SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  '".$row2['username']."'   ";  
$result = mysqli_query($db, $sqli);    
while ($row = mysqli_fetch_assoc($result)) 
{
    echo '<h3> '. $row['unternehmen'] .' '. $row['event_name'] .' </h3>'; 

      

and my conclusion: enter image description here

Where is the mistake? I can't believe it :-( Please help me !!!!

here's the complete code:

<?php


$db = mysqli_connect(DB_HOST, DB_BENUTZER, DB_password, DB_NAME);
mysqli_set_charset($db, "utf8");
$sqli2 = "SELECT username, premium FROM users WHERE id =  $id   ";  
$result2 = mysqli_query($db, $sqli2);


 foreach ($result2 as $row2) {


           echo '<div class="tabs">';
  echo '<ul>';
  echo '<li><a href="#tabs-1"> '. $row2['username'] .' </a></li>';
  echo '<li><a href="#tabs-2">Proin dolor</a></li>';
  echo '<li><a href="#tabs-3">Aenean lacinia</a></li>';
  echo '</ul>';
  echo '<div id="tabs-1">';
  echo '<p>Proin elit ac sollicitudin mi sit amet mauris. Nam elementum quam us.</p>';






  echo '</div>';
  echo '<div id="tabs-2">';




   echo '<p><div class="accordion">';


$db = mysqli_connect(DB_HOST, DB_BENUTZER, DB_password, DB_NAME);
mysqli_set_charset($db, "utf8");
$sqli = "SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  '".$row2['username']."'   ";
$result = mysqli_query($db, $sqli);


    while ($row = mysqli_fetch_assoc($result)) 
        {        


    echo '<h3> '. $row['unternehmen'] .' '. $row['event_name'] .' </h3>'; 
    echo '<div> ';
    echo '<form enctype="multipart/form-data" method="post" >';
    echo '<legend>Event Daten</legend>';
    echo '<label for="event_name">Name:</label>';
    echo '<input type="text" id="event_name" name="event_name" value="'. $row['event_name'] .'" /><br />';
    echo '<label for="beginn">Beginn:</label>';
    echo '<input type="text" id="beginn" name="beginn" value=" '. $row['beginn'] .'" /><br />';
      echo '<label for="party_id" value=" '. $row['party_id'].'"> Party ID:'. $row['party_id'].' </label>';
      echo '<input type="radio" id="party_id" name="party_id" value=" '. $row['party_id'] .'" /><br />';

  echo '</fieldset>';
  echo '<input type="submit" value="ร„nderungen speichern" name="partyspeicher" />';
  echo '</form>';
        echo '</div>';

         }

    echo '</div>';
    echo '</p>';


    echo '</div>';
    echo '<div id="tabs-3">';
    echo '<p> Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>';
  echo '</div>';
  echo '</div>';
   }      





?>

      

+3


source to share


2 answers


I'll just post this as an answer instead of a comment.

Your problem seems to be directly related to your sql string:

$sqli = "SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  '$row2[username]'   ";

      

Note $row2[username]

, it should be $row2['username']

, but it might cause a problem in your sql string. Create another variable$username

$username = $row2['username'];
$sqli = "SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  '$username'   ";

      



And see if that has changed.

For future reference:

I would recommend using PDO (more secure) instead of mysqli. Relatively easy to use with many useful features such as bindValue () which would replace $username = $row2['username'];

something like

 $sqli = $db->prepare("SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  ':user'   ");
$sqli->blindValue(':user', $row2['username'], PDO::PARAM_STR);

      

(if $ db was a PDO object) http://php.net/manual/fr/pdostatement.bindvalue.php

+2


source


your second request should be listed

    $sqli = "SELECT party_id, event_name, beginn, ende, unternehmen 
FROM party WHERE unternehmen =  '".$row2['username']."'   "

      

you are missing one quete in the username, which means it must be $ row ['username'];



always include an error or use a function mysqli_error()

, they'll tell you what's wrong.

Please check this link to shave off your knowledge http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html

+1


source







All Articles