Codeigniter - Input post array insert into mysql database - mysql

I dont understand how to exactly insert a row into a MySQL table. If I use my code, I add a new row for every post. Rather than that, I want only one row with all the values.
Here is the code sample.
HTML:
echo form_open('account/update');
echo "<p><label for='gender'>Gender</label>
<input type='text' name='gender' id='gender' value='".$gender."' />
</p>
<p>
<label for='age'>Age</label>
<input type='text' name='age' id='age' value='".$age."' />
</p>
<p>
<label for='bio'>Biografie</label>
<input type='text' name='bio' id='bio' value='".$bio."' />
</p>
<p>
<label for='skills'>Skills</label>
<input type='text' name='skills' id='skills' value='".$skills."' />
</p>
<p><input type='submit' value='Save' /></p>";
echo form_close();
Controller:
function update()
{
$userid = $this->session->userdata("userid");
$datanew = array(
'userid' => $this->session->userdata("userid"),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'bio' => $this->input->post('bio') ,
'skills' => $this->input->post('skills')
);
$this->session->set_userdata($data);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_insert($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
Model:
function profile_insert($datanew)
{
$this->db->insert('profile', $datanew);
}
I get 5 rows if I submit the HTML form.

This works for me:
I Set the userid to unique and made a if statement in the controller ti switch between a insert and update function.
Controller:
function update()
{
$datanew = array(
'userid' => $this->session->userdata("userid"),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'bio' => $this->input->post('bio') ,
'skills' => $this->input->post('skills')
);
$exists = $this->db->select('profile')->where('userid', $this->session->userdata("userid"));
if($exists)
{
$this->session->set_userdata($datanew);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_update($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
else
{
$this->session->set_userdata($datanew);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_insert($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
}
model:
function profile_insert($datanew)
{
$this->db->insert('profile', $datanew);
}
function profile_update($datanew)
{
$this->db->update('profile', $datanew);
}
Thanks for helping me

Related

how to insert these data into mysql in wampserver

here are is the code.no matter what i did couldnt connect to mysql database in phpadmin in the wampserver all attempts has failed please help to solve
if (isset($_POST['continue'])) {
$j=0;
while ($j < $passengers)
{
$register_data2 = array(
'first_name' => $_POST["fname"][$j],
'last_name' => $_POST["lname"][$j],
'passport' => $_POST["passport"][$j],
'visa' => $_POST["visa"][$j],
'address1' => $_POST["address1"][$j],
'address2' => $_POST["address2"][$j],
'email' => $_POST["email"][$j],
'contact' => $_POST["contact"][$j],
'pin' => $_POST["pin"][$j],
'leaving_from' => $pieces[0],
'going_to' => $pieces[2],
'depart_date' => $pieces[7],
'depart_time' => $pieces[12],
'arrival_time' => $pieces[17],
'grand_fare' => $pieces[22],
'returning_from' => $pieces1[0],
'returning_to' => $pieces1[2],
'returning_date' => $pieces1[7],
'returning_time' => $pieces1[11],
'reaching_time' => $pieces1[16],
'fare' => $pieces1[21]
);
session_start();
$_SESSION['ticket'][] = $_POST["fname"][$j];
$_SESSION['ticket'][] = $_POST["lname"][$j];
$_SESSION['ticket'][] = $_POST["passport"][$j];
$_SESSION['ticket'][] = $_POST["visa"][$j];
$_SESSION['ticket'][] = $_POST["pin"][$j];
register_passenger($register_data2);
$j = $j+1;
}
$_SESSION['ticket1'] = $pieces[0];
$_SESSION['ticket2'] = $pieces[2];
$_SESSION['ticket3'] = $pieces[7];
$_SESSION['ticket4'] = $pieces[12];
$_SESSION['ticket5'] = $pieces[17];
$_SESSION['ticket6'] = $pieces[22];
$_SESSION['ticket11'] = $pieces1[0];
$_SESSION['ticket22'] = $pieces1[2];
$_SESSION['ticket33'] = $pieces1[7];
$_SESSION['ticket44'] = $pieces1[11];
$_SESSION['ticket55'] = $pieces1[16];
$_SESSION['ticket66'] = $pieces1[21];
}
?>
<?php
if (isset($_POST['pay'])){
if ($_POST['cash'] != $grand_total) {
echo "*Pay the given amount!"."<br>";
}
else{
header ('Location: ticket.php');
}
}
?>
<h2> Select payment method </h2>
<form action="payment.php" method="post">
<input type="radio" name="payment" id="cash" checked="checked" value="cash">
<label for="cash">Cash</label>
<input type="number" id="cash" name="cash" size="8"><br><br>
<input type="radio" name="payment" id="card" value="card">
<label for="card">Card</label>
<select>
<option>Debit card</option>
<option>Credit card</option>
</select>
<br>
<img src="Credit.jpg">
<br>
<input type="submit" name="pay" value="Make payment">
</form>
<?php
if(isset($_POST["Continue"])){
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$passport = $_POST['passport'];
$visa = $_POST['visa'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$pin = $_POST['pin'];
mysql_query( "INSERT INTO passengers (first_name,last_name,passport,visa,address1,address2,email,contact,pin) VALUES('$lastname','$passport','passport','$visa','$address1','$address2','$email','$contact','$pin')");
}
?>
i couldnt connect the data to mysql database in the phpadmin in the wampserver. it shows up nothing. as you can see below one of my many attempt to correct to make it work it up as fail. non of the method that i tried didnt work. please help to sove this problem
It seems you messed up the values list:
mysql_query( "INSERT INTO passengers
(first_name,last_name,passport,visa,address1,address2,email,contact,pin)
VALUES
('$lastname','$passport','passport','$visa','$address1','$address2','$email','$contact','$pin')");
First name is missing, 'passport' should not be there, etc. It should be:
('$firstname','$lastname','$passport','$visa','$address1','$address2','$email','$contact','$pin')");

checkboxlist not rendering checkbox checked correctly

I have a two array that I'm going to include them into a checkbox,
echo Html::checkboxList('item', $selectedItem, $dataItem, [
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='col-md-2'>
<input type='checkbox' {$checked} name='{$name}' value='{$value}'>
<span>{$label}</span>
</label>";
}
]);
I see in code in browser like this :
<label class="col-md-2">
<input 1="" name="item[]" value="1" disabled="" type="checkbox">
<span>Login</span>
</label>
The checked render into 1="" .
Please advise.
UPDATE
$dataItem
E:\wamp64\www\yii_tresnamuda\modules\it\views\request\preview.php:128:
array (size=6)
1 => string 'Login' (length=5)
2 => string 'Printer' (length=7)
3 => string 'Monitor' (length=7)
4 => string 'Computer' (length=8)
5 => string 'Network' (length=7)
6 => string 'Lain Lain' (length=9)
$selectedItem
E:\wamp64\www\yii_tresnamuda\modules\it\views\request\preview.php:129:
array (size=2)
2 => int 2
1 => int 1
The problem is in your {$checked} element of your item template. Try this:
echo Html::checkboxList('item', $selectedItem, $dataItem, [
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='col-md-2'>
<input type='checkbox' name='{$name}' value='{$value}' ".($checked ? 'checked' : '').">
<span>{$label}</span>
</label>";
}
]);
Also, I think you have to change $selectedItem specification form [2=>2, 1=>1] to just [2,1]

data is not inserting into database in codeigniter

actually i am having student records in $students array and there is another array within $students, whose name is skill[], which is a checkbox form field name, so pls tell me how to use json_encode and where.
input form
<td>ENTER SKILLS</td>
<td>
<input type="checkbox" name="skills[]" value="php">php<br>
<input type="checkbox" name="skills[]" value="dotnet">dotnet<br>
<input type="checkbox" name="skills[]" value="java">java<br>
<input type="checkbox" name="skills[]" value="ruby_on_rails">ruby_on_rails<br>
</td>
controller
<?php
public function insert(){
if ($this->input->post('add')==true)
{
$student = array( 'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'skills' => $this->input->post(json_encode(skills)),
'notes' => $this->input->post('notes'),
'gender' => $this->input->post('gender') );
$result = $this->Student_info_model->insertStudent($student);
if($result==true){
echo "inserted";
}
else {
echo "Not Inserted";
}
}
}
?>
model
function insertStudent($student){
$this->db->insert('student_info_table', $student); // insert data into "student_info_table" table`
if ($this->db->affected_rows() > 0) {
return true;
}
else {
return false;
}
}
error
Error Number: 1048
Column 'skills' cannot be null
INSERT INTO `student_info_table` (`name`, `email`, `skills`, `notes`, `gender`) VALUES ('gailyn', 'quentin#gmail.com', NULL, 'dsas', 'male')
Filename: C:/xampp/htdocs/codeigniter/system/database/DB_driver.php
Line Number: 691
As far as i understand you want to get the array of skills from the http request and then to encode it and save it to your database. For that please use json_encode($this->input->post('skills') instead of $this->input->post(json_encode(skills)), so you first get the data, and then apply the json encoding over it.
Try with this : json_encode($this->input->post('skills')) You need to get the value of skills using input post and then need to convert it using json_encode

How to remove 'label' decorator from Zend 2 form

I have the simple Radio element:
$form->add([
'name' => 'account_type',
'type' => 'Zend\Form\Element\Radio',
'options' => [
'label' => 'Account type',
'value_options' => [
1 => 'Employer',
2 => 'Performer'
]
]
]
);
But in my view I get this html:
<div class="zf-form-el-account_type">
<label for="account_type">Account type</label>
<div class="zf-radio">
<label>
<input type="radio" name="account_type" class="account_type" value="1">Employer
</label>
</div>
<div class="zf-radio">
<label>
<input type="radio" name="account_type" class="account_type" value="2">Performer
</label>
</div>
</div>
How I can to remove this empty label wrapper around an radio element? Or how I can to insert some tag after radio element? Thanks.
I extended standard view helper:
<?php
namespace Application\Form\View\Helper;
use Zend\Form\View\Helper\FormRadio;
use Zend\Form\Element\Radio as RadioEl;
class FormRadioElement extends FormRadio
{
protected function renderOptions(RadioEl $element, array $options, array $selectedOptions, array $attributes)
{ ...
... and set template in helper like:
$template = '%s%s';
Then I declared it in my bootstrap:
public function getViewHelperConfig() {
return [
'invokables' => [
'formRadioHelper' => 'Application\Form\View\Helper\FormRadioElement',
]
];
}
... and called in my view like:
<?php echo $this->formRadioHelper($form->get('account_type'))?>

How to display cakephp 2.5.3 model validation message.By default it shows html 5 validation message

How to display the validation message which is defined in CakePHP model.When I submit the form , CakePHP model validation works.But validation message for input field is not displayed. It shows HTML 5 validation Message. I want to display CakePHP model validation message. I am using CakePHP 2.5.3
Following is my code block:
Controller:
$this->User->set($this->request->data);
if ($this->User->validates()) {
if ($this->User->save($this->request->data)) {
$Email->send();
$this->Session->setFlash(__('The user has been created'));
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
}
} else {
$this->Session->setFlash(__('The user could not be created. Please, try again.'));
}
}
Model:
public $validate = array(
'username' => array(
'nonEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required',
'allowEmpty' => false
),
'unique' => array(
'rule' => array('isUniqueUsername'),
'message' => 'This username is already in use'
),
'alphaNumericDashUnderscore' => array(
'rule' => array('alphaNumericDashUnderscore'),
'message' => 'Username can only be letters, numbers and underscores'
),
)
View:
<div id="divfirstname" class="input text required">
<label for="UserFirstname">First Name <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('firstname');?>
</div>
<div id="divlastname" class="input text required">
<label for="UserLastname">Last Name <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('lastname');?>
</div>
<div class="input text required">
<label for="UserUsername">Username <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('username');?>
</div>
You don't need to wrap the validation-block around your save call.
So this is enough:
if ($this->User->save($this->request->data)) {
$Email->send();
$this->Session->setFlash(__('The user has been created'));
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
}
} else {
$this->Session->setFlash(__('The user could not be created. Please, try again.'));
}
CakePHP handles automatically the validation when Model->save() is called and displays the error under your input fields. So if $this->save fails because of a validation error, cakephp adds <div class="error-message">Message Text from your model validation array</div> under the right input field.
You are left with 2 options :
Either dont use validation rule in cakePHP which validates on Client side.
On DOM ready , call $("#formId").removeAttr('novalidate'); Like
$(document).ready(function(){
$("#formId").removeAttr('novalidate');
});