Retrieve WordPress Database Info and display in form - database

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>

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

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.

Issue with Check box update or Edit to database in Codeigniter

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.

get form data wordpress in same page

I am learning word press. I have 2 questions
I have created a page and assigned my template file for it.
1. Question
How to get my form input values in same page or in another page template?
<form id="formId" action="" method="post">
<input style="width:88%" type="text" name="name">
<input type="submit" name="subs" value="Submit" />
</form>
In same file I put:
<?php
if( isset($_POST['subs']) ) {
echo $_POST['name'];
}
?>
I also tried $_SERVER['PHP_SELF']; in action but it's not displayed.
How could I store it in my own table?
When click submit, it refreshes and displays "No Results Found".
2. Question
How to show the submitted values in wp-admin?
Here's the word press code flow I can't solve.
Change Form action to the same page:
<form action="<?php the_permalink(); ?>
try to use
action="<?php echo get_permalink(); ?>"
instead of
action="<?php echo $_SERVER['PHP_SELF']; ?>"
in your form

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

Resources