输入为空,但该功能仍然运行

时间:2013-04-28 14:32:30

标签: php

我有这个输入字段:

<input type="text" id="image_url_input" class="url_input" /></div>

这个php代码:

$title = mysqli_real_escape_string($con,htmlentities($_POST['title']));
$content = mysqli_real_escape_string($con,nl2br(htmlentities($_POST['content'])));

$sql = mysqli_query($con,"INSERT INTO nfeed (title, content) VALUES 
('$title','$content')");

if (isset($_POST['image'])) { //This one runs whether or not the input field is empty 

        $image = file_get_contents($_POST['image']);

        $result = mysqli_query($con,"SELECT * FROM nfeed WHERE ID=(SELECT max(ID) FROM nfeed)"); //Here the renaming of the image starts...

            $row = mysqli_fetch_array($result);

            $id = $row['ID'];

            $new_name = $id . "_" . "000.jpg";

                if(file_exists("../images/" . $new_name)) {
                    $new_name = $id . "_" . "001.jpg";
                }

            file_put_contents("../images/" . $new_name, $image);

            $sql = mysqli_query($con,"UPDATE nfeed SET file='{$new_name}' WHERE ID='{$id}'");

        }

当我输入图像URL时,它运行得非常好......当我不输入时 - 这就是问题

我试图将file_get_contents($_POST['image'])写入.txt文件,猜猜是什么:它完全是空的!

我在切换到AJAX之前运行了相同的功能。这是一个简单的从形式上抓取的代码,如果它是从用户的PC上传的话,它会添加一个图像。

我也尝试过:if (isset($_POST['image']) && ($_POST['image'] != 0)) {但仍然没有运气。为什么它认为我已经输入了什么?

1 个答案:

答案 0 :(得分:2)

使用

if(!empty($_POST['image']))

Isset检查数组($ _POST)键是否存在。

因此,如果值为空,则isset将返回true,您需要检查它是否为空。

相关问题