php - 无法使用 PHP 将数据库中的 PDF 替换为另一个 PDF

标签 php mysql pdf

我正在尝试将之前在 MySQL 中上传的一个 pdf 替换为另一个 pdf 。我创建了一个 href [编辑],供用户选择 pdf 文件并链接到放置查询更新的 editDB.php。

<小时/>

这是edit.DB.php

<?php
        // Connect to the database
        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password=""; // Mysql password 
        $db_name="is"; // Database name 
        $tbl_name="publication"; // Table name 

if(isset($_POST['submit']) && $_FILES['userfile']['size'] > 0)
    {
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];

        $fp      = fopen($tmpName, 'r');
        $content = fread($fp, filesize($tmpName));
        $content = addslashes($content);
        fclose($fp);

        if(!get_magic_quotes_gpc())
            {
                $fileName = addslashes($fileName);
            }

        $conn = mysql_connect("$host", "$username", "$password"); 
        if(! $conn )
            {
              die('Could not connect: ' . mysql_error());
            }
<小时/>

连接数据库,获取file_id,获取更新的其他数据

$conn = mysql_connect("$host", "$username", "$password"); 
        if(! $conn )
            {
              die('Could not connect: ' . mysql_error());
            }

        if (isset($_GET["id"])) 
        {                           
                $id =$_GET["id"];
        }   
        else
        {
            echo'failed';
        }

        mysql_select_db($db_name);

        $title=mysql_real_escape_string($_POST['title']);
        $author=mysql_real_escape_string($_POST['author']);
        $year=mysql_real_escape_string($_POST['year']);
        $abs=mysql_real_escape_string($_POST['abstract']);
<小时/>

在此查询

$query="update publication set title='".$title."', author='".$author."', year='".$year."' , abstract='".$abs."', file_name='".$fileName."', file_size='".$fileSize."', file_type='".$fileType."', content='".$content."' where file_id='$id'";

        mysql_query($query) or die(mysql_error()); 


        echo '<script type="text/javascript">alert("'.$title.' updated!");
                window.location.href="publication.php";
            </script>';

        mysql_close($conn);
        }
?>

我面临的问题是,在我在编辑表单中输入更新的信息后。数据成功定向到editDB.php。显示了显示数据已更新的弹出对话框,但数据未更新数据库和显示信息的页面。我将感谢任何帮助。谢谢。

最佳答案

而不是

if (isset($_GET["id"])) 
        {                           
                $id =$_GET["id"];
        }   
        else
        {
            echo'failed';
        }

我把它改为

if(isset($_POST['submit'])&& $_FILES['userfile']['size'] > 0)
    {
        $id =$_POST["id"];

当然id必须正确传递并确保id与你的数据库匹配。

这段代码可以工作。希望能够帮助到大家。

关于php - 无法使用 PHP 将数据库中的 PDF 替换为另一个 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22651054/

相关文章:

mysql - SQL:INNER JOIN SELECT 使整个查询不返回任何内容

c# - 在 PHP/Bash/C# 中从 PDF 中删除图层/背景#

php - Foreach 在 where 语句中?

mysql - 连接两个表时如何避免重复值?

Php Hack中使用反射获取泛型类型

php - mysql_fetch_assoc 进入无限循环

cocoa - 在 Cocoa 中将 PDF 转换为高分辨率图像的最佳方法

typescript - Foreach 数组以在 TypeScript 中显示值

php - 使用mysql中的json字段获取页面内容

php - 如何在 Laravel 5 中处理 PDOException