如果field为空,则使用PHP empty()文件链接设置为null

时间:2015-01-20 15:31:22

标签: php file-upload string

我有一个允许用户更新标题和文件的编辑表单。如果用户将文件字段留空,则从我的sql表中删除url。我使用empty()函数将脚本定向到两个部分之一。更新文件URL的位置和不更新的位置。这不仅不起作用,而且似乎也是一种不正确的方法。如果用户将文件输入字段留空,我希望已存储在sql表中的url不会更新。任何建议 - 提前谢谢你!

HTML

<form name="edit_date" class="data-form" method="POST" id="edit_list_data" enctype="multipart/form-data>
<input  class="fileUpload" name="flyer" type="file"  />

PHP

<?php
    //if file field empty just update title
    if (empty($_FILES['flyer']['name'])) 
    {
        require('../dbcon2.php');
        try 
        {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $stmt = $conn->prepare("UPDATE listings SET title = :title WHERE id = :id");
            //Bind
            $stmt->bindParam(':id', $_POST['id']);
            $stmt->bindParam(':title', $_POST['title']); 
            $stmt->execute();
            $response = array
                 ('state'  => 200, "success" => true, "id" => $_POST['id'], "titlea" => $_POST['title'] 
                 );
            echo json_encode($response);
        }
        catch (Exception $e) 
        {
            $data = array("success" => false, "sentence" => $sql, "error" => $e->getMessage());
            exit(json_encode($data));
        }
    }
    //if file field not empty update it and title
    else 
    {
        $allowedExtsf = array("pdf");
        $tempf = explode(".", $_FILES["flyer"]["name"]);
        $extensionf = end($tempf);
        if (($_FILES["flyer"]["type"] == "application/pdf") && ($_FILES["flyer"]["size"] < 524288000) && in_array($extensionf, $allowedExtsf)) 
        {   
            if ($_FILES["flyer"]["error"] > 0) 
            {
                echo "Return Code: " . $_FILES["flyer"]["error"] . "<br>";
            }   
            else 
            {
                if (file_exists("../flyers/" . $_FILES["flyer"]["name"])) 
                {
                    unlink("../flyers/" . $_FILES["flyer"]["name"]);
                }
                move_uploaded_file($_FILES["flyer"]["tmp_name"],"../flyers/" . $_FILES["flyer"]["name"]);             
                $ad_link="http://www.website.com/flyers/" . $_FILES["flyer"]["name"];
            }
        }
    }
    require('../dbcon2.php');
    try 
    {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("UPDATE listings SET title = :title, ad_link = :ad_link WHERE id = :id");
        //Bind
        $stmt->bindParam(':id', $_POST['id']);
        $stmt->bindParam(':title', $_POST['title']); 
        $stmt->bindParam(':ad_link', $ad_link);
        $stmt->execute();
        $response = array
             ('state'  => 200, "success" => true, "id" => $_POST['id'], "ad_linka" => $ad_link, 
             );
        echo json_encode($response);  
    }
    catch (Exception $e) 
    {
        $data = array("success" => false, "sentence" => $sql, "error" => $e->getMessage());
        exit(json_encode($data));
    }     
?>

1 个答案:

答案 0 :(得分:1)

  

第二个try / catch应该只在第二个实例中运行,只运行   如果文件字段不为空,则为else {}

但无论if/else阻止,这部分代码都在运行。因此,覆盖表中的字段ad_link。请参阅:UPDATE listings SET title = :title, ad_link = :ad_link WHERE id = :id

只需将代码放在$ad_link="http://www.website.com/flyers/" . $_FILES["flyer"]["name"];

之后
require('../dbcon2.php');
try 
{
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("UPDATE listings SET title = :title, ad_link = :ad_link WHERE id = :id");
    //Bind
    $stmt->bindParam(':id', $_POST['id']);
    $stmt->bindParam(':title', $_POST['title']); 
    $stmt->bindParam(':ad_link', $ad_link);
    $stmt->execute();
    $response = array
         ('state'  => 200, "success" => true, "id" => $_POST['id'], "ad_linka" => $ad_link, 
         );
    echo json_encode($response);  
}
catch (Exception $e) 
{
    $data = array("success" => false, "sentence" => $sql, "error" => $e->getMessage());
    exit(json_encode($data));
}   

完整代码应该是这样的

//if file field empty just update title
if (empty($_FILES['flyer']['name'])) 
{
    require('../dbcon2.php');
    try 
    {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("UPDATE listings SET title = :title WHERE id = :id");
        //Bind
        $stmt->bindParam(':id', $_POST['id']);
        $stmt->bindParam(':title', $_POST['title']); 
        $stmt->execute();
        $response = array
             ('state'  => 200, "success" => true, "id" => $_POST['id'], "titlea" => $_POST['title'] 
             );
        echo json_encode($response);
    }
    catch (Exception $e) 
    {
        $data = array("success" => false, "sentence" => $sql, "error" => $e->getMessage());
        exit(json_encode($data));
    }
}
//if file field not empty update it and title
else 
{
    $allowedExtsf = array("pdf");
    $tempf = explode(".", $_FILES["flyer"]["name"]);
    $extensionf = end($tempf);
    if (($_FILES["flyer"]["type"] == "application/pdf") && ($_FILES["flyer"]["size"] < 524288000) && in_array($extensionf, $allowedExtsf)) 
    {   
        if ($_FILES["flyer"]["error"] > 0) 
        {
            echo "Return Code: " . $_FILES["flyer"]["error"] . "<br>";
        }   
        else 
        {
            if (file_exists("../flyers/" . $_FILES["flyer"]["name"])) 
            {
                unlink("../flyers/" . $_FILES["flyer"]["name"]);
            }
            move_uploaded_file($_FILES["flyer"]["tmp_name"],"../flyers/" . $_FILES["flyer"]["name"]);             
            $ad_link="http://www.website.com/flyers/" . $_FILES["flyer"]["name"];

            //Moved here - Starts
            require('../dbcon2.php');
            try 
            {
                $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $stmt = $conn->prepare("UPDATE listings SET title = :title, ad_link = :ad_link WHERE id = :id");
                //Bind
                $stmt->bindParam(':id', $_POST['id']);
                $stmt->bindParam(':title', $_POST['title']); 
                $stmt->bindParam(':ad_link', $ad_link);
                $stmt->execute();
                $response = array
                     ('state'  => 200, "success" => true, "id" => $_POST['id'], "ad_linka" => $ad_link, 
                     );
                echo json_encode($response);  
            }
            catch (Exception $e) 
            {
                $data = array("success" => false, "sentence" => $sql, "error" => $e->getMessage());
                exit(json_encode($data));
            }     

            //Moved here - Ends
        }
    }
}