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

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.

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);
?>

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 !

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.

how to upload the File to some location in cakephp

i am trying to use the File upload feature in my application ..
i am having a Form with many fields of type Text,textarea and including File upload FIeld.
I have kept a Submit button at the end of the Form which on click will submit the actual value of the textbox /textarea and even the value for the Field of type File upload.
How to get the actual file that is uploaded and to save it in a location so that i can view the uploaded file later on ..
The code that i have used is,
Edit :
i have added enctype in the Form tag but not working while submission
<form method="post" action="/FormBuilder/index.php/forms/submit/93/13" id="ResultSubmit" enctype="multipart/form-data">
<div class="input file">
<label for="276">Choose Ur File To Upload</label>
<input type="file" value="" style="width: 400px;" id="276" name="Choose ur file to upload"/>
</div><br/>
<div class="input text">
<label for="277">Name</label>
<input type="text" value="" style="width: 200px;" id="277" name="Name"/>
</div> <br/>
<div class="submit"><input type="submit" value="submit"/></div>
</form>
Action Submit in the Cakephp controller is
function submit($formid = null,$fillerid=null)
{
$this->data['Result']['form_id']=$formid;
$this->data['Result']['submitter_id']=$fillerid;
$this->data['Result']['submitter']=$this->Session->read('filler');
echo "submitter: ".$this->Session->read('filler');
$results=$this->Form->hasResults($this->data);
echo http_build_query($_POST);
if(empty($results)){
foreach ($_POST as $key => $value):
if(is_array($value)){
$value = implode('', $_POST[$key]);
$this->data['Result']['value']=$value;
}
else{
$this->data['Result']['value']=$value;
}
$this->data['Result']['form_id']=$formid;
$this->data['Result']['submitter_id']=$fillerid;
$this->data['Result']['label']=Inflector::humanize($key);
$this->data['Result']['submitter']=$this->Session->read('filler');
$this->Form->submitForm($this->data);
endforeach;
$this->Session->setFlash('Your entry has been submitted.');
$this->Invite->updateAll(array('Invite.filled'=>"'Yes'"),array('Invite.id'=>"$fillerid"));
}else{
$this->Session->setFlash('You have already filled the Form .');
}
}
In /app/models/upload.ctp:
function beforeSave()
{
if (!empty($this->data['Upload']['File']) && is_uploaded_file($this->data['Upload']['File']['tmp_name']))
{
if (!move_uploaded_file($this->data['Upload']['File']['tmp_name'], 'some_location/' . $this->data['Upload']['File']['name']))
{
return false;
}
$this->data['Upload']['name'] = $this->data['Upload']['File']['name'];
$this->data['Upload']['type'] = $this->data['Upload']['File']['type'];
$this->data['Upload']['size'] = $this->data['Upload']['File']['size'];
}
return true;
}
In /app/controllers/uploads_controller.php:
function add()
{
if (!empty($this->data))
{
if ($this->Upload->save($this->data))
{
$this->Session->setFlash('File upload successful.');
$this->redirect('/uploads');
}
}
}
In app/views/uploads/add.ctp:
echo $form->create('Upload');
echo $form->input('Upload.File', array('type' => 'file'));
echo $form->submit('Upload the file');
echo $form->end();

Form helper for creating Radio button in Cakephp

i am trying to create a Radio button using Cakephp like the one the result should resemble like
<div data-attr="radio" id="1">
<label id="label1">Untitled1</label><br/>
<input type="radio" value="option1" id="Radio11" name="Workexperience"/>
<label for="Radio11">Option1</label>
<input type="radio" value="option2" id="Radio12" name="Workexperience"/>
<label for="Radio12">Option2</label>
</div>
how to generate so using Form helper..
Please suggest me..
This might help,
http://book.cakephp.org/view/189/Automagic-Form-Elements#options-before-options-between-options-separator-a-191
For radio type input the 'separator' attribute can be used to inject markup to separate each input/label pair.
Code View
<?php echo $form->input('field', array(
'before' => '--before--',
'after' => '--after--',
'between' => '--between---',
'separator' => '--separator--',
'options' => array('1', '2'),
'type' => 'radio'
));?>
Output:
<div class="input">
--before--
<input name="data[User][field]" type="radio" value="1" id="UserField1" />
<label for="UserField1">1</label>
--separator--
<input name="data[User][field]" type="radio" value="2" id="UserField2" />
<label for="UserField2">2</label>
--between---
--after--
</div>
Looks like $form->radio() should do what you need. I don't know if it will look exactly like your example.

Resources