skip to Main Content

I try to use SELECT in echo because I make while loop and I want to SELECT id in table account so I will use this data inside form

here is some of my php code:

    $sql = "SELECT * FROM account";
    $getquery = mysqli_query($conn, $sql);

    while ($runrows = mysqli_fetch_array($getquery)) {
        echo '
      <!-- td -->
      <li class="account">
       <div class="products">
        ' . $runrows["id"] . '
       </div>
       <span class="status">
        <span></span>
        ' . $runrows["username"] . '</span>
       <div class="button-wrapper">
       ' . $runrows["rank"] . '
       </div>
       <div class="button-wrapper" style="margin-right: 10px;">
       <a href="#edit' . $runrows["id"] . '"><button style="margin-right: 10px; background-color: #4299e1; border: none; border-radius: 5px; font-size: 25px; font-family: supermarket; border: black solid 1px;">edit</button></a>
       <a href="#delete"><button style="margin-right: 10px; background-color: #f56565; border: none; border-radius: 5px; font-size: 25px; font-family: supermarket; border: black solid 1px;">delete</button></a>
      </div>
      </li>
      <div id="edit' . $runrows["id"] . '" class="overlay" style="font-family: supermarket">
    <div class="popup">
        <h2 style="font-family: supermarket"><i class="fa-solid fa-user-pen"></i> edit data of user ID : ' . $runrows["id"] .'</h2>
        <a class="close" href="/backend/user/"><i class="fa-solid fa-xmark"></i></a>
        <div class="content" style="overflow: hidden; font-size: 20px;">
            <form action="collector.php" method="post">
            <?php
            $sql = "SELECT * FROM account WHERE id = ' . $runrows["id"] . '";
            $queryId = mysqli_query($conn,$sql);
            $idRows = mysqli_fetch_array($queryId)
            ?>
                <div class="txt_field">
                    <input type="text" required name="phone" max-length="10" style="color: var(--content-color); font-family: supermarket;" value="' . $idRows["id"] .'">
                    <span></span>
                    <label style="font-family: supermarket;">' . $runrows["id"] . '</label>
                </div>
            </form>
        </div>
    </div>
</div>';

as you can see I try to use SELECT in echo but at input feild it don’t show me nothing (all of php syntax are correct but I don’t sure if it correct or not in echo that have sql inside)

here is some of my database

id username password
1 test1 testno1
2 test2 testno2

If anyone have better way or can fix my code please let me know. Thanks

2

Answers


  1. I think it should something like this in your php file (replace html tags with yours, but the logic should be like this):

    <?php
    //get query result
    ?>
    <html>
    <body>
        ...
        <table border="1">
            <thead>
            <tr>
                <td>id</td>
                <td>username</td>
                <td>password</td>
            </tr>
            </thead>
            <tbody>
            <?php
            $results = [];
            while ($runrows = mysqli_fetch_array($getquery)) {
                $results[] = $rows;
            }
            foreach ($results as $row):
                ?>
                <tr>
                    <td><?php echo $row["id"]; ?></td>
                    <td><?php echo $row["username"]; ?></td>
                    <td><?php echo $row["rank"]; ?></td> 
                </tr>
            <?php endforeach;
            ?>
            </tbody>
        </table>
        ...
        </body>
    </html>
    
    Login or Signup to reply.
  2. Have you tried error_reporting for find errors in php code? Have you checked the DB?

    Try

    <?php
        error_reporting(E_ALL & ~E_DEPRECATED);
        
        define("DBPWD","password");
        define("DBHOST","localhost");
        define("DBUSER","username");
        define("DBNAME","nameofdb");
        
        $conn = mysqli_connect(DBHOST,DBUSER,DBPWD,DBNAME) or die("<b>Connection error</b>" ); 
        $sql = "SELECT * FROM account";
        $getquery = mysqli_query($conn, $sql);
    
        while($runrows = mysqli_fetch_array($getquery,MYSQLI_ASSOC)){
        $id = $runrows["id"];
        
            echo '
          <!-- td -->
          <li class="account">
           <div class="products">
            ' . $id . '
           </div>
           <span class="status">
            <span></span>
            ' . $runrows["username"] . '</span>
           <div class="button-wrapper">
           ' . $runrows["rank"] . '
           </div>
           <div class="button-wrapper" style="margin-right: 10px;">
           <a href="#edit' . $id . '"><button style="margin-right: 10px; background-color: #4299e1; border: none; border-radius: 5px; font-size: 25px; font-family: supermarket; border: black solid 1px;">edit</button></a>
           <a href="#delete"><button style="margin-right: 10px; background-color: #f56565; border: none; border-radius: 5px; font-size: 25px; font-family: supermarket; border: black solid 1px;">delete</button></a>
          </div>
          </li>
          <div id="edit' . $id . '" class="overlay" style="font-family: supermarket">
        <div class="popup">
            <h2 style="font-family: supermarket"><i class="fa-solid fa-user-pen"></i> edit data of user ID : ' . $id .'</h2>
            <a class="close" href="/backend/user/"><i class="fa-solid fa-xmark"></i></a>
            <div class="content" style="overflow: hidden; font-size: 20px;">
                <form action="collector.php" method="post">
                    <div class="txt_field">
                        <input type="text" required name="phone" max-length="10" style="color: var(--content-color); font-family: supermarket;" value="' . $id .'">
                        <span></span>
                        <label style="font-family: supermarket;">' . $id . '</label>
                    </div>
                </form>
            </div>
        </div>
    </div>';
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search