我需要帮助来弄清楚为什么它不起作用。我显然不了解在数据库中插入变量的语法。在数组中没有变量的情况下,此数据库插入正在起作用,因此问题并非出自此。我还在文件中进一步回显了该变量,以便该变量存在并起作用。
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.';
return;
}
insert_row();
}
$donneesPost = get_post();
echo $donneesPost->ID; //this works
function insert_row()
{
global $wpdb,$donneesPost;
$tablename = 'pp_candidates';
$data = array(
'candidate_email' => 'email@email.com',//just to see that without a variable it functions
'project_ID' => $donneesPost->ID,'candidate_approved' => '1'
);
var_dump($data);
$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
);
$wpdb->show_errors();
$wpdb->insert($tablename,$data,$formats);
}
function display_form(){
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">
</form>
';
}
display_form();
这是我得到的错误:
Erreur de la base dedonnéesWordPress:[列'project_ID'不能 为空]插入
pp_candidates
(candidate_email
,project_ID
,candidate_approved
)值('emqil@email.com',NULL,'1')
这是脾气暴躁的人提出的解决方案
// This is what it was like in the original post
insert_row();
}
$donneesPost = get_post();
// This is what I changed to get it to work
$donneesPost = get_post();
insert_row();
}