duan39779 2018-01-01 06:20
浏览 78
已采纳

在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.

展开全部

  • 写回答

1条回答 默认 最新

  • douyan9398 2018-01-01 06: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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部