PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 14, 2022

[FIXED] Unable to insert POST variables into database

 March 14, 2022     codeigniter, mysql, php     No comments   

Issue

I'm a beginner in CodeIgniter and I'm trying to insert the value that I'm getting from the post into my database, for which I'm using the following code:

View Class:

<form method="POST">
    <label>Username</label>
    <input type="text" name="username"/>
    <br/>
    <label>Password</label>
    <input type="password" name="password"/>
    <br/>
    <button type="submit" name="login">Sign In</button>
    </form>

Controller Class:

class Auth extends CI_Controller{
    public function index(){
        $this->load->model("membership_model");
        $username=$this->input->post('username');
        $password=$this->input->post('password');
        $content='';
        return $this->load->view('auth/login');
        if(isset($_POST["username"]) && isset($_POST["password"])){
            $content['data'] = $this->membership_model->create_user($username,$password);
        }
}

Model Class:

class Membership_model extends CI_Model {
         public function create_user($username,$password){
            $this->db->set('display_name',$username);
             $this->db->set('password',$password);
             $this->db->insert("table1");
             return true;
         }
     }

But when I hit submit, I do not get any data in my database for some reason.

Thanks in advance.


Solution

The following code should work well.

Controller modification:

<?php

namespace App\Controllers;

use CodeIgniter\Controller;

    class Auth extends Controller{
        public function index(){
            if($this->input->method() == "post") {
                $this->load->model("membership_model");
                $username=$this->input->post('username');
                $password=$this->input->post('password');
                $content='';
                
                if(isset($_POST["username"]) && isset($_POST["password"])){
                    $content['data'] = $this->membership_model->create_user($username,$password);
                }
            }
            return $this->load->view('auth/login');
    }

Model modification:

public function create_user($username, $password){
    $data = array(
        'display_name'=>$username,
        'display_name'=>$password
    );
    $this->db->insert("table1", $data);
    return ($this->db->affected_rows() != 1) ? false : true;
}


Answered By - Raphael
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing