doumu1212 2015-11-11 16:07
浏览 49

使用PHP将表单上传到数据库

I am trying to code for uploading data from form plus upload file to a server path using PHP. In this I am able to achieve the functionality of uploading files and data but in this I wanted to add one more feature where, if email already exist then a warning should be thrown that "email already exist" and pnly pdf file should be accepted. I tried some coding but it does not work.

Can any guru add code block to the below code to achieve the functionality of unique email id and restrict file type to only pdf.

    <?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{    
     $name = trim($_POST["uname"]);
     $email = trim($_POST["uemail"]);
     $exp = trim($_POST["uexp"]);
     $desig = trim($_POST["udesig"]);
     $tech = trim($_POST["utech"]); 


    $file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder="uploads/";

    // 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);    

    if(move_uploaded_file($file_loc,$folder.$final_file))

    {

        $sql="INSERT INTO tbl_uploads(name,email,exp,desig,tech,file,type,size) VALUES('$name','$email','$exp','$desig','$tech','$final_file','$file_type','$new_size')";
        mysql_query($sql);
        ?>
        <script>

        window.location.href='success.php';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
        alert('error while uploading file');
        window.location.href='index.php?fail';
        </script>
        <?php
    }
}

?>
  • 写回答

2条回答 默认 最新

  • doujia7779 2015-11-11 18:36
    关注

    Try changing your code to

    $sql = "SELECT COUNT(*) AS count from tbl_uploads where email = :email_id";
    try {
        $stmt = $DB->prepare($sql);
        $stmt->bindValue(":email_id", $email, PDO::PARAM_STR);
        $stmt->execute();
        //only grabbing one row so use fetch instead of fetchAll
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
        /*
        *check if result is an array
        *check is count exists in the array
        * check if result['count'] gt 0
        */
        if (is_array($result) && array_key_exists('count', $result) && $result["count"] > 0) {
            $msg = "Email already exist";
            $msgType = "warning";
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条