如果字段为空,则使用 PHP empty() 文件 link 将设置为 null

Using PHP empty() file link gets set to null if field is empty

我有一个允许用户更新标题和文件的编辑表单。如果用户将文件字段留空,则 url 将从我的 sql table 中删除。我正在使用 empty() 函数将脚本定向到两个部分之一。一种是文件 url 已更新,另一种是未更新。这不仅行不通,而且似乎是一种不正确的实现方式。如果用户将文件输入字段留空,我希望已经存储在 sql table 中的 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));
    }     
?>

The second try/catch should only run in the second instance, only run for else{} if the file field is not empty

但这部分代码是 运行 而不管 if/else 块。因此,覆盖 table 中的字段 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
        }
    }
}