1

So I am attempting to create a modal which will edit the user (selected) from a displayed table.

I've tried 2 different types of submits to get this process to work, but firstly my PHP code.. (Yes Ik, sanitize variables, it will be added shortly)

if ($_POST['edituser']) {
   $uid = strip_tags($_POST['edituser']);
   if (isset($_POST['email'])) {
       $newemail = $_POST['email'];
       mysqli_query($con, "UPDATE `users` SET `email` = '$newemail' WHERE `uid` = '$uid'") or die(mysqli_error($con));
   }
   if (isset($_POST['usergroup'])) {
       $newusergroup = $_POST['usergroup'];
       mysqli_query($con, "UPDATE `users` SET `usergroup` = '$newusergroup' WHERE `uid` = '$uid'") or die(mysqli_error($con));
   }
   if (isset($_POST['paypal'])) {
       $newpaypal = $_POST['paypal'];
       mysqli_query($con, "UPDATE `users` SET `paypal` = '$newpaypal' WHERE `uid` = '$uid'") or die(mysqli_error($con));
   }
   if (isset($_POST['avatarlink'])) {
       $newavatarlink = $_POST['avatarlink'];
       mysqli_query($con, "UPDATE `users` SET `avatarlink` = '$newavatarlink' WHERE `uid` = '$uid'") or die(mysqli_error($con));
   }
   if (isset($_POST['job'])) {
       $newjob = $_POST['job'];
       mysqli_query($con, "UPDATE `users` SET `job` = '$newjob' WHERE `uid` = '$uid'") or die(mysqli_error($con));
   }
   if (isset($_POST['aboutme'])) {
       $aboutme = $_POST['aboutme'];
       mysqli_query($con, "UPDATE `users` SET `aboutme` = '$newaboutme' WHERE `uid` = '$uid'") or die(mysqli_error($con));
   }
   header("Location: /staff/users?success=true");
   die();
}

Now these are the two types of submit methods I tried...

1 -

<?php
            $ug = $userRow['usergroup'];
            $result = mysqli_query($con, "SELECT * FROM `users`") or die(mysqli_error($con));
            while ($row = mysqli_fetch_array($result)) {
            echo '
            <div id="editUser'.$row['uid'].'" class="modal fade" role="dialog">
               <div class="modal-dialog">
                  <div class="modal-content">
                     <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Edit User</h4>
                     </div>
                     <form method="POST" action="users">
                     <div class="modal-body">
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="username" class="control-label">Username</label>
                                     <input type="text" class="form-control" readonly="" id="username" value="'.$row['username'].'" disabled>
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="email" class="control-label">Email</label>
                                     <input type="text" class="form-control" name="email" id="email" value="'.$row['email'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="usergroup" class="control-label">Usergroup</label>';
                                     if ($ug == "Owner" || $ug == "Manager") {
                                       echo '<select name="usergroup" class="form-control">
                                          <option value="'.$row['usergroup'].'" selected="selected" disabled>'.$row['usergroup'].'</option>
                                          <option value="Client">Client</option>
                                          <option value="Freelancer">Freelancer</option>
                                          <option value="Moderator">Moderator</option>
                                          <option value="Manager">Manager</option>
                                          <option value="Owner" disabled>Owner</option>
                                       </select>';
                                     } else {
                                       echo '<input type="text" class="form-control" readonly="" id="usergroup" value="'.$row['usergroup'].'" disabled>';
                                     }echo '
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="paypal" class="control-label">PayPal</label>';
                                     if ($ug == "Owner" || $ug == "Manager") {
                                     echo '<input type="text" class="form-control" name="paypal" id="paypal" value="'.$row['paypal'].'">';
                                   } else {
                                     echo '<input type="text" class="form-control" readonly="" id="paypal" value="'.$row['paypal'].'" disabled>';
                                   }echo '
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="joindate" class="control-label">Join Date</label>
                                     <input type="text" class="form-control" readonly="" id="joindate" value="'.$row['joindate'].'" disabled>
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="joinip" class="control-label">Join IP</label>
                                     <input type="text" class="form-control" readonly="" id="joinip" value="'.$row['joinip'].'" disabled>
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="avatarlink" class="control-label">Avatar Link</label>
                                     <input type="text" class="form-control" name="avatarlink" id="avatarlink" value="'.$row['avatarlink'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="job" class="control-label">Job / Skills</label>
                                     <input type="text" class="form-control" name="job" id="job" value="'.$row['job'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="aboutme" class="control-label">About</label>
                                     <input type="text" class="form-control" name="aboutme" id="aboutme" value="'.$row['aboutme'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="portfolio_link" class="control-label">Portfolio</label>
                                     <input type="text" class="form-control" name="portfolio_link" id="portfolio_link" value="'.$row['portfolio_link'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="tos" class="control-label">Accepted TOS</label>
                                     <input type="text" class="form-control" readonly="" id="tos" value="'.$row['tos_accepted'].'" disabled>
                                 </div>
                             </div>
                         </div>
                     </div>
                     <div class="modal-footer">
                        <button class="btn btn-danger btn-block" name="edituser">Edit User</button>
                     </div>
                   </form>
                  </div>
               </div>
            </div>
            '; }
            ?>

Using this method (above), nothing happens... page just refreshes.

2

<?php
            $ug = $userRow['usergroup'];
            $result = mysqli_query($con, "SELECT * FROM `users`") or die(mysqli_error($con));
            while ($row = mysqli_fetch_array($result)) {
            echo '
            <div id="editUser'.$row['uid'].'" class="modal fade" role="dialog">
               <div class="modal-dialog">
                  <!-- Modal content-->
                  <div class="modal-content">
                     <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Edit User</h4>
                     </div>
                     <form method="POST" action="users">
                     <div class="modal-body">
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="username" class="control-label">Username</label>
                                     <input type="text" class="form-control" readonly="" id="username" value="'.$row['username'].'" disabled>
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="email" class="control-label">Email</label>
                                     <input type="text" class="form-control" name="email" id="email" value="'.$row['email'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="usergroup" class="control-label">Usergroup</label>';
                                     if ($ug == "Owner" || $ug == "Manager") {
                                       echo '<select name="usergroup" class="form-control">
                                          <option value="'.$row['usergroup'].'" selected="selected" disabled>'.$row['usergroup'].'</option>
                                          <option value="Client">Client</option>
                                          <option value="Freelancer">Freelancer</option>
                                          <option value="Moderator">Moderator</option>
                                          <option value="Manager">Manager</option>
                                          <option value="Owner" disabled>Owner</option>
                                       </select>';
                                     } else {
                                       echo '<input type="text" class="form-control" readonly="" id="usergroup" value="'.$row['usergroup'].'" disabled>';
                                     }echo '
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="paypal" class="control-label">PayPal</label>';
                                     if ($ug == "Owner" || $ug == "Manager") {
                                     echo '<input type="text" class="form-control" name="paypal" id="paypal" value="'.$row['paypal'].'">';
                                   } else {
                                     echo '<input type="text" class="form-control" readonly="" id="paypal" value="'.$row['paypal'].'" disabled>';
                                   }echo '
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="joindate" class="control-label">Join Date</label>
                                     <input type="text" class="form-control" readonly="" id="joindate" value="'.$row['joindate'].'" disabled>
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="joinip" class="control-label">Join IP</label>
                                     <input type="text" class="form-control" readonly="" id="joinip" value="'.$row['joinip'].'" disabled>
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="avatarlink" class="control-label">Avatar Link</label>
                                     <input type="text" class="form-control" name="avatarlink" id="avatarlink" value="'.$row['avatarlink'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="job" class="control-label">Job / Skills</label>
                                     <input type="text" class="form-control" name="job" id="job" value="'.$row['job'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="aboutme" class="control-label">About</label>
                                     <input type="text" class="form-control" name="aboutme" id="aboutme" value="'.$row['aboutme'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="portfolio_link" class="control-label">Portfolio</label>
                                     <input type="text" class="form-control" name="portfolio_link" id="portfolio_link" value="'.$row['portfolio_link'].'">
                                 </div>
                             </div>
                         </div>
                         <div class="row">
                             <div class="col-md-12">
                                 <div class="form-group">
                                     <label for="tos" class="control-label">Accepted TOS</label>
                                     <input type="text" class="form-control" readonly="" id="tos" value="'.$row['tos_accepted'].'" disabled>
                                 </div>
                             </div>
                         </div>
                     </div>
                     <div class="modal-footer">
                        <input type="submit" name="edituser" class="btn btn-danger btn-block" value="Edit User">
                     </div>
                   </form>
                  </div>
               </div>
            </div>
            '; }
            ?>

When attempting this method (above), the page, and the notification display.. but nothing updates from the database.

Thanks for any help :)

Community
  • 1
  • 1
Candy
  • 11
  • 1
  • 2
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or **any** user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Feb 08 '18 at 20:23
  • 2
    Don't say "I'll do the important security stuff later" because sometimes later never happens. You need to do this **first**, and **always** like your life depends on it, because it just might. This code is so full of holes it whistles if it runs too fast. – tadman Feb 08 '18 at 20:24
  • 1
    @tadman is right. Besides, how have you saved time by _not_ parameterizing your query? – Aluan Haddad Feb 08 '18 at 20:25
  • Thank you for the response @tadman , I'm kinda of new at coding, so I will definitely do as you suggested. – Candy Feb 08 '18 at 20:26
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. Once you have that set up keep an eye on your error logs to spot problems the moment they occur. – tadman Feb 08 '18 at 20:26
  • Instead of doing this enormous `echo` with lots and lots of quotes in it, just flip back to HTML mode: `?> .... (html) ... – tadman Feb 08 '18 at 20:28
  • @tadman Do you have any suggestions for one? I've heard mixed reviews about most. – Candy Feb 08 '18 at 20:29
  • The thing about frameworks is there's no "best" framework, everyone has opinions and preferences, which is fine. There's dozens of them of varying quality, but I'd suggest looking at a full-featured one like [Laravel](http://laravel.com/) as well as a much more minimalist one like [Fat-Free Framework](https://fatfreeframework.com/) to get a sense of what they offer. Maybe you like having a lot of support, having all the tools on hand, or maybe you prefer to compose your environment by hand-picking dependencies. It's entirely up to you. – tadman Feb 08 '18 at 20:30
  • Besides the mentioned SQL injections, you should also be extremely careful with outputting values from your database. Use htmlspecialchars or htmlentities every time, otherwise people can inject evil javascript into your website. – Stratadox Feb 08 '18 at 20:35

1 Answers1

0

As mentioned in the comments, your code is currently highly insecure. You most definitely should use parametrised queries to prevent people from messing with your database, and protect yourself from xss attacks in order to prevent your website from becoming malware.

The answer to your question, though, is to give your input fields a name attribute in addition to an id.

Right now, your form is not posting any data - hence the lack of any changes.

Stratadox
  • 1,291
  • 8
  • 21
  • 1
    I notice someone downvoted this answer. To the best of my knowledge, it is a correct and relevant answer. If there is something incorrect or objectionable about it, I would be much obliged to learn what that is. – Stratadox Feb 12 '18 at 19:23
  • Not my downvote but its a poor answer. Linking to other stuff even when on SO does not make for a good answer. At least add a code snippet from the OP's code demonstrating the use of a `name` attribute – RiggsFolly Mar 17 '18 at 00:07