Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:55:16+00:00 2026-05-31T14:55:16+00:00

After creating a handful of functions for my new website, I quickly realized things

  • 0

After creating a handful of functions for my new website, I quickly realized things were getting out of hand with all the include files using perpetual programming, so I decided to learn and convert my currently written functions into OOP classes and convert from mysql to mysqli. Hasn’t been too bad of a transition so far but I’ve reached a function that has/needs multiple queries, one a SELECT to check the data before I UPDATE it. What I’ve written so far, using a prepared statement, works like a charm but it only does the SELECT. Where I’m stuck is where it comes time to UPDATE the db. Here’s what I have so far:

public function ban() {
  $connection  = Database::getConnection();
  $user_id = $_POST['user'];
  $feedback = '';
  $query = "SELECT banned, user_name
            FROM users
            WHERE user_id = ?";
  $stmt = $connection -> prepare($query);

  // Check for a multi-user selection on POST
  if (count($user_id) > 1) {
     foreach ($user_id as $value) {
        $stmt -> bind_param('i', $value);
        if (!$result = $stmt -> execute()) {
           $feedback .= "Did not execute.";
        } else {
           $stmt -> bind_result($banned, $user);
           $stmt -> fetch();
           if ($banned == 1) {
              $feedback .= $user . " is already banned.<br />";
           } else {
              // This is where I need the code to update the database
              // with the users who aren't already banned.
           }
        }
     }
     $stmt -> close();
     return $feedback;
  } else {
     // This is where the UPDATE will be for a single person ban
     // if only one was selected on POST
  } 
}

Can I create/execute a second prepared statement for the UPDATE injection, running that inside another loop in the section I need the code or would it be best to avoid that, or would that even work? I’m sure a mysqli_multi_query is probably the best way to go, having to rewrite the function again, since I found out (after writing this much of the function) that you can’t use a prepared statement with a multi_query injection. Rewriting isn’t a big deal but the help for using multi_query is far and few between. PHP website has a lot of documentation but lets be honest, it’s pretty darn confusing.

The UPDATE query would look something like this:

$explain = mysql_real_escape_string($_POST['banExplain']);
UPDATE users
SET banned = '1', ban_reason = '$explain'
WHERE user_id = '$value'"

ANY help with this would be greatly appreciated. Hope I explained well enough with what I need to do. If not, let me know. THANKS!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:55:17+00:00Added an answer on May 31, 2026 at 2:55 pm

    The code sample is still very procedural.

    You should have a method to check if a user is user_banned and a method to ban_user a user.

    The user_banned method should take a user_id and return a boolean.

    The ban_user method should take a reason and user_id.

    There should be another function or method which does the loop.

    Cast the user_id as an array and you can do one loop.

    Use exceptions to handle errors.

    <?php
        //the model
        ...  
    
        public function update_users (array $user_ids)
        {
           $result = array();         
    
           foreach ($user_ids as $user_id) {
               if (!$this->user_banned($user_id)) {
                   $this->ban_user($user_id, $reason);
    
               } else {
                   $result[$user_id] = "already banned";
               }
    
            return $result;
        }
        ...
    
        //the controller
    
        //prevent XSS, cast as integers or use filter_var or something
        $user_ids = sanitize((array) $_POST['user']);
        try {
            $result = $obj->update_users($user_ids);
        } catch (Exception $e) {
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

even after creating new object of unmarshaller every time, threads are getting blocked Please
Immediately after creating all the beans declared in the various context files of my
After creating new cocoa application,I have added localization string files localizeFile.strings(English) and localizeFile.strings(Spanish) In
After creating a new form, I usually perform this ritual: Change the name into
After creating a new object (Foo), I set the key (BarId) for an EntityRef
When I try to install gem like bundler or rake after creating new gemset
After creating a new ASP.NET Web Service I want it only to support HTTP-POST
After creating this object ClassName obj = new ClassName { Currency = eur, DateStart
After creating a new user john with: CREATE USER john PASSWORD 'test'; CREATE DATABASE
After creating a new workbook, I am trying to solve a newly created worksheet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.