- This topic is empty.
-
AuthorPosts
-
June 7, 2016 at 12:54 pm #242640
cscodismith
ParticipantI am trying to make it so when I am changing a users admin level in the control panel I made it only targets the one that I chose (clicked on) but when the bootstrap modal pops up it is displaying every user that is not an admin instead of just the one I selected. Here is a screenshot of what I mean. So I would want it show ‘Changing admin level for lowheartrate’ instead of both users and obviously only change one users admin_level instead of both.
My code currently looks like this:
// users $sth2 = $conn->prepare("SELECT id, username, email, admin_level FROM users WHERE admin_level < 1"); $sth2->execute(); $users = $sth2->fetchAll(); <!-- myModal --> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <?php // Display username that we are changing! foreach ($users as $row) { echo '<p class="title">Changing admin level for ' . $row['username'] . '!</p>'; } ?> </p> </div> <div class="modal-body"> <?php // Display current admin level foreach ($users as $row) { echo '<p class="title2">Current admin level is level ' . $row['admin_level'] . '. <br /><p class="title">Change to admin level:</p><br /> <input class="level-input" type="text" name="admin_level" placeholder="Change to level .... (ex: 1)" required/> </p>'; } ?> </div> <div class="modal-footer"> <button class="level-submit" type="submit" data-dismiss="modal">Submit Changes</button> </div> </div> </div> </div>
June 7, 2016 at 1:37 pm #242641rkieru
ParticipantYou need to review your SQL, because right now it’s doing exactly what it was written to do: Pull a list of users from the database where
admin_level < 1
.In your post you reference that you are selecting some number of users, but your query does not provide any indication that this is ever being applied.
June 7, 2016 at 1:50 pm #242642cscodismith
ParticipantYes the query I am using is correct for the admin panel because the module is echoing every single user that is
admin_level < 1
but I need to make new query now that only echo’s the id that I have clicked on? Not sure exactly how to make a query that will only show one specific users$row['username']
and$row['admin_level']
depending on which user I actually select.I know
$_POST
gets the variable that the user actually submits but seeming in my scenario I am trying to get the variable that I click on what kind of php would I use to fetch that in my foreach statement?Also, forgot to put in this code but this is what is linked to the ‘Change Level’ link in the website which is making me a bit confused as to how to query that I selected a specific user.
foreach ($users as $row) { echo '<center>'; echo '<tr>'; echo '<td class="info">' . $row['id'] . '</td>'; echo '<td class="info">' . $row['username'] . '</td>'; echo '<td class="info">' . $row['email'] . '</td>'; echo '<td class="info">' . $row['admin_level'] . '<br /><a data-toggle="modal" data-target="#myModal">Change Level</a></td>'; echo '</tr>'; echo '</center>'; }
That code is what also is displaying all of the users that are
admin_level 0
June 8, 2016 at 6:40 am #242664rkieru
ParticipantAssuming you are flagging multiple users for an
admin_level
change via something like a checkbox, you would have something like this:<input type="checkbox" name="change_id[]" value="<?php echo $row['id']; ?>">
The brackets after
change_id
indicate that this is an array, so now you have an array of user IDs that are subject to the change, under$_POST['change_id']
From there you can use
IN()
to modify your ‘results’ query along the lines of:$sth2 = $conn->prepare("SELECT id, username, email, admin_level FROM users WHERE id IN(' . implode(",", $_POST['change_id']) .'");
Note: For the purposes of sanitization you would likely first want to make sure that
$_POST['change_id']
exists and is an array, asimplode()
will return an error otherwise.Resources:
June 9, 2016 at 12:03 am #242692cscodismith
ParticipantFor some reason when using the query you provided I am getting a syntax error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in A:\wamp\www\hn\index.php on line 25
June 9, 2016 at 6:57 am #242697rkieru
ParticipantI believe I used single quotes and omitted a closing parenthesis in my outline earlier. Try something like this, with the caveat that you’ll obviously need to test it yourself, and adjust variable names as needed.
$sth2 = $conn->prepare("SELECT id, username, email, admin_level FROM users WHERE id IN(" . implode(",", $_POST['change_id']) . ") );
Remember – if you get an error like the one you posted above, see it as a learning experience in correcting syntax errors. It’s usually an issue of missing semi-colon or a parenthesis missing, etc. Learning to identify and correct these sort of issues will make you a better programmer.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.