简体   繁体   English

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

[英]upload form to database using PHP

I am trying to code for uploading data from form plus upload file to a server path using PHP. 我正在尝试编写代码,以便使用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. 在此,我能够实现上传文件和数据的功能,但是在此,我想增加一个功能,如果已经存在电子邮件,则应该发出警告,提示“电子邮件已经存在”,并且应该接受pdf文件。 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. 任何大师都可以将代码块添加到以下代码中,以实现唯一电子邮件ID的功能并将文件类型限制为仅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
    }
}

?>

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";
    }

There are many ways to count rows, in your case change to: 有多种计数行的方法,您可以更改为:

$stmt->execute();
$result = $stmt->fetchAll();
if (count($result) > 0) {
   $msg = "Email already exist";
   $msgType = "warning";
}

or using fetch() 或使用fetch()

$stmt->execute();
if ($stmt->fetch()) {
  $msg = "Email already exist";
  $msgType = "warning";
}

or without fetching: 或不获取:

$stmt->execute();
if ($stmt->rowCount() > 0) {
  $msg = "Email already exist";
  $msgType = "warning";
}

or since PHP is loosely typed perhaps you just needed to cast it to an integer: 或者由于PHP是松散类型的,也许您只需要将其强制转换为整数即可:

if ((int)$result[0]["count"] > 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM