duan39779
2018-01-01 14:20
浏览 63
已采纳

在Wordpress中,如何使用文件上载选项编写自定义表单并将记录保存在MySql中?

So far I have been able to make a custom form which upon successful entry save the value to a MySql table called "form_entry"

Below is the code for a form:

<form method="post" class="container">
<div class="row">
<div class="col-md-6 mb-3">
<label>Name</label>
<input type="text" name="myname" class="form-control" placeholder="Name" value="John Doe" required>
</div>
<div class="col-md-3 mb-3">
<label>State</label>
<input type="text" name="statename" class="form-control" placeholder="State" required>
</div>
</div>
<button class="btn btn-primary" type="submit" name="BtnSubmit" value="submit">Submit form</button>
</form>

And I insert a few codes in the functions.php under my template folder like this :

if(isset($_POST['BtnSubmit'])) {
    global $wpdb;
    $data_array = array(
                        'myname' => $_POST['myname'],
                        'statename'  => $_POST['statename'],

                        );

    $table_name = 'form_entry';
    $rowResult = $wpdb ->insert($table_name, $data_array, $format=NULL);

    if($rowResult == 1) {
        echo 'Success';
    }
else {
    echo ' Error in my pant';
}   
die;
}

This is working fine. But I want to add a file upload option in the form and save the uploaded file location to MySql. Please help.

图片转代码服务由CSDN问答提供 功能建议

到目前为止,我已经能够创建一个自定义表单,在成功输入后将值保存到名为“MySql表” form_entry“

以下是表单的代码:

 &lt; form method =”post“class =”container“&gt;  
&lt; div class =“row”&gt; 
&lt; div class =“col-md-6 mb-3”&gt; 
&lt; label&gt;名称&lt; / label&gt; 
&lt; input type =“text”name =  “myname”class =“form-control”占位符=“名称”值=“John Doe”必需&gt; 
&lt; / div&gt; 
&lt; div class =“col-md-3 mb-3”&gt; 
&lt;  label&gt; State&lt; / label&gt; 
&lt; input type =“text”name =“statename”class =“form-control”placeholder =“State”required&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt;  button class =“btn btn-primary”type =“submit”name =“BtnSubmit”value =“submit”&gt;提交表单&lt; / button&gt; 
&lt; / form&gt; 
   
  
 

我在我的模板文件夹下的functions.php中插入了一些代码,如下所示:

  if(isset($ _ POST ['BtnSubmit']))  {
 global $ wpdb; 
 $ data_arra  y =数组(
'myname'=&gt;  $ _POST ['myname'],
'statename'=&gt;  $ _POST ['statename'],
 
); 
 
 $ table_name ='form_entry'; 
 $ rowResult = $ wpdb  - &gt; insert($ table_name,$ data_array,$ format = NULL); \  n 
 if($ rowResult == 1){
 echo'Eccess'; 
} 
else {
 echo'in pant in my pant'; 
} 
die; 
} 
    
 
 

这很好用。 但我想在表单中添加一个文件上传选项,并将上传的文件位置保存到MySql。 请帮忙。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyan9398 2018-01-01 14:46
    已采纳

    You have to add one field into your html form. and add parameter for into form.

    <form method="post" class="container" enctype="multipart/form-data">
    
    <div class="row">
        <div class="col-md-6 mb-3">
            <label>File</label>
            <input type="file" name="myfile" class="form-control" required/>
        </div>
    </div>
    

    then after submit you have to save uploaded file at particular location and save that file into your db.

    if (isset($_POST['BtnSubmit'])) {
        $target = 'uploads/' . basename($_FILES['myfile']['name']);
    
        if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target)) {
            $fp = fopen($target, "r");
        }
        global $wpdb;
        $data_array = array(
                        'myname' => $_POST['myname'],
                        'statename'  => $_POST['statename'],
                        'filename' => $target
                     );
    }
    

    I think it will work for you !! feel free to ask me.

    点赞 打赏 评论

相关推荐 更多相似问题