I'm rewriting this to try to make it easier. I am inserting into a database table information which is sent when I click on a button in the page. When I insert the data it works, unless I try to include a post meta variable ($donnesPost->ID). I don't seem to know how to insert these variables.

`    if(isset($_POST['ticket_priority']))
// Get the nonce value for validation
$nonce = $_POST['ticket_nonce'];
// If the nonce does not verify, do NOT process the form.
if ( ! wp_verify_nonce($nonce, 'MyNonceAction')) {
     // If this spits out an error, that means the nonce failed
     echo 'Security error. Do not process the form.';

$donneesPost = get_post();
echo $donneesPost->ID; //this works !

function insert_row()
global $wpdb, $donneesPost;

$tablename = 'pp_candidates';
$data = array( 
'candidate_email' => 'r.roberts@valdoisecommunication.fr', 
'project_ID' => $donneesPost->ID,
    'candidate_approved' => '1' 

// Debugging: Lets see what we're trying to save

// FOR database SQL injection security, set up the formats
$formats = array( 
  //  '%d', // ticket_id should be an integer
  //  '%s', // ticket_user_id should be an integer
  //  '%d', // ticket_description should be a string
  //  '%s' // ticket_user_id should be an integer

// Debugging: Turn on error reporting for db to see if there's a database error
// Actually attempt to insert the data
$wpdb->insert($tablename, $data, $formats);

function display_form(){
// Display the post's ID
echo '
<form action="" method="post">';
// Add a nonce field
wp_nonce_field('MyNonceAction', 'ticket_nonce');
echo '
<input type="submit" name="ticket_priority" value = "Postuler pour ce projet" id="insertcandidatebutton">

So I wish someone could help me how to add the post meta variables.

This is what is printing: Erreur de la base de données WordPress : [Column 'project_ID' cannot be null] INSERT INTO pp_candidates (candidate_email, project_ID, candidate_approved) VALUES ('r.roberts@valdoisecommunication.fr', NULL, '1')

  • It looks like the problem is, your form action is empty, which means by default all the values will be POSTed to the URL the form is on. In other words, take your processing code out of functions.php and instead change your single.project.php template. The single project template should have the conditional at the top - if(isset($_POST['ticket_priority'])) do the form processing; else display the page with its form.
    – WebElaine
    Nov 13 '19 at 14:26
  • Thanks. I just tried this. I put all the code which was previously found in functions.php within single-project.php. Thanks I wasn't comfortable with it in functions since it only concerns this custom page. If I don't include the variable, the row is effectively inserted with the table, it's really the variable which is killing me. I'm going to modify this post.
    – RichyR
    Nov 13 '19 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.