简体   繁体   中英

update rows in mysql with codeigniter

so. i've been trying to update the password. randomly generated. but. if i refresh the database, nothing happens. what's wrong with my code?

this is my controller:

function create_password()
{
    $this->load->model('members_model');

    $newRow = array(
        'name' => $this->input->post($a);
        'value' => 
    );
    $this->membership_model->passchange($newRow);
}

this is my model:

function passchange()
{
$this->db->select();
$this->db->from('membership');
$this->db->where('security_answer',$this->session->userdata('security_answer'));

$q=$this->db->get();

if($q->num_rows() > 0) {
$data = array();
foreach($q->result() as $row) {

    $data['result']=$row;

    $this->db->where('email_address', $this->session->userdata('email_address'));
    $this->db->update('membership', 'password');
}

return $data;
}

}

and this is my view:

<?php echo form_open('login/create_password')?>
<?php $this->load->view('includes/header');
$CI =& get_instance();
$CI->load->model('membership_model');
$result = $CI->membership_model->passchange();
foreach($result as $row) {

    }
?>

Your Email Address is: &nbsp; <?php echo $row->email_address;?> <br/>
<?php
$a = random_string('alnum', 6);
?> 

Your password is: &nbsp; <?php echo $a; "<br/>"?>

<p align="right"><?php echo anchor('login/signin', 'Back to Login Page'); ?></p>

the password in my database is blank. it is not updating. please help. thank you in advance! :)

The error lies here

$this->db->update('membership', 'password');

This method gets array as second parameter

$data['password'] = $newpassword;
$this->db->update('membership',  $data);

Also use this method

echo $this->db->last_query();

to see what query has been run recently. And dont forget to use where instruction with update instruction. Read the userguide again it is very helpful

https://www.codeigniter.com/userguide2/database/active_record.html

Marishka you really really need to watch those tutorials I gave you before continuing this process. You're going to get to a point where your application is so badly messed up it will be nearly impossible to fix.

Almost everything you're doing there shows bad practices, not the least of which is stop loading the CI instances and getting data from the model in the view.

Here are the problems with your code as it stands.

  1. You're creating an array of $newRow using a post value you're not getting because you aren't posting anything. Creating a variable in a view does not automatically post it back, to post data to a controller it needs to be in a form and that form needs to have an action to call the controller function.

  2. $a isn't going to equal anything because random_string isn't a php function that I know of, it looks like something you just pulled out of the middle of a tutorial without reading how it was created, so $a is going to be null, even if it were posted back to the controller it would still have no value to insert into the database.

  3. In your model that you're passing the $newRow to you're never actually getting the $newRow into the function.

  4. This has nothing to do with it not working but you're basing which record to update on the security answer? The possibility of numerous users having the same answer makes this a horrendous idea.

  5. As was previously said the second parameter in active records update function should be an array. So to understand what that function is doing it breaks down like this.

    $this->db->update ($tableToUpdate, $arrayOfValuesToUpdate);

The array of values are key pairs so for password you'd have an array like the following:

$arrayOfValuesToUpdate = array(
    'nameOfPasswordFieldInYourDB' => 'value you want to set the password to'
);

I really get the feeling you're trying to learn CI from various examples and tutorials and you really do need to just stop and look at the nettuts tutorials I linked you before from step one. Your practices are terrible and your understanding of what you're actually doing is low enough that you're just copying and pasting and not getting what it is that's supposed to actually be happening. The Nettuts tutorials will give you that base understanding you need to do these things properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM