Page 1 of 1

Admin Dropdown [Resolved]

Posted: Tue Aug 27, 2013 6:22 am
by Epiales
Okay, so I've got it to where it connects to database and shows the fields from the table that I want it to.

There is a dropdown that shows the user id. When I click on the drop down, displays the user id's of all the members in the database. 1 through however many. It doesn't matter what ID that I choose thoug; it only calls the last user id in the database. When I click the edit button, it shows their information, but always the last users id. So if I have 10 users, and I click to edit number 3, it only goes to user number 10 no matter what. I'm sure it's something to do with my foreach, but haven't figured it out.

Code: Select all

<?php

$result = mysql_query("SELECT * FROM stats");
echo "<table border = '1' id='table2'>
<tr>
<th>ID</th>
<th>Gold</th>
<th>Attack</th>
<th>Defense</th>
<th>Food</th>
</tr>";

while($row = mysql_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>".$row['id']."</td>";
    echo "<td>".$row['gold']."</td>";
    echo "<td>".$row['attack']."</td>";
    echo "<td>".$row['defense']."</td>";
    echo "<td>".$row['food']."</td>";    
    echo "</tr>";
}
echo "</table>";
    
?>

<form>
<?php
$select = mysql_query("Select id FROM stats");
    echo "<SELECT>";
    while($result = mysql_fetch_array($select))
    {
        foreach ($result as $value)
        { 
            echo "<OPTION>"; 
          } 
            echo $value;
            echo "</OPTION>"; 
   }    
    echo "</SELECT>";
    
$result = mysql_query("SELECT * FROM stats WHERE id='".$value."'");
$fetch = mysql_fetch_array($result);

?>
<input type="button" id="button1" value="Edit" onClick="return clearTable()">
</div>
<div class="panel2">
<br>Gold <input type = "text" name= "editName" ID= "edit1" 
value= "<?php echo $fetch['gold'];?>">
<br>Attack <input type="text" name="editSurname" ID="edit2"
value= "<?php echo $fetch['attack']?>">
<br><input type="button" id="button2" value="Update" onClick="javascript:updateTable();">
</div>
</form>
</body>
</html>    
I'm also open to suggestion on anything that can allow me to edit users information through an admin panel easier. Thanks for any info/help.

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 7:37 am
by Chris
I think you have a foreach loop there that isn't doing anything but complicating stuff. You only need to loop through once from what I can tell.

Code: Select all

echo '<select name="userId">';

$select = mysql_query("SELECT `id`,`username` FROM `stats`");
while( $result = mysql_fetch_assoc($select) )
{
    echo '<option value="'. $result['id'] .'">'; 
    echo '(' . $result['id'] ')' . ' ' . $username;
    echo "</option>"; 
}

echo "</select>";
 

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 8:11 am
by Epiales

Code: Select all

    echo '(' . $result['id'] ')' . ' ' . $username;
Parse error: syntax error, unexpected '')'' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in C:\xampp\htdocs\flash\admin\anothertest.php on line 80

Seems to be an error on that line. But I think that puts me closer. Thank you

If I write it like this, I get the tables shown, but also the fields to edit, which should not be showing up until you press edit.

Code: Select all

    echo '(' . $result['id'] . ')' . ' ' . $username;
Also shows error: Notice: Undefined variable: value in C:\xampp\htdocs\flash\admin\anothertest.php on line 86 if I put the other . in there.

Line 86:

Code: Select all

$result = mysql_query("SELECT * FROM stats WHERE id='".$value."'");

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 9:10 am
by Chris

Code: Select all

echo '<select name="userId">';

$select = mysql_query("SELECT `id`,`username` FROM `stats`");
while( $result = mysql_fetch_assoc($select) )
{
    echo '<option value="'. $result['id'] .'">'; 
    echo '(' . $result['id'] . ')' . ' ' . $username;
    echo "</option>"; 
}

echo "</select>";
 

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 9:31 am
by Epiales
Okay, I've kind of mixed the two. This is my result...

Code: Select all

<?php
$select = mysql_query("Select id FROM stats");
    echo '<SELECT name="dropdown">';
while( $result = mysql_fetch_assoc($select) )
    {
        foreach ($result as $value)
              echo '<option value="'. $result['id'] .'">'; 
             echo $value;
            echo "</OPTION>"; 
   }    
    echo "</SELECT>";

$result = mysql_query("SELECT * FROM stats WHERE id='".$value."'");
$fetch = mysql_fetch_array($result);
?>
<input type="button" id="button1" value="Edit" onClick="return clearTable()">
</div>
<div class="panel2">
<br>ID <input type = "text" name= "id" ID= "edit1" 
value= "<?php echo $fetch['id'];?>">
<br>Gold <input type="text" name="gold" ID="edit2"
value= "<?php echo $fetch['gold']?>">
<br><input type="button" id="button2" value="Update" onClick="javascript:updateTable();">
</div>
</form>
</body>
</html>
If I remove the echo $value, then it doesn't show my user id's. If I don't have the $value, as you are removing, then the values do not show up in the form I have listed below in the script above.

It's supposed to list user id's... right now it lists 1,2,3. I choose the option 2, and it should put the values in the bottom form. Instead, it always shows the 3rd choice in the bottom form and won't ever change to show the other two options. That's what I can't figure out. *Pulls hair out* LOL. I hope u're not going bald with me. Don't mean to be so frustrating :(

In otherwords, it's listing the users id and gold, but only the 3rd user no matter if you choose 1 or 2.

Eventually I will make it editable using jquery more than likely. But taking it step by step before I move on.

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 10:05 am
by Chris

Code: Select all

<html>

<head>
</head>

<body>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" metod="GET" id="UserSelectForm">
        <select name="UserId" onchange="javascript: document.getElementById('UserSelectForm').submit();">
            <?php
            // select all ID's and usersnames to populate dropdown list
            $query = mysql_query("SELECT `id`,`username` FROM `stats`");
            while( $result = mysql_fetch_assoc($select) )
            {
                echo '<option value="'. $result['id'] .'">('. $result['id'] .')'. $username ."</option>"; 
            }
            ?>
        </select>
    </form>

    <?php
    // Get selected ID data

    if( !empty($_GET['UserId']) && is_int($_GET['UserId']) )
    {
        $query = mysql_query("SELECT * FROM `stats` WHERE `id` = {$_GET['UserId']} LIMIT 1");
        while( $result = mysql_fetch_assoc($query) )
        {
            $fetch = $result;
        }
    }
    ?>

    <div class="panel2">
        <input type="button" id="button1" value="Edit" onClick="return clearTable()">
    </div>

    
    <div class="panel2">
        <br />
        ID <input type = "text" name= "id" ID= "edit1" value= "<?php echo $fetch['id'];?>" />
        <br />
        Gold <input type="text" name="gold" ID="edit2" value= "<?php echo $fetch['gold']?>" />
        <br/>
        <input type="button" id="button2" value="Update" onClick="javascript:updateTable();" />
    </div>

</body>
</html>
Can't spoon feed you forever ;)

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 10:56 am
by Epiales
Okay, when I replace the code with the above, I get this error that shows in the form input boxes at the bottom:

Code: Select all

<br /><b>Notice</b>:  Undefined variable: fetch in <b>C:\xampp\htdocs\flash\admin\Table.php</b> on line <b>108</b><br />

Code: Select all

<br /><b>Notice</b>:  Undefined variable: fetch in <b>C:\xampp\htdocs\flash\admin\Table.php</b> on line <b>110</b><br />
Also, it shows the information in the tables from the database, but in the dropdown menu, there is nothing there to choose from?

I'm just going to post the entire page, with database and all. Not like anyone can access it LOL. It's a test database before I do any implementing anyway.

I don't know why it's giving such a difficult time. But driving me up the wall. I appreciate the kindness and hospitality, and most of all, patience you are giving me. Thank you.

Code:

Code: Select all

<?php

$username = "root";
$password = "";
$hostname = "localhost"; 
$database = "game";


mysql_connect("$hostname", "$username", "$password") or die (mysql_error()) or die (mysql_error());

mysql_select_db("$database") or die (mysql_error());

?>

<html>
<head>
<script src= "jquery.js"></script>
<script type="text/javascript" language="javascript">
function clearTable()
{
     $(document).ready(function() 
    {
           $('.panel').hide();  
        $('.panel2').show(); 
    });
}
function updateTable()
{
    $(document).ready(function()
    {
        $('.panel').show();
        $('.panel2').hide(); 
    });
}
</script>
</head>
<body onLoad="updateTable()">

<table border = "1" id="table1">
<?php

?>
</table>
<div class = "panel">
<?php

$result = mysql_query("SELECT * FROM stats");
echo "<table border = '1' id='table2'>
<tr>
<th>ID</th>
<th>Gold</th>
<th>Attack</th>
<th>Defense</th>

</tr>";

while($row = mysql_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>".ucfirst($row['id'])."</td>";

    echo "<td>".$row['gold']."</td>";
    echo "<td>".$row['attack']."</td>";
    echo "<td>".$row['defense']."</td>";
    
    echo "</tr>";
}
echo "</table>";
    
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" metod="GET" id="UserSelectForm">
        <select name="UserId" onchange="javascript: document.getElementById('UserSelectForm').submit();">
            <?php
            // select all ID's and usersnames to populate dropdown list
            $query = mysql_query("SELECT `id`,`username` FROM `stats`");
            while( $result = mysql_fetch_assoc($select) )
            {
                echo '<option value="'. $result['id'] .'">('. $result['id'] .')'. $username ."</option>"; 
            }
            ?>
        </select>
    </form>

    <?php
    // Get selected ID data

    if( !empty($_GET['UserId']) && is_int($_GET['UserId']) )
    {
        $query = mysql_query("SELECT * FROM `stats` WHERE `id` = {$_GET['UserId']} LIMIT 1");
        while( $result = mysql_fetch_assoc($query) )
        {
            $fetch = $result;
        }
    }
    ?>

    <div class="panel2">
        <input type="button" id="button1" value="Edit" onClick="return clearTable()">
    </div>

    
    <div class="panel2">
        <br />
        ID <input type = "text" name= "id" ID= "edit1" value= "<?php echo $fetch['id'];?>" />
        <br />
        Gold <input type="text" name="gold" ID="edit2" value= "<?php echo $fetch['gold']?>" />
        <br/>
        <input type="button" id="button2" value="Update" onClick="javascript:updateTable();" />
    </div>

</body>
</html>
The above code is the current code with the changes you have made above.

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 12:13 pm
by MikuzA
Hello,

use mysql_fetch_array/assoc when you are expecting more than one row and every result handled inside the while loop:

As an example from your code:

Code: Select all

            $query = mysql_query("SELECT `id`,`username` FROM `stats`");
            while( $result = mysql_fetch_assoc($select) )
            {
                echo '<option value="'. $result['id'] .'">('. $result['id'] .')'. $username ."</option>"; 
            } 
use mysql_fetch_row when you obtain only one row, limit = 1 etc. As an example from your code, this doesn't really work now:

Code: Select all

$query = mysql_query("SELECT * FROM `stats` WHERE `id` = {$_GET['UserId']} LIMIT 1");
        while( $result = mysql_fetch_assoc($query) )
        {
            $fetch = $result;
        }
Rewrite it like this, since you do not need a loop when only one row is returned from database>

Code: Select all

$query = mysql_query("SELECT * FROM `stats` WHERE `id` = {$_GET['UserId']} LIMIT 1");
$fetch = mysql_fetch_row($query);
 
Hope this helps :)


EDIT:

And by looking at the code, I'm most certain this ends up being FALSE.

Code: Select all

if( !empty($_GET['UserId']) && is_int($_GET['UserId']) ) 
Due It's a Form posting :)

So change that to,

Code: Select all

if( !empty($_POST['UserId']) && is_int($_POST['UserId']) )
or

Code: Select all

if(isset($_POST['UserId']))

EDIT2:

Just a hint on troubleshooting where something might be wrong is,
What is the error?
You got an error that the variable you are trying to use is undefined, therefore the variable you are trying to use has never been written into the phase you expect it to.
When this happens, why is there no data in it?

Check that what is the IF ( ) question, if it looks ok, try it.
Add a DIE("HELLO, CODE IS GOING HERE NOW"); inside the IF-sentence. If you can't see that, IF is FALSE.
Also you could add a IF-ELSE { DIE(); }
So that if the IF is FALSE, it stops due something is wrong.

Debug. Debug. Debug. :)

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 3:19 pm
by Epiales
Thanks for trying to help. I"m going to have to find another way. No matter what I try it just won't work.

Re: Admin Dropdown

Posted: Tue Aug 27, 2013 4:09 pm
by MikuzA
Oh, I'm having hard time understand what problem are you still struggling with?
Is the original still an issue?

You have X-amount of selections in dropdown and only the last one gets chosen?