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

Friday, February 18, 2022

[FIXED] Error writing CodeIgniter incorrect forms to database

 February 18, 2022     codeigniter, email, mysql, php, phpmyadmin     No comments   

Issue

I've added a contact form and I've added a captcha validation, the contact form is working.

but even the faulty forms write to the database. Even if you press the submit button without filling the error form at all, it is registered in the database.

How do I get only successful jerseys to register in the database

this is my controller codes

public function contact(){

    $viewData = new stdClass();
    $viewData->viewFolder = "contact_v";
    $this->load->helper("captcha");

    $config = array(
        "word"          => '',
        "img_path"      => 'captcha/',
        "img_url"       => base_url("captcha"),
        "font_path"     => 'base_url("fonts/corbel.ttf")',
        "img_width"     => 150,
        "img_height"    => 50,
        "expiration"    => 7200,
        "word_length"   => 4,
        "font_size"     => 40,
        "img_id"        => "captcha_img",
        "pool"          => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        "colors"        => array(
            'background' => array(0,0,0),
            'border'     => array(0,0,0),
            'text'       => array(255,255,255),
            'grid'       => array(0,0,0),
        )

    );

    $viewData->captcha = create_captcha($config);

    $this->session->set_userdata("captcha", $viewData->captcha["word"]);

    $this->load->view($viewData->viewFolder, $viewData);


}  


public function send_contact_message(){


    $this->load->library("form_validation");
    $this->load->model("Contact_model");

        $insert = $this->Contact_model->add(
            array(
                "name"          => $this->input->post("name"),
                "email"         => $this->input->post("email"),
                "message"       => $this->input->post("message"),
                "subject"       => $this->input->post("subject"),
                "created_at"    => date("Y-m-d H:i:s")
            )
        );


    $this->form_validation->set_rules("name", "Ad", "trim|required");
    $this->form_validation->set_rules("email", "E-posta", "trim|required|valid_email");
    $this->form_validation->set_rules("subject", "Konu", "trim|required");
    $this->form_validation->set_rules("message", "Mesaj", "trim|required");
    $this->form_validation->set_rules("captcha", "Doğrulama Kodu", "trim|required");
    if($this->form_validation->run() === FALSE){

        // TODO Alert...
        $this->session->set_flashdata('info','Action Completed');
        redirect(base_url("iletisim"));


    } else {


        if($this->session->userdata("captcha") == $this->input->post("captcha")){

            $name = $this->input->post("name");
            $email = $this->input->post("email");
            $subject = $this->input->post("subject");
            $message = $this->input->post("message");

            $email_message = "{$name} isimli ziyaretçi. Mesaj Bıraktı <br><b>Mesaj : </b> {$message} <br> <b>E-posta : </b> {$email}";

            if(send_email("", "Site başvuru Mesajı | $subject", $email_message)){
                $this->session->set_flashdata('success','Action Completed');
                redirect(base_url("iletisim"));
                // TODO Alert..
            } else {
                $this->session->set_flashdata('success','Action Completed');
                redirect(base_url("iletisim"));
                // TODO Alert..
            }

        } else {
            //başarısıs
        $this->session->set_flashdata('error','Action Not Completed');
        redirect(base_url("iletisim"));

        }

    }

}

Solution

I went through it and found that you are inserting the data first and then performing validation which is not at all correct.

You should write your function in the following way,

public function contact(){

    $viewData = new stdClass();
    $viewData->viewFolder = "contact_v";
    $this->load->helper("captcha");

    $config = array(
        "word"          => '',
        "img_path"      => 'captcha/',
        "img_url"       => base_url("captcha"),
        "font_path"     => 'base_url("fonts/corbel.ttf")',
        "img_width"     => 150,
        "img_height"    => 50,
        "expiration"    => 7200,
        "word_length"   => 4,
        "font_size"     => 40,
        "img_id"        => "captcha_img",
        "pool"          => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        "colors"        => array(
                            'background' => array(0,0,0),
                            'border'     => array(0,0,0),
                            'text'       => array(255,255,255),
                            'grid'       => array(0,0,0),
        )
    );

    $viewData->captcha = create_captcha($config);
    $this->session->set_userdata("captcha", $viewData->captcha["word"]);
    $this->load->view($viewData->viewFolder, $viewData);
}  


public function send_contact_message(){

    $this->load->library("form_validation");
    $this->load->model("Contact_model");

    $this->form_validation->set_rules("name", "Ad", "trim|required");
    $this->form_validation->set_rules("email", "E-posta", "trim|required|valid_email");
    $this->form_validation->set_rules("subject", "Konu", "trim|required");
    $this->form_validation->set_rules("message", "Mesaj", "trim|required");
    $this->form_validation->set_rules("captcha", "Doğrulama Kodu", "trim|required");
    if($this->form_validation->run() === FALSE) {
        // TODO Alert...
        $this->session->set_flashdata('info','Action Completed');
        redirect(base_url("iletisim"));
    } else {

        $insert = $this->Contact_model->add(
            array(
                "name"          => $this->input->post("name"),
                "email"         => $this->input->post("email"),
                "message"       => $this->input->post("message"),
                "subject"       => $this->input->post("subject"),
                "created_at"    => date("Y-m-d H:i:s")
            )
        );

        if($this->session->userdata("captcha") == $this->input->post("captcha")){

            $name = $this->input->post("name");
            $email = $this->input->post("email");
            $subject = $this->input->post("subject");
            $message = $this->input->post("message");

            $email_message = "{$name} isimli ziyaretçi. Mesaj Bıraktı <br><b>Mesaj : </b> {$message} <br> <b>E-posta : </b> {$email}";

            if(send_email("", "Site başvuru Mesajı | $subject", $email_message)){
                $this->session->set_flashdata('success','Action Completed');
                redirect(base_url("iletisim"));
                // TODO Alert..
            } else {
                $this->session->set_flashdata('success','Action Completed');
                redirect(base_url("iletisim"));
                // TODO Alert..
            }

        } else {
            //başarısıs
        $this->session->set_flashdata('error','Action Not Completed');
        redirect(base_url("iletisim"));

        }
    }
}

This will prevent you from inserting wrong data. If validation fails then it will redirect you on the page which you have written inside "if" block of second function



Answered By - Riosant
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

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