wpdb not inserting row into the table - php

i have a form that allows input of name ,email & photo...when i click on submit i want it to insert a row into the posttype called'contact' with the info submitted the by the form and the image as the featured image but it does nothing
my code
<?php
if (isset($_POST['submit'])) {
$yourname=$_POST['yourname'];
$email=$_POST['email'];
$myimage=$_POST['myimage'];
include_once('../../../wp-config.php');
global $wpdb;
$table = 'wp_posts';
$data = array(
post_title=>$yourname,
post_status=>'Published',
post_type=>'contacts',
email=>$email,
featured_image=>$myimage
);
$wpdb->insert( $table, $data);
}
?>
<form action="" method="POST">
Your Name: <input type="text" name="yourname" value=""> <br>
Your Email: <input type="text" name="email" value=""> <br>
Image: <input type="file" name="myimage" id=""><br>
<input type="submit" name="submit" value="submit" />
</form>
is there any way i can check if row is uploaded & echo a message if insert is done and then redirect to a new url after few seconds.

Check if columns passed on $data exists in database or in form don't have method="post" enctype="multipart/form-data", try insert this.

You can not get the value of uploaded files from the $_POST variable. For that you need to use $_FILES variable.
To make that code work first you have to add following :
In the form tag you need to add method="post" enctype="multipart/form-data".
In the the PHP script where you get the value of image, Use this code to get the value of image.
featured_image => $_FILES['myimage']['name']

Related

Fetch value of a input type and store in variable

I have following input type
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
Its value is value="EXTERIOR". I want to fetch the value and store in a variable. I am not getting any idea regarding this. I am trying to modify plugin (career portfolio) according to my project and its part of that.
Your html form:
<form method="POST" action="process.php">
<input type="text" name="foo">
<input type="submit">
</form>
Your Php form processing page (process.php):
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$foo = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING);
}
?>
This is front end code, if you are trying to move front end data to server side it must pass through the server. i.e. $_POST...
now you can either do this with AJAX / PHP or PHP / HTML only
<?php
if(isset($_POST[project.title])
$var = $_POST[project.title]; //its now in a varible
?>
<form method="POST" action="thispage.php">
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
<input type=submit value="PRESS ME TO SUBMIT VALUE TO VAR">
</form>

Updating user table in database

I've created a members area where a user can update their bio. The problem is that the information the user submits isn't updating the rows in the database.
Member's Area
<body bgcolor="#E6E6FA">
<button>Log Out</button><br><br>
<input type="text" name="age"placeholder="Enter a your age."><br>
<input type="text" name="bio"placeholder="Enter your bio.">
<input type="submit" name="submit" value="Submit your details!">
PHP
<?php
if(isset($_POST['submit'])){
$con=mysql_connect("localhost","root","****","****");
// Check connection
if (mysql_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$age = mysql_real_escape_string($_POST['age']);
$bio = mysql_real_escape_string($_POST['bio']);
$name = mysql_real_escape_string($_SESSION['username']);
mysql_query($con,"UPDATE accs SET age='.$age.' WHERE name='.$name.'");
mysql_query($con,"UPDATE accs SET bio='.$bio.' WHERE name='.$name.'");
mysql_close($con);
};
?>
</body></html>
Any Ideas as to what is wrong here?
in your HTML page, the form should be inside the <form></form> tags
<form method="post" action="update.php">
<input type="text" name="age" placeholder="Enter a your age.">
<br>
<input type="text" name="bio" placeholder="Enter your bio.">
<input type="submit" name="submit" value="Submit your details!">
</form>
In your PHP page - to check the results, you can temporarily echo $age; echo $bio;
As you are using $_SESSION['username']; I think you are missing session_start(); to the top of your PHP code.
Also mysql_query only needs the SQL command, and not the connection ($con), that is mysqli, which is strongly advised to use instead of mysql_*.
As a side note, don't rely on user names in your database as the update criteria. If not already introduced, you can add an ID column to your table
a) create a proper submit form. use form tags around your form fields.
b) check, that the form is correctly submitted, by checking the $_POST array.
var_dump($_POST);
c) check, that you have values for the fields that you want to insert.
do a var_dump() before mysql_query(), to see what's going on.
var_dump($age, $bio, $name);
d) combine your two query calls into one:
mysql_query($con, "UPDATE accs SET age='.$age.', bio='.$bio.' WHERE name='.$name.'");
If you want to use the page it self to process your request, then empty the action property of your form. For example :
<form method="post" action="">
<input type="text" name="age"placeholder="Enter a your age."><br>
<input type="text" name="bio"placeholder="Enter your bio.">
<input type="submit" name="submit" value="Submit your details!">
</form>

multiple submit buttons and $_post data - php

I have a simple photo gallery using CodeIgniter that displays thumbnails and select buttons for each image.
On this page I also have a choose file and 'upload' button and a 'delete selected items' button.
<form action="http://localhost:8080/PhpProject1/gallery"
method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" value="">
<input type="submit" name="upload" value="Upload">
<input type="submit" name="delete" value="Delete Selected">
</form>
My check boxes are grouped using the following style (i.e. 'photos[]' for group):
<input type="checkbox" name="photos[]" value="IMG_20120709_151023.jpg">
When debugging with Netbeans I am definitely calling the right method by getting the name value from the post data but with the 'delete' method the post data contains nothing else, just the input name and value (key= delete, value = Delete Selected)using.
Here is the php code:
$this->load->model('gallery_model');
if ($this->input->post('upload')) {
$this->gallery_model->do_upload($order_no);
redirect('gallery');
}
if ($this->input->post('delete')) {
$this->gallery_model->do_delete($order_no); // this is getting called ok, just no $_post data??
redirect('gallery');
}
Is there something else I need to do to ensure the post request is picking up the selected items?
I'd like to do this with php but if I have to go down the ajax route so be it, thanks.
Mick.

mysql_query result available on second post?

I am using a dynamically generated query string to display results from a search form for some reports - there are 5 search fields and $query2 could contain nothing or 5 additional search values.
$query="SELECT * from employee_work where company_id='$company_id' $query2";
When I POST the form I get the data displayed on screen which is great. I also then am using TCPDF to offer a PDF download of the data. I currently also the class for TCPDF via POST:
if($_POST['PDF']) {
do the TCPDF stuff......
}
<div id="export-buttons">
<form name="export" method="post" action="">
<input type="submit" name="PDF" value="PDF" class="button pdf">
</div>
The problem is that when a user clicks on the PDF button the POST array now only contains the value for the submit button and NOT the $query2 data from earlier so when I use TCPDF it outputs all the data and ignores the $query2 part of the search string.
How can I address this so either the original $query2 data stays available OR have anther way of checking if the form button has been clicked without overwriting the contents of POST? Can I use javascript to do this?
Thanks
Jason
why dont you add hidden inputs that have the values of the criteria from the search? That way when the user posts the request for the PDF you get also those fiels and can use them (AFTER making sure the values are SAFE) in the query.
Other safer way is to store in the session an object or array with the parameters that made the list and then pass the identifier of that search as a hidden input to the PDF form, like:
$_SESSION['sc0000001'] = array('field1'=>'value1', 'field2'=>'value2', 'field3'=123);
...
<form>
<input type="hidden" name="sc" value="0000001" />
...
</form>
when you post the form you get the identifier of the search and create the query with the criteria assign to session...
EDITED:
html before posting list criteria:
<form>
<input type="text" name="filter1" value="" />
<input type="text" name="filter2" value="" />
<input type="text" name="filter3" value="" />
<input type="text" name="filter4" value="" />
<input type="text" name="filter5" value="" />
...
<input type="submit" value="go go go" />
</form>
PHP that gets the filters, builds query, gets results and stores in session.
$sql = " select * from table_name where 1 ";
$arrFilters = array();
for($i=1;isset($_POST['filter'.$i]) && trim($_POST['filter'.$i])!="";$i++) {
$arrFilters['filter'.$i] = mysql_real_escape_string($_POST['filter'.$i]);
$sql.=" AND filter".$i."=".$arrFilters['filter'.$i];
}
// here you should have the complete $sql with the filters supplied
// lets save this search, we are going to keep only the last search from this user in his session.
$_SESSION['listingFilters'] = $arrFilters;
HTML with search results and after the form to get pdf:
<form>
<input type="submit" value="get pdf" />
</form>
PHP:
After the post to get the pdf we go check if there are filters
$sql = " select * from table_name where 1 "; // basic query
// get the filters array from session
$arrFilters = isset($_SESSION['listingFilters']) ? $_SESSION['listingFilters'] : array();
foreach($arrFilters as $filter => $value) { // for each filter add it to the query
$sql.=" AND filter".$i."=".$arrFilters['filter'.$i];
}
// here you should have the complete $sql with the filters in session
// and can do your pdf magic
Add pepper and salt to your pleasure (you need to revise the code to work for you and maybe also the query if you are using text has filters)
Without seeing more code, this is probably because you have two "forms" on the HTML page, with one submit button in one form, and another in the other form.
<form>
<input name="1" />
<input type="submit" name="go"/>
</form>
<form>
<input type="submit" name="createPDF" />
</form>
Make sure you have all the fields/buttons inside the one form.
<form>
<input name="1" />
<input type="submit" name="go"/>
<input type="submit" name="createPDF" />
</form>

passing value in hidden field from one page to another in php

I have a simple registration form.
I want to pass value entered in one page to other in a text field.
how to pass and access it from next page in php.
this is my first php page.
Thanks in advance.
You can add hidden fields within HTML and access them in PHP:
<input type="hidden" name="myFieldName" value="someValue"/>
Then in PHP:
$val = $_POST['myFieldName'];
If you're going to ouput this again you should use htmlspecialchars or something similar to prevent injection attacks.
<input type="hidden" name="myFieldName" value="<?=htmlspecialchars($_POST['myFieldName']);?>"/>
Suppose this the form input in page A
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="">
<input type=submit>
</form>
In page B
In the page you want to get values put this code
<?PHP
foreach ($_REQUEST as $key => $value ) {
$$key=(stripslashes($value));
}
?>
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="<?PHP echo $myvalue" ?>">
<input type=submit>
</form>
So yo can use or attach variable value to another form do what else you want to do
use following code, that should help you.
<form action ="formhandler.php" method ="POST" >
<input name = "inputfield" />
<input type="submit" />
</form>
on formhandler.php file yo need to enter following code to get the value of inputfiled.
$inputfield = isset($_POST['inputfield'])?$_POST['inputfield']:"";
// now you can do what ever you want with $inputfield value
echo($inputfield);

Categories