1

So i need a way to be able to echo $row['username'] and echo $row['password'] where the id is specified.

So example is i have 3 rows i need to get information from with id numbers 20, 56, 88. Now i have made the query $query = "SELECT * FROM Card_File WHERE id = $id";

How do i echo this data (keeping in mind its not in a table and could be placed in random location on the webpage, and the forms the outputs will be going into will be different (eg, not got the same form field names.))

echo $row['username'] $id=20;
echo $row['password'] $id=20;

echo $row['username'] $id=56;
echo $row['password'] $id=56;

echo $row['username'] $id=88;
echo $row['password'] $id=88;

I have been looking arround and seen some very complicated examples but dont belive its that hard to echo a row by id. I may be wrong

Below is not a great example of what the ends result is but would give you an idea.

<!DOCTYPE html>
<html>
    <head>

        <?php
        // Connect to the database
        $link = mysqli_connect('localhost','root','pass','ACS_SHAREPOINT_ADDONS');
        // Select table and arguments
        $query = "SELECT * FROM Card_File WHERE id = $id";
        $result = mysqli_query($link, $query);
        ?>

    </head>



    <body>
        <?php foreach($row = mysqli_fetch_array($result)): ?>
        <div class="loginForms">
            <form target="_blank" action="https://www.website.com" method="post">
                <input type="hidden" name="username" id="auth-form-login" value="<?php $id=20 echo $row['username']; ?>"/>
                <input type="hidden" name="password" id="auth-form-pass" value="<?php $id=20 echo $row['password']; ?>"/>
                <input type="hidden" name="redirDocument" value="user"/>
                <input type="hidden" name="query" value=""/>
                <input type="hidden" name="urlString" value="doc-rc-login;lng-ww-en;tpl-;ver-;"/>
                <input type="submit" name="authSubmit" value="AVG Resellers Center" id="auth-form-submit"/>
            </form>
        </div>

<div class="loginForms">
            <form target="_blank" action="https://www.website.com" method="post">
                <input type="hidden" name="username" id="auth-form-login" value="<?php $id=56 echo $row['username']; ?>"/>
                <input type="hidden" name="password" id="auth-form-pass" value="<?php $id=56 echo $row['password']; ?>"/>
                <input type="hidden" name="redirDocument" value="user"/>
                <input type="hidden" name="query" value=""/>
                <input type="hidden" name="urlString" value="doc-rc-login;lng-ww-en;tpl-;ver-;"/>
                <input type="submit" name="authSubmit" value="AVG Resellers Center" id="auth-form-submit"/>
            </form>
        </div>

<div class="loginForms">
            <form target="_blank" action="https://www.website.com" method="post">
                <input type="hidden" name="username" id="auth-form-login" value="<?php $id=88 echo $row['username']; ?>"/>
                <input type="hidden" name="password" id="auth-form-pass" value="<?php $id=88 echo $row['password']; ?>"/>
                <input type="hidden" name="redirDocument" value="user"/>
                <input type="hidden" name="query" value=""/>
                <input type="hidden" name="urlString" value="doc-rc-login;lng-ww-en;tpl-;ver-;"/>
                <input type="submit" name="authSubmit" value="AVG Resellers Center" id="auth-form-submit"/>
            </form>
        </div>

        <?php endforeach; ?>
    </body>

</html>
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
okaybuzo
  • 13
  • 3
  • The question is difficult to understant, would you clarify it. – Rafiqul Islam Nov 09 '14 at 04:53
  • Try `while($row = mysqli_fetch_array($result))` instead of `foreach` then do `` - However, if what you're using now works and want to add on to it and to echo stuff from DB, then you still need to do a `while` loop in addition to what you have now. – Funk Forty Niner Nov 09 '14 at 04:58
  • Now this `value=""` should theoretically be `value=""` you can't pass variables in there like that. Do the same for the others and removing all `$id=xx`. Plus this `echo $row['username'] $id=20;` is invalid. Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Nov 09 '14 at 05:03
  • Well...for starters you are querying by id. FOr a speific $id, therefore your results will only ever contain that id. So you wont be able to echo $id 56, if youre just retrieving id 20. If you get what Im saying. You need the whole table, if you want to refer to different rows in your code – Kylie Nov 09 '14 at 05:08
  • Sorry for not being clear, so lets say i have ID, Name, Age in a database table. If i have 100 entries but would like to only retrieve 3 of them. How do i echo the Name and age for 3 specific ID's? hence the example of something like echo $row['name'] $id=20; that would get the name and age for ID 20 – okaybuzo Nov 09 '14 at 05:08
  • Better way would be to us IN query, for the 3 ID's you want to find. Then echo them out. – Kylie Nov 09 '14 at 05:09
  • To query for X-number of specific ids, you use `WHERE user IN(1,2,3)` – Funk Forty Niner Nov 09 '14 at 05:09

1 Answers1

0

I believe this is what you want to do.

   <?php 
    $ids_tofind = array(1,6,3); //for example
    $id = implode(",",$ids_tofind);

    $link = mysqli_connect('localhost','root','pass','ACS_SHAREPOINT_ADDONS');
    // Select table and arguments
    $query = "SELECT * FROM Card_File WHERE id IN(". $id.")";
    $result = mysqli_query($link, $query);

    while($row = mysqli_fetch_array($result)){;?>

       <div class="loginForms">
        <form target="_blank" action="https://www.website.com" method="post">
            <input type="hidden" name="username" id="auth-form-login" value="<?php echo $row['username']; ?>"/>
            <input type="hidden" name="password" id="auth-form-pass" value="<?php echo $row['password']; ?>"/>
            <input type="hidden" name="redirDocument" value="user"/>
            <input type="hidden" name="query" value=""/>
            <input type="hidden" name="urlString" value="doc-rc-login;lng-ww-en;tpl-;ver-;"/>
            <input type="submit" name="authSubmit" value="AVG Resellers Center" id="auth-form-submit"/>
        </form>
       </div>

     <?php };?>

This will echo out the form for all three users with ID 1, 6 and 3.

Kylie
  • 11,421
  • 11
  • 47
  • 78
  • You can probably skip `$id = implode(",",$ids_tofind);` and use `WHERE id IN($ids_tofind)` - `IN()` should parse those correctly. – Funk Forty Niner Nov 09 '14 at 05:15
  • Are you sure?? I wasn't aware it could do that automatically. http://stackoverflow.com/questions/1356008/passing-an-array-to-mysql – Kylie Nov 09 '14 at 05:17
  • thank you for the reply, i think it will do it except ID 1 could be in the navigation, then ID 2 could be nested a side bar on the same page. and the above would mean the results would have to be next to each other. – okaybuzo Nov 09 '14 at 05:17
  • Not necessarily. Loop and apply them to variables, then use those variables wherever you want – Kylie Nov 09 '14 at 05:17
  • @KyleK On second thought, I could be mistaken. I'd have to look at some of the stuff I've used before. But a straight `WHERE user IN(1,6,3)` would work. – Funk Forty Niner Nov 09 '14 at 05:19
  • Thanks @KyleK , great help. I will go off and try this out. – okaybuzo Nov 09 '14 at 05:21
  • @Fred-li Yes, but what if he wants to dynamically pass in the values, thats where my example comes in. – Kylie Nov 09 '14 at 05:49