如何在MySQL数据库中插入变量数据

我需要帮助来弄清楚为什么它不起作用。我显然不了解在数据库中插入变量的语法。在数组中没有变量的情况下,此数据库插入正在起作用,因此问题并非出自此。我还在文件中进一步回显了该变量,以便该变量存在并起作用。

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_candidatescandidate_emailproject_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();
    }
woshijxsd0001 回答:如何在MySQL数据库中插入变量数据

如果$donnesPost没有名为ID的键,则语句$donnesPost->ID将静默返回NULL,这就是这里发生的情况。

如果根本没有定义$donnesPost,那么会发生同样的事情,无论是否相信,PHP都会向日志文件发出“警告”消息,然后继续!!

em>在我看来,在调用update_post()时,还没有定义变量。 ,

感谢您的帮助。这是Grumpy建议的解决方案,Reinstate Monica起作用

// 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();
    }
本文链接:https://www.f2er.com/3107942.html

大家都在问