-1

I am quite new to PHP and am following tutorials to insert data into database. Syntax is all running fine, just that typing in data in the form doesn't insert any data into database. I've created out the table, and have checked if the insert query is working correctly in MySQL.

Controller: studCredController

    class studCredController extends CI_Controller {

         public function __construct()
    {
        parent::__construct();
        $this->load->database();
        //load Model
        $this->load->model('studCredModeller');
    }
        //Register students
        public function registerStud() {

        //load registration view form
        $this->load->view('Home');

        //Check the submit button
        if($this->input->post('save'))
        {
            $username = $this -> input -> post('username');
            $email = $this -> input -> post('email');
            $password = $this -> input -> post('password');
            $admin_no = $this -> input -> post('adminNo');
            $phone = $this -> input -> post('phone');

            //call method of studCredModeller and pass variable as parameter
            $this -> studCredModeller -> saveRecords($username, $email, $password, $admin_no, $phone);
            echo "Records Saved Successfully";

            }
        }
    }

Model: studCredModeller

class studCredModeller extends CI_Model
    {
        public function saveRecords($username, $email, $password, $adminNo, $phone);
        {
            $query="insert into locka.stud_login values('$username','$email','$password','$admin_no','$phone', ' ')";
            $this->db->query($query);
    }
    }

View: Home

  <form method="post">
   <div class="row">
      <div class="col-xs-6 col-sm-6 col-md-6">
         <div class="form-group">
            <label>Username</label>   
            <input type="text" class="form-control" name="username" placeholder="Username">
         </div>
      </div>
      <div class="col-xs-6 col-sm-6 col-md-6">
         <div class="form-group">
            <label>Email</label>
            <input type="email" class="form-control" name="email" placeholder="Email">
         </div>
      </div>
   </div>
   <div class="row">
      <div class="col-xs-6 col-sm-6 col-md-6">
         <div class="form-group">
            <label>Password</label>
            <input type="password" class="form-control" name="password" placeholder="Password">
         </div>
      </div>
      <div class="col-xs-6 col-sm-6 col-md-6">
         <div class="form-group">
            <label>Admin No</label>
            <input type="text" class="form-control" name="adminNo" placeholder="AdminNo">
         </div>
      </div>
      <div class="col-xs-6 col-sm-6 col-md-6">
         <div class="form-group">
            <label>Phone number</label>
            <input type="number" class="form-control" name="phone" placeholder="Phone">
         </div>
      </div>
   </div>
   <input type="submit" value="Submit" name="save" class="btn btn-skin btn-block btn-lg">
   <p class="lead-footer">* We'll contact you by phone & email later</p>
</form>

Thank you for looking at the codes. I have been stuck at this for a few days, trying out different tutorials.

3
  • 2
    You are open to SQL injections. Also don't store plain text passwords. Commented Jul 27, 2018 at 20:28
  • are you using javascriot/jquery to trigger action when clicking on submit button? If not there is no action related with the form, I'd uggest to read quickly through this: codeigniter.com/user_guide/helpers/form_helper.html and this: codeigniter.com/user_guide/tutorial/create_news_items.html Commented Jul 27, 2018 at 20:40
  • In addition to what @user3783243 said, why are you not using CI's query builder? It's there for a reason. Commented Jul 27, 2018 at 20:51

2 Answers 2

1

You will find this much easier to do if you use Query Builder and in particular the insert() method.

Modify the portion of the controller that reacts to a submit

//Check the submit button
if($this->input->post('save'))
{
    $data['username'] = $this->input->post('username');
    $data['email'] = $this->input->post('email');
    $data['password'] = $this->input->post('password');
    $data['admin_no'] = $this->input->post('adminNo');
    $data['phone'] = $this->input->post('phone');

    //call method of studCredModeller and pass variable as parameter
    $result = $this->studCredModeller->saveRecords($data);
    echo $result === TRUE ? "Records Saved Successfully" : "Records did not save";
}

Modify the model method

public function saveRecords($data);
{
    //returns true if insert works, otherwise false
    return $this->db->insert('stud_login', $data);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your insert statement is incorrect. See: https://www.w3schools.com/sql/sql_insert.asp

You are missing the column names.

Unless:

If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table.

If you are specifying all columns and they are in order I would suggest turning on db_debug in database.php to see what's up.

Although I wouldn't even bother with making me own queries and I'd use query builder which automatically escapes the input data:

$data = array(
    'username' => $username,
    ...
);
$this->db->insert('tablename', $data);

and for obvious reasons DON'T store plaintext passwords or use md5 or sha1 to hash the passwords. There are other, better password hashing algos out there.

4 Comments

This answer would be much better with everything before to see what's up. removed.
depends on what the issue is doesnt it? as we don't have the table schema the first thing could very well be the issue
The first part of the answer is a guess. Your insert statement is incorrect. is possibly false. That is valid SQL (depending on the values going into it). I'd use the mysql manual as a reference as well dev.mysql.com/doc/refman/5.7/en/insert.html If you do not specify a list of column names for INSERT ... VALUES or INSERT ... SELECT, values for every column in the table must be provided by the VALUES list or the SELECT statement. If you do not know the order of the columns in the table, use DESCRIBE tbl_name to find out.
many times answers on stack are best guesses when the user isn't forthcoming with information. "That is valid SQL (depending on the values going into it)" the part in parenthesis is exactly my point. As this already has an answer this is a moot point, but feel free to add your own if you don't feel mine is adequately addressing potential issues.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.