2

I want to insert 2 strings data to 1 colomn(combine) in my database. This is my proccess code.

models:

public function edit_code($input_id,$input_name, $input_code){
          $data['name'] = $input_name;
          $data['code'] = $input_code;
          $this->db->where('id',$input_id);
          $this->db->update('code',$data);
        
    }

view :

<div>  
                            <div>
                            <h2>Edit Code:</h2>
                                <div>
                                <?php echo form_open('login/edit_code');?>
                                <?php 
                                
                                $codeLAT= 'KUCK';

                                $input_name = array(
                                'name'  => 'input_name',
                                'class' => 'forminput',
                                'size' => '30'
                                );
                                
                                $input_code = array(
                                'name'  => 'input_code',
                                'class' => 'forminput',
                                'size' => '30'
                                );
                                                                                                                               
                                ?>

<span>Name</span>: <?php echo form_input($input_name,$page['name']) ?><br/>
<span>Code</span>: <?php echo $codeLAT , form_input($input_code,$page['code']) ?>
<?php echo form_hidden('input_id',$page['id']);?>
<?php echo form_submit('submit', 'Submit', 'class=buttonss'); ?>
<?php echo form_close();?>
</div>

controler :

public function save_edit_code(){
            
        $this->check_logged_in();
        
        $codeLAT = 'KUCK';

        $input_id = $_POST['input_id'];
        
        $input_name = $_POST['input_name'];
                
        $input_code = ($_POST[$codeLAT]. '' .$_POST['input_code']);
                
        $this->Promo_model->edit_promo($input_id,$input_name, $input_code);
        redirect('login/codepage');
    }

In here i want to combine $codeLAT with $inputcode

Example :

Script : $input_code = ($_POST[$codeLAT]. '' .$_POST['input_code']);

Save in my database colomn code : KUCK009912

But in my code, my form just save $inputcode in my colomn database. Not include $codeLAT.

In my example just save 009912.

4
  • check the input_code field type, if its a number then you cannot store string, so change it to varchar and it will work. Commented Sep 16, 2019 at 9:08
  • sorry the input_code its succesfull save to my database. But the codeLAT not save. For example my system must save KUCK00912, but just 00912 save it in my colomn code Commented Sep 16, 2019 at 9:12
  • what is field type of that field, integer or varchar ? Commented Sep 16, 2019 at 9:12
  • my field varchar Commented Sep 16, 2019 at 9:14

2 Answers 2

1

I think there is a clear issue that is being overlooked here.

The form that will be generated by your view will look something like the HTML below.

<form action="http://localhost/index.php/login/edit_code" method="post" accept-charset="utf-8">

    <span>Name</span>: <input type="text" name="input_name" value="" class="forminput" size="30">
    <br>
    <span>Code</span>: KUCK<input type="text" name="input_code" value="" class="forminput" size="30">

    <input type="hidden" name="input_id" value="">
    <input type="submit" name="submit" value="Submit" class="buttonss">
</form>

As you can see $codeLAT is not a form input and will thus not be sent to the server on form submit.

$_POST is an array that only contains the variables that were passed to the server via the HTTP POST method, which occurs on form submit. Due $codeLAT not being an input in the generated HTML form, the variable will never be added to the $_POST array.

This is why $_POST[$codeLAT] does not retrieve any value. Secondly, if there was a variable named codeLAT in the $_POST array then you would access it using $_POST['codeLAT'].

To make use of $_POST['codeLAT'] you could include it as a hidden input on the form with the following CodeIgniter function.

form_hidden('codeLAT', 'KUCK');

The codeLAT variable would then be included in the POST method and added to the $_POST array as a result, but would not be visible input to the user.

If you prefer to not add it as a hidden input then you can just access the variable, $codeLAT, as declared in you controller with the following.

$input_code = $codeLAT. '' .$_POST['input_code'];

I would recommend that you take a look at the CodeIgniter Form Helper documentation where the use of the form_hidden function is explained.

I would also recommend that you take a look at the PHP Form Handling at W3 Schools and an explanation of the $_POST variable.

Sign up to request clarification or add additional context in comments.

1 Comment

your comment same with the first answer, but with detail information. Thanksyouu
1

Problem while you storing data in $input_code here $input_code = ($_POST[$codeLAT]. '' .$_POST['input_code']);

Here you already store data $codeLAT so $_POST is not needed.

use below code.

$input_code = $codeLAT. '' .$_POST['input_code'];

1 Comment

Thanks yout code succesfully for me.. but the last answer is very important to another people, because its very detail. Thanksyou for your code :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.