我正在学习如何将php脚本集成到wordpress中。
我创建了一个函数:
function sollicitatie_formulier() {
ob_start();
include 'scripts/solliciteer-form.php';
return ob_get_clean();
}
add_shortcode( 'sollicitatie_formulier', 'sollicitatie_formulier' );
我正在调用的脚本如下所示:
<?php
global $wpdb;
if(isset($_POST['btn-upload']))
{
$table = sollicitaties;
$file = rand(1000,100000)."-".$_FILES['afile']['name'];
$file_size = $_FILES['afile']['size'];
$file_type = $_FILES['afile']['type'];
$file_loc = $_FILES['afile']['tmp_name'];
$folder="wp-content/themes/jobify-extended/uploads/";
$data = array(
'voornaam' => $_POST['avoornaam'],
'familienaam' => $_POST['afamilienaam'],
'comment' => $_POST['amsg'],
'afile' => rand(1000,100000)."-".$_FILES['afile']['name'],
'size' => $_FILES['afile']['size'],
'type' => $_FILES['afile']['type'],
);
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
$format = array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$success=$wpdb->insert( $table, $data, $format,$folder);
if($success){
?>
<script>
alert('successfully uploaded');
window.location.href='sollicitatie-formulier?success';
</script>
<?php
}
}
}
else {
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="voornaam">Voornaam: </label><input type="text" name="avoornaam" required="">
<label for="familienaam">Familienaam: </label><input type="text" name="afamilienaam" required="">
<label for="comment">bericht: </label><textarea type="text" name="amsg"></textarea>
<input type="file" name="afile" accept=".pdf, .doc, .docx" />
<input type="submit" name="btn-upload" value="upload"</input>
</form>
<?php
if(isset($_GET['success']))
{
?>
<label>Kandidaat succesvol toegevoegd... <a href="/bekijk/">click hier om kandidaten te bekijken.</a></label>
<?php
}
else if(isset($_GET['fail']))
{
?>
<label>Oeps...een probleem! Probeer opnieuw</label>
<?php
}
else
{
?>
<label>Try to upload any files(PDF, DOC, DOCX)</label>
<?php } }
?>
我的文件上传到我的文件夹,但是没有数据发送到我的数据库。
我正试图了解问题所在,但无法解决。
我希望您不介意解释我的错,而不仅仅是给出正确的答案。
最佳答案
您忘记添加$wpdb->prefix
。
例
global $wpdb;
$table = $wpdb->prefix.'sollicitaties';
关于php - 数据未上传到mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44905602/