how to create batch insert in yii2 - yii2

how to create batch insert
view
two input number check and tow input price check and tow input date check
<input type='text' name="number_check[]">
<input type='text' name="price_check[]">
<input type='text' name="date_check[]">
<input type='text' name="number_check[]">
<input type='text' name="price_check[]">
<input type='text' name="date_check[]">
controller
I don't know what to write ?????
Yii::$app->db->createCommand()->batchInsert('sale_check',[
'number_check',
'price_check',
'date_check',
'user_id',
'sale_id',
],$data)->execute() ;

Looking to you sample where you have just two set of same fields you shoul populate the $data array and perform execute() of db command
$post = Yii::$app->request->post();
for ($i=0; $i<2; $i++){
$data[$i][0] = $post[number_check][$i];
$data[$i][1] = $post[price_check][$i];
$data[$i][2] = $post[date_check][$i];
$data[$i][3] = Your_value_for_user_id;
$data[$i][4] = Your_value_for_sale;
}
Yii::$app->db->createCommand()->batchInsert('sale_check',[
'number_check',
'price_check',
'date_check',
],
$data
)->execute();

Related

Wordpress. Insert form values to custom table

How can i on click on submit send values to my custom mysql table??
Form html:
<form action="/ekz.php" method="post">
<input type="text" name="jjkk1">
<input type="text" name="jjkk2">
<input type="text" name="jjkk3">
<input type="text" name="jjkk4">
<input type="submit" name="submit">
</form>
Table name is wp_ekz2020:
Try doing like this :
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$a=$_POST['jjkk1'];
$b=$_POST['jjkk2'];
$c=$_POST['jjkk3'];
$d=$_POST['jjkk4'];
$wpdb->insert( 'wp_ekz2020', array( 'num1' => $a, 'num2' => $b,'num3' => $c,
'num4' => $d), array( '%s', '%s','%s', '%s' ) );
}
?>
please try to place both html and php in single file
here is code i have try and it works
<form action="<?php the_permalink(); ?>" method="post">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="text" name="num3">
<input type="text" name="num4">
<input type="submit" name="submit">
</form>
if(isset($_POST['submit']))
{
function insertnumber(){
global $wpdb;
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$num4 = $_POST['num4'];
$table_name = "newtab";
$wpdb->insert( $table_name, array(
'num1' => $num1,
'num2' => $num2,
'num3' => $num3,
'num4' => $num4
) );
}
insertnumber();
}
put <?php the_permalink(); ?> in your form action
i hope above code will help you : )
change table name and also variable
in this case, whenever you submit the form with action attribute then the page gets reloaded, and '$wpdb' returns null. So this will cause error.
To prevent this you have to include 'wp-load.php' in your file or you can use
add_action('init', 'your_function_name')

Using if(isset($_POST[''])) method to see if the user enter the data so in will store them , and if not so will not save when i refresh the page

i want to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page!!
i've tried to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page but doesnt work ! and when i refresh the page it store the last data that i've tried before!!
this is index.php :
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
this is the users.php file :
<?php
class Users extends DB {
function insertUser($name,$age,$email){
$query = "INSERT INTO users (name, age, email )
VALUES ('$name', '$age', '$email')";
$res=$this->insert($query);
//return $this->select("SELECT * FROM `users`");
}
}
i've tried to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page but doesnt work ! and when i refresh the page it store the last data that i've tried before!!
Please help!!
it show this error and store the data at the same time !
Notice: Undefined index: name in C:\xampp\htdocs\gestion de paie\index.php on line 10
Notice: Undefined index: age in C:\xampp\htdocs\gestion de paie\index.php on line 10
Notice: Undefined index: email in C:\xampp\htdocs\gestion de paie\index.php on line 10
You can put a condition before you create user, in index file
index.php
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
if(isset($_POST['name']) && isset($_POST['age']) && isset($_POST['email']){
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
}
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
Also don't put user inputs inside a query without sanitize, i suggest always to controll user inputs before you enter in you sql query:
users.php :
<?php
class Users extends DB {
function insertUser($name,$age,$email){
// Sanitize your data always don't trust user
$name = mysql_real_escape_string($name);
$age = mysql_real_escape_string($age);
$email = mysql_real_escape_string($email);
$query = "INSERT INTO users (name, age, email )
VALUES ('$name', '$age', '$email')";
$res=$this->insert($query); //return $this->select("SELECT * FROM `users`");
Hope to be the needed question :)
Instead of isset, you should use !empty (look here), as isset doesn't work for arrays. isset($_POST["anything"]) always returns true, while !empty($_POST["anything"]) only returns true if $_POST["anything"] exists.
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
if (!empty($_POST['name']) && !empty($_POST['age']) && !empty($_POST['email']) {
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
}
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
Also, you should use Prepared statements.

Update multiple rows with same id using foreach loop

I echo 2 input-fields (2 db columns) foreach row in the database (where id=$id) with each row's data being inside the input field. Admin-user can change data inside input-field and click update.
Currently, when clicking the update-button, it does update the rows (where id=$id) but it updates each row with the input-data of the first input field instead of updating input-data foreach input field. Thank you in advance for help.
view:
<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
<?php foreach($link_data as $row) : ?>
Link:<br />
<input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
<input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
<?php endforeach; ?>
<input type='submit' name='update_site' value='Update'>
</form>
controller:
public function do_edit_page(){
$id = $this->input->post('page_id', TRUE);
$this->content_model->update_links($id);
}
model:
public function update_links($id){
foreach($_POST as $update_rows){
$update_rows = array(
array(
'page_id' => $id,
'link_title' => $this->input->post('page_link_title', TRUE),
'link' => $this->input->post('page_link_sub_title', TRUE)
)
);
$this->db->update_batch('content_links', $update_rows, 'page_id');
}
}
you have to move your form tag between the foreach loop so that it can submit the data of single row. something like this
<?php foreach($link_data as $row) : ?>
<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
Link:<br />
<input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
<input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
<input type='submit' name='update_site' value='Update'>
</form>
<?php endforeach; ?>

Passing checkbox values to mysql database using Codeigniter

I'm using CodeIgniter and mySQL to build a checkbox form. The form contains 4 options; each option has only one checkbox; users can select any combination of the options. I want to do the following:
1 - For each checkbox, use a value of 1 (if unchecked) or 2 (if checked) and pass those values through to the database (each checkbox has its own field). Right now, whether checked or unchecked, the checkboxes are sending a value of 0 through to the database.
2 - Once users update their checkboxes, I'd like to update the database to reflect the new values. Right now, a new row is added for each update to the checkboxes.
What I've got so far is a form that submits the checkbox values to the database, a controller, and a model):
Form
<?php echo form_open('addFoo'); ?>
<input type="checkbox" name="foo1" value="" />
<input type="checkbox" name="foo2" value="" />
<input type="checkbox" name="foo3" value="" />
<input type="checkbox" name="foo4" value="" />
<?php echo form_submit('submit', 'Save Changes'); ?>
<?php echo form_close(); ?>
Controller
function addFoo()
{
if ($this->input->post('submit')) {
$id = $this->input->post('id');
$foo1 = $this->input->post('foo1');
$foo2 = $this->input->post('foo2');
$foo3 = $this->input->post('foo3');
$foo4 = $this->input->post ('foo4');
$this->load->model('foo_model');
$this->foo_model->addFoo($id, $foo1, $foo2, $foo3, $foo4);
}
}
Model
function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
$data = array(
'id' => $id,
'foo1' => $foo1,
'foo2' => $foo2,
'foo3' => $foo3,
'foo4' => $foo4
);
$this->db->insert('foo_table', $data);
}
At your Controller :
if you want to insert new entry for all selected checkbox:
foreach($this->input->post('foo') as $r)
{
$data['fieldname']=$r;
$this->model_name->insert($data);
}
if you want to insert all selected checkbox values in different fields within single entry then,
foreach($this->input->post('foo') as $key=>$val)
{
$data['field'.$key]=$val;
}
$this->model_name->insert($data);
Well in reference to setting the values, a checkbox doesn't send anything if unchecked. To achieve what you want, you have to do this:
<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="2" />
This will send a value regardless of whether the checkbox is checked or not.
use the different values for each checkbox and get the value of checkbox array and use this in your controller print_r($this->input->post('foo')); it will print the values that are selected by user
use like this
<?php
if (isset($_POST['submit'])){
print_r($_POST['foo']);
}
?>
<form action="" method="POST">
<input type="checkbox" name="foo[]" value="1">1<br>
<input type="checkbox" name="foo[]" value="2">2<br>
<input type="checkbox" name="foo[]" value="3">3<br>
<input type="checkbox" name="foo[]" value="4">4<br>
<input type="submit" value="submit" name="submit">
</form>

Parsing Just a specific HTML tag by name or ID in Perl?

This is something thats been hard for me to find info on, I was fortunate to get an answer on the other thing I was trying to do that relates to this (code below).. so say I am using the $content input (in reality it would be a full HTML page, not just the snippet I gave below) and I want to just get the contents of the input tag that has the name or ID "hush_username". The way it is now it gives the content of all of the input tags.. the only thing that I could find on this mentioned something like incorporating this:
$tag->[1]{name} and $tag->[1]{name} eq "hush_username" ;
But I have been unable to get this to work.. I would greatly appreciate any advice
#!/usr/bin/perl
use strict; use warnings;
use HTML::TokeParser::Simple;
$content = do { local $/; <DATA> };
my $parser = HTML::TokeParser::Simple->new(\$content);
while ( my $tag = $parser->get_tag('input') ) {
print $tag->as_is, "\n";
print "####" ;
for my $attr ( qw( type name value ) ) {
printf qq{%s="%s"\n}, $attr, $tag->get_attr($attr);
}
}
__DATA__
<form name="authenticationform" id="authenticationform"
action="/authentication/login?skin=mobile&next_webapp_name=hushmail5&next_webapp_url_name=m" method="post">
<input type="hidden" name="next_webapp_page" value=""/>
<p><label for="hush_username">Email address:</label><br/>
<input type="email" name="hush_username" id="hush_username" value="email#test.com"/></p>
<p><label for="hush_passphrase">Passphrase:</label><br/>
<input type="password" name="hush_passphrase" id="hush_passphrase" maxlength="1000" value=""/></p>
<p><input type="checkbox" name="hush_remember_me" id="hush_remember_me" value="on"
/><label for="hush_remember_me">Stay signed in when I close my browser</label></p>
<p><input type="submit" value="Sign In"/></p>
<input type="hidden" name="hush_customerid" value="0000000000000000"/>
</form>
Keep it simple. How about this?
while ( my $tag = $parser->get_tag('input') ) {
my $name = $tag->get_attr('name');
next unless defined $name and $name eq 'hush_username';
print "Value: ", $tag->get_attr('value'), "\n";
}