Welcome Guest, Not a member yet? Register   Sign In
all db records get updated instead of only 1
#1

(This post was last modified: 01-05-2021, 08:42 AM by [email protected].)

Hu guys, I am working on a school project.

I have a validateUser function in my Users.php controller:
PHP Code:
public function validateUser()
    {
        
$data = [];

        
UserModel::validateUser($this->request->getPost('id_user')); // validate the user

        
if (session()->get('success')) 
        {
            return 
redirect()->to('admin_panel');
        } 
        else 
        {
            
            echo 
view('templates/header'$data);
            echo 
view('admin_panel2',$data);
            echo 
view('templates/footer');
        }
    }


This is my validateUser function in my UserModel.php

PHP Code:
   public static function validateUser($id_user)
    {
        
        $model = new UserModel();

        $data =  ['id_role' => '2'];

        $model->update($id_user,$data);

        session()->setFlashdata('success''Successfuly Validated');
    } 


My view

Code:
  <div class="container">
    <div class="row">
        <div class="col-12">
            <div class="container">
                <p></p>
                <div class="panel-group">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h6 class="panel-title">
                                <a data-toggle="collapse" href="#collapse1">
                                    <?php
                                    echo "<h3>Users To Validate</h3>";
                                    ?>
                                </a>
                                <?php

                                ?>
                            </h6>
                        </div>
                        <div id="collapse1" class="panel-collapse collapse">
                            <div class="panel-body">
                                <h4></h4>
                                <ol>
                                    <div class="container">
                                        <div class="row">
                                            <div class="col-12 col-sm8- offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white form-wrapper">
                                                <div class="container">
                                                    <?php if (session()->get('success')) : ?>
                                                        <div class="alert alert-success" role="alert">
                                                            <?= session()->get('success') ?>
                                                        </div>
                                                    <?php endif; ?>
                                                    <?php foreach ($usersToValidate as $row)
                                                        echo '<li class="nav-item">' . $row->firstname . '  ' . $row->lastname . '<a class="nav-link active" href="validate_user?id_user='.$row->id_user.'">Validate</a></li>';



                                                    ?>
                                                    <ul class="nav">

                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </ol>
                            </div>
                            <div class="panel-footer">

                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="container">
                <hr>
                <?php if (isset($permissions)) : ?>
                    <div class="col-12">
                        <div class="alert alert-danger" role="alert">

                        </div>
                    </div>
                <?php else :   ?>
                <?php endif; ?>

                <?php if (isset($validation)) : ?>
                    <div class="col-12">
                        <div class="alert alert-danger" role="alert">
                            <?= $validation->listErrors() ?>
                        </div>
                    </div>
                <?php else :   ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
The problem is that in my 'users' table all 'id_role' records get changed to '2'. I only wish to update a single record in the db.


Any idea what I'm doing wrong ? I read the CI4 documentation on db updates...
Reply
#2

Use getGet() instead getPost()
Reply
#3

(01-05-2021, 10:28 AM)iRedds Wrote: Use getGet() instead getPost()


That did the trick. Thanks !

I think I understand why it didn't work before: because my view page sends the parameter 'id_user' via the GET method. Is this correct ? 

Could I force my view to send the parameter via POST instead of GET ? I want to try to juggle with the methods now so that I get a better grasp. 

Thanks again !
Reply
#4

Also your update is backwards.

PHP Code:
$model->update($id_user,$data);

// should be.
$model->update($data$id_user); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(01-05-2021, 12:30 PM)InsiteFX Wrote: Also your update is backwards.

PHP Code:
$model->update($id_user,$data);

// should be.
$model->update($data$id_user); 


I found this in the documentation https://codeigniter.com/user_guide/model...aving-data


PHP Code:
$data = [
    'username' => 'darth',
    'email'    => '[email protected]'
];

$userModel->update($id$data); 


And my code also works in this fashion. If I reverse the parameters like you said I get an error.
Reply
#6

(01-05-2021, 10:36 AM)[email protected] Wrote:
(01-05-2021, 10:28 AM)iRedds Wrote: Use getGet() instead getPost()


That did the trick. Thanks !

I think I understand why it didn't work before: because my view page sends the parameter 'id_user' via the GET method. Is this correct ? 

Could I force my view to send the parameter via POST instead of GET ? I want to try to juggle with the methods now so that I get a better grasp. 

Thanks again !

Yes you are right. 
All data sent in url like ?кey=value are placed in the $_GET array. 

There is no easy way to send data using the $_POST method (at least I don't know about that).
Or use a form to submit data or AJAX. In this way, you can specify the method for sending data.
Reply
#7

(This post was last modified: 01-06-2021, 04:14 AM by [email protected].)

(01-06-2021, 04:10 AM)iRedds Wrote:
(01-05-2021, 10:36 AM)[email protected] Wrote:
(01-05-2021, 10:28 AM)iRedds Wrote: Use getGet() instead getPost()


That did the trick. Thanks !

I think I understand why it didn't work before: because my view page sends the parameter 'id_user' via the GET method. Is this correct ? 

Could I force my view to send the parameter via POST instead of GET ? I want to try to juggle with the methods now so that I get a better grasp. 

Thanks again !

Yes you are right. 
All data sent in url like ?кey=value are placed in the $_GET array. 

There is no easy way to send data using the $_POST method (at least I don't know about that).
Or use a form to submit data or AJAX. In this way, you can specify the method for sending data.

Thanks for the help, much appreciated !

Alex
Reply




Theme © iAndrew 2016 - Forum software by © MyBB