how to store multiple checkbox data in database - 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.

Related

Display success update message, after saving data from a form to MySql database

I have a form to edit product_Details, after I press save button it run updateProduct.php which is update product_detail table successfully, the problem is : success message displayed on a new page, I just want it to be displayed beside save button, please advice and I thank you very much
here is code of my form:
<form action = "updateproduct.php">
<!-- \\\\\\\\\\\\\\\\\\\START Product Name\\\\\\\\\\\\\\\\\\\\ -->
<div class="mb-3 mt-3">
<label for="product_id" class="form-label">Product Name</label>
<?php $strquery2="SELECT Name, ID FROM products WHERE ID='". htmlspecialchars($row['product_id'])."'";
$result2 = mysqli_query($conn, $strquery2);
$row2 = mysqli_fetch_array($result2)
?>
<input type="text-area" class="form-control" name="productname" value= <?= htmlspecialchars($row2['Name']) ?>>
<input type="text" class="form-control" name="ID" value= <?= htmlspecialchars($row2['ID']) ?> hidden>
</div>
<!-- \\\\\\\\\\\\\\\\\\\END Product Name\\\\\\\\\\\\\\\\\\\\ -->
<div class="mb-3 mt-3">
<label for="discription" class="form-label">Discription</label>
<input type="text" class="form-control" name="discription" value= "<?= htmlspecialchars($row['discription']) ?>">
</div>
<input type="submit" value="Save">
</form>
Bellow is my updateproduct.php file
<?php $mysqli = new mysqli("localhost", "root", "", "test")
or die('Error connecting to MySQL server.');
$id=mysqli_real_escape_string($mysqli, $_REQUEST['ID']);
$comments=mysqli_real_escape_string($mysqli, $_REQUEST['discription']);
$query= "UPDATE product_details SET
discription = '$comments'
WHERE product_id='$id'";
if(mysqli_query($mysqli, $query)){
echo '<script>alert("Product added successfully")</script>';
}
else{
echo "ERROR: Hush! Sorry $sql. "
. mysqli_error($conn);
}
// Close connection
mysqli_close($mysqli);
?>

Why doesn't the submitted form submit to database WP?

I am trying to submit data, retrieved through a custom form, into the post type 'posts'.
How ever it isn't working. It var_dumps, but it when I go to 'posts' I can see it hasn't been uploaded.
'Posts' is not custom, I also tried doing it through a custom post type which I registered, But it also didn't work.
My formHandler.php:
if(isset($_POST['submit'])){
$berichtnaam = $_POST['berichtnaam'];
$categories = array('kat', 'hond', 'slang');
$categorie = $_POST['categorie'];
$foto = $_FILES['foto'];
$bericht = $_POST['bericht'];
$post_content = array($categorie, $bericht);
var_dump($berichtnaam);
var_dump($foto);
var_dump($post_content);
$blog_post = (array(
'post_status' => 'publish',
'post_type' => 'post',
'post_title' => wp_strip_all_tags($berichtnaam),
'post_content' => $post_content
) );
wp_insert_post($blog_post);
}
My form:
<form action="<?php echo get_template_directory_uri()?>/formHandler.php" method="post" enctype="multipart/form-data">
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Berichtnaam</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Geen titel" name="berichtnaam">
</div>
<label for="select-field">Categorie</label>
<select class="form-select form-control" id="select-field" aria-label="Default select example" name="categorie">
<option selected>Geen categorie</option>
<option value="kat">Kat</option>
<option value="hond">Hond</option>
<option value="slang">Slang</option>
</select>
<div class="mb-3" id="file_field">
<label for="uploadFoto" id="input_file">
<i class="fi fi-rr-camera"></i><span id="upload_btn">Kies bestand</span>
</label>
<input type="file" class="form-control" id="uploadFoto" onchange="showFileName()" name="foto">
<p id="fileSelected"></p>
<p id="fileUnSelected"></p>
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Bericht</label><br>
<textarea class="form-control" id="formGroupExampleInput2" name="bericht"></textarea>
</div>
<button id="card-L" type="submit" name="submit" value="submit">Bericht aanmaken</button>
</form>
I've tried it like this, which was answered in a similar question:
$blog_post = wp_insert_post(array(
'post_status' => 'publish',
'post_type' => 'post',
'post_title' => wp_strip_all_tags($berichtnaam),
'post_content' => $post_content
) );
$post_type = 'blogpost_vero';
$query = "UPDATE {$wpdb->prefix}posts SET post_type='".$post_type."' WHERE id='".$blog_post."' LIMIT 1";
GLOBAL $wpdb;
$wpdb->query($query);
Note: I am not yet submitting the $_FILES because I had to figure this out yet.
Eventually I want to upload what is submitted through the form and then display this on the home page and blog archive page, inside of cards with a thumbnail. This is not going online, this is just as an exercise for (hopefully) a new workplace
Any help is appreciated greatly! Thanks so much in advance.
I suspect you should do a little more error checking.
Try this:
$post_id = wp_insert_post( $blog_post, true);
if ( is_wp_error( $post_id ) ) {
print_r ( $post_id );
} else {
/* post_id has the ID of the new post */
}
This should help you determine what happened to your post.

Laravel : add multiple selected boxes on my cart

I'm building an backoffice using Laravel and i have an issue with my form.
I made a form to add one or many products with quantity wanted.
The problem is i dont figure out how to add multiples products with their quantity in my database.
Here is my code :
index.blade.php:
<label class="col-4">Produits</label>
<div class="col-8">
#foreach ($nom_produits as $nom_produit)
<div class="form-check">
<input class="form-check-input" type="checkbox" value="{{ $nom_produit -> id }}" id="flexCheckDefault" name="produit[]">
<label class="form-check-label" for="flexCheckDefault">
{{ $nom_produit -> nom_produit }}
<div class="form-group col-md-4">
<input type="number" class="form-control" name="nombre[]" value="">
</div>
</label>
</div>
#endforeach
controller.php :
public function store(Request $request){
$table_commande = new Commandes();
// Récupération donnée Commandes via formulaire :
$table_commande->commercial_id=$request->select_commercial;
$table_commande->client_id=$request->select_client;
//$table_commande->save();
// Récupération données DétailsCommande via formulaire:
$table_detail = new DetailCommande();
$table_detail->commande_id = $table_commande->id;
$table_detail->produit_id = $request->produit;
$table_detail->quantite = $request->nombre;
dd($request);
}
I have all my data when i try the dd function.
But how do i manage to insert the right quantity to the right products ?
Thanks for any tips !

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>

How to pass data from view to controller in cakephp?

I have a view like that:
What I want is when I click on the button, it will execute a select query:
SELECT discounts.product_id, products.product_name,
sum(products.product_price - discounts.product_discount) as total_Amount,
count(orders.order_id) as total_Number
FROM products
inner join discounts on products.product_id = discounts.product_id
inner join orders on discounts.discount_id = orders.discount_id
where discounts.start_time >= **FromTextBox** and discounts.end_time <= **ToTextBox**
group by discounts.product_id,products.product_name
FromTextBox and ToTextBox are values from 2 textboxes.
This is in my controller:
....
$option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
//$option['conditions']=array('Discount.start_time >'=>array('')); //where I put values from view
$option['group'] = array('Discount.product_id','Product.product_name');
$products = $this->Order->find('all',$option);
$this->set('products',$products);
And my view:
<label class="control-label">From</label>
<div class="controls input-append date" id="dp1" data-date="" data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
<input size="16" type="text" value="" readonly> <-- where I put textbox
<span class="add-on"><i class="icon-remove"></i></span>
<span class="add-on"><i class="icon-th"></i></span>
</div>
<label class="control-label">To</label>
<div class="controls input-append date" id="dp2" data-date="" data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
<input size="16" type="text" value="" readonly> <-- where I put textbox
<span class="add-on"><i class="icon-remove"></i></span>
<span class="add-on"><i class="icon-th"></i></span>
</div>
<div>
<?php echo $this->Form->button('A Button'); ?>
</div>
Two textboxes are used Bootstrap datepicker.
Please help me! thanks in advance.
You should use "Ajax" to send what is selected in "datepicker" to server, execute your query and use the result to create your table. There is no direct way in cakephp for your problem

Resources