Issue with Check box update or Edit to database in Codeigniter - database

My function is when I will check then 1 will insert and if not then by default the field will fill with 0.At the time of insert problem is not happen, it store '1' to db but at the time of update with check box it not able to send '1' again to db, it sending 0.
AT THE INSERT TIME
View
.
<?php echo form_checkbox("feature_opd","1", set_checkbox("feature_opd","1")); ?>
<?php echo form_checkbox("feature_gyane","1", set_checkbox("feature_gyane","1")); ?>
<?php echo form_checkbox("feature_iui","1", set_checkbox("feature_iui","1")); ?>
Model
public function add_article($array)
{
return $this->db->insert('table_name',$array);
}
AT THE UPDATE TIME
View
<div>
<?php
if($article->feature_opd == 1)
{?>
<input type="checkbox" name="feature_opd" checked >
<?php
}
else
{?>
<input type="checkbox" name="feature_opd" >
<?php
}
?>
</div>
<div>
<?php
if($article->feature_gyane == 1)
{?>
<input type="checkbox" name="feature_gyane" checked>
<?php
}
else
{?>
<input type="checkbox" name="feature_gyane" >
<?php
}
?>
</div>
<div>
<?php
if($article->feature_iui == 1)
{?>
<input type="checkbox" name="feature_iui" checked>
<?php
}
else
{?>
<input type="checkbox" name="feature_iui" >
<?php
}
?>
</div>
Model
public function update_article($article_id , array $article)
{
return $this->db
->where('id',$article_id)
->update('ivf_productsettings', $article );
}
My Query is how I will update db with check box. I am facing problem to update with check box in Codeigniter, Please suggest me , if my code going to tough then please suggest me in simple way how to update with check box in codeigniter .I am new in ci .

You can create your update checkboxes like so:
<?php echo form_checkbox("feature_iui","1", set_checkbox("feature_iui","1", $article->feature_iui)); ?>
The default value will apply from the db.
Aside from that there is nothing aside from user input preventing the checkboxes from being properly sent. Thus, any code that is causing a 0 being put in the db is probably due to how you are processing the checboxes in your controller update function. As I've said in the comments, checkboxes only submit 1's never 0's.

Related

Dropdown is empty when using foreach from DB

My program brings a list of a teacher from a table.
Then I want to show the names in the drop-down but for some reason, it opens an empty dropdown with the number of empty lines like the records in the table.
This is my code for this problem
<div>
<label for="teacher_name"> שם המורה: </label>
<select name="teacher_name" id="teacher_name">
<?php if (empty($teachers)): ?>
<p> No teachers found</p>
<?php else: ?>
<?php if($status == 'new'): ?>
<option value="please_select">נא לבחור</option>
<?php foreach ($teachers as $teacher): ?>
<option value=<?=$teacher['teacher_name']; ?> name="teacher_name" id="teacher_name"></option>
<?php endforeach; ?>
<?php endif; ?>
<?php if($status == 'edit'): ?>
<p> edit</p>
<input type="text" name="teacher_name" id="teacher_name" value="<?= htmlspecialchars($lesson->teacher_name); ?>" >
<?php endif; ?>
<?php endif; ?>
</select>
</div>
If you "view source" of your output I think you will see that the option tags do have data in them - but you haven't set the value to display, it should be more like <option ....>Show this in the list</option>.
Try this (the same code as you were using, but adding the teacher name variable again where it will display)
<option value=<?=$teacher['teacher_name']; ?> name="teacher_name" id="teacher_name"><?=$teacher['teacher_name']; ?></option>

how to store multiple checkbox data in database

i am trying to insert multiple checkbox data in my database but having a little bit problem. When i am trying to insert checkbox data it inserts only id and nothing else.
Here is my full code:-
<?php
include("config.php");
if(isset($_POST['save'])){
$contact_sms = $_POST['contact_sms'];
$check[] = $_POST['check'];
$firm_name = $_POST['firm_name'];
var_dump($firm_name);
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++){
$bulksms="INSERT INTO bulk_sms (sent_sms,firmname_sms,contact_sms) VALUES('".$checkbox[$i]. "','".$row['firm_name']. "','$contact_sms')";
$smsquery=mysqli_query($conn, $bulksms);
}
}
$sql="SELECT * FROM inventory_details where status ='0' AND role='0' ORDER BY position ASC, role ASC, visiter DESC limit 0,100";
$query=mysqli_query($conn, $sql);
?>
<form method="post" action="" id="msform">
<?php
$i=0;
while($row=mysqli_fetch_assoc($query)){
$id = $row['id'];
$firm_name = $row['firm_name'];
?>
<div class="col-md-4">
<input type="text" class="form-control" name="id_sms" value="<?php echo $id;?>">
</div>
<div class="col-md-6">
<input type="text" class="form-control" name="firm_name[]" value="<?php echo $firm_name;?>">
</div>
<div class="col-md-2">
<input type="checkbox" id="checkItem" name="check[]" value="<?php echo "$id"; ?>">
</div>
<?php
$i++;
}
?>
<input type="text" class="form-control" name="contact_sms" placeholder="Contact Number..">
<button type="submit" class="btn btn-success" name="save">Send</button>
</form>
when i click on Send button , it only stores idinside the field sent_sms and nothing in field firm_name. Please help me out. I am poorly trapped in it.
Updated code:
if(isset($_POST['save'])){
$contact_sms = $_POST['contact_sms'];
$check[] = $_POST['check'];
$firm_name = $_POST['firm_name'];
var_dump($_POST);
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++){
$bulksms="INSERT INTO bulk_sms (sent_sms,firmname_sms,contact_sms) VALUES('".$checkbox[$i]. "','".$firm_name[$i]. "','$contact_sms')";
$smsquery=mysqli_query($conn, $bulksms);
}
}
I think $row['firm_name'] is not defined, try changing this line:
$bulksms="INSERT INTO bulk_sms (sent_sms,firmname_sms,contact_sms) VALUES('".$checkbox[$i]. "','".$row['firm_name']. "','$contact_sms')";
to this:
$bulksms="INSERT INTO bulk_sms (sent_sms,firmname_sms,contact_sms) VALUES('".$checkbox[$i]. "','".$firm_name. "','$contact_sms')";
Also it should be noted that you have not sanitized the inputs, leaving your site vulnerable to sql injection. You should always sanitize user input, and it is highly recommended to use prepared statements when inserting into the database.

Retrieve WordPress Database Info and display in form

Sorry I am new to WordPress and still having difficulty to retrieve my data from my custom database. So far this is my code for submitting data to my wp_datareg table.
<?php
/*
Template Name: Data-Register
*/
get_header();
?>
<?php
If($_POST['Submit']) {
global $wpdb;
$Data1=$_POST['Data1'];
$Data2=$_POST['Data2'];
if($wpdb->insert(
'wp_datareg',
array(
'Data1' => $Data1,
'Data2' => $Data2
)
) == false) wp_die('Database Insertion Failed');
else echo '<h2>Database Insertion Succesful</h2><p />';
?>
<?php
}
else // else we didn't submit the form so display the form
{
?>
<
<h4>Data Registration Form</h4>
<form action="" method="post" id="addcourse">
<p><label>Put Data1:<input type="text" name="Data1" size="30" /></label></p>
<p><label>Put Data2: <input type="text" name="Data2" size="30" /></label></p>
</div>
<input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
</form>
What I want to add is put another text form and search button where user can search a data of Data1 and edit its value on the form, (please see the image)
Sorry for being such a noob.
PLEASE SEE THIS IMAGE
Thank you.
br
We can fetch data from wp_datareg table using this code, however putting back the data to the form is my problem.
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_datareg" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->Data1;?></td>
</tr>

CakePHP field has fieldname in as a value

On a page I have this form:
<form action="/store_collections/create" method="post">
<input type="text" name="data[test]" placeholder="Create new collection" class="span2">
</form>
I submit the form which sends it to the StoreCollectionsController:
<?php
App::uses('AppController', 'Controller');
class StoreCollectionsController extends AppController {
public function create() {
print_r($this->request->data);
}
The print_r returns this:
Array
(
[test] => fdsdata[test]=fds
)
How is this possible?? The field's name is appended to the value. The value should be fds but it's fdsdata[test]=fds.
Any ideas? I removed everything from the controller ($uses...) to see what it could be. All the other forms on the site is fine. Just the data submitted to this controller.
The input control Name property in your example above is missing the name of the model. Are you using the form helper to create the form on the view? Try this in your view:
<?php echo $this->Form->create('StoreCollection'); ?>
<?php echo $this->Form->input('test'); ?>
<?php echo $this->Form->end('submit'); ?>

Getting Cake PHP to output a HTML submit button with a given class name

I can get Cake to output a submit button using the following PHP:
<?php echo $this->Form->end(__('Submit')); ?>
which outputs this HTML:
<input type="submit" value="Submit">
but I want to use a specific input class to get the following:
<input type="submit" value="Submit" class="some class">
is this possible?
Thank you :).
Yes it's possible :
<?php
echo $this->Form->submit(__('Submit',true), array('class'=>'some class'));
echo $this->Form->end();
?>
Which is documented here.
This should do the trick:
<?php echo $this->Form->end(array('label' => __('Submit', true), 'class' => 'some class')); ?>

Resources