MySQL PHP没有将所有数据插入数据库

时间:2017-07-24 14:23:44

标签: php sql forms

所以......这个问题让我在这个时刻失去了头发。

我的网站上有一个表单,允许用户将图像上传到图库文件夹,并且工作正常。它上传文件,将其插入数据库,然后我可以进入图库并显示。

问题在于,由于某种原因让我感到厌烦,它不会将$ _POST [' caption']变量插入到数据库中。当你点击提交时它甚至无法捕获它。所以现在我有几张没有列出标题的图片,即使已经输入了一个标题。 (请注意,我有一个检查以确保该字段不为空,并且在运行检查时不会抛出任何错误。)

这是我的php和表单部分的代码:

if(isset($_POST['submit']))
{
    $caption = trim($_POST['caption']);
    $category = trim($_POST['gallery']);

    if($caption = '')
    {
        $error .= '<p class="error">Please enter a caption for your image.</p>';
    }

    if($gallery = '')
    {
        $error .= '<p class="error">Please select a gallery for your image.</p>';
    }   

    //Begin upload checks
    $dir = "../gallery/";
    $maxsize = 5000000; // 5MB
    $valid_exts = array('jpeg','jpg','png','gif');
    $ok = 1;

    if(isset($_FILES['file']))
    {
        $target_file = $dir . basename($_FILES['file']['name']);
        if($_FILES['file']['size'] < $maxsize)
        {
            // get file extension
            $ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
            if(in_array($ext, $valid_exts))
            {
                if(file_exists($target_file))
                {
                    $error .= '<p class="error">File already exists.</p>';
                    $ok = 0;
                }
                else
                {
                    $ok = 1;
                }
            }
            else
            {
                $error .= '<p class="error">Image must be a png, gif, or jpg/jpeg file.</p>';
                $ok = 0;
            }
        }
        else
        {
            $error .= '<p class="error">Image must be no larger than 5MB.</p>';
            $ok = 0;
        }
    }
    else
    {
        $error .= '<p class="error">No image was selected for upload.</p>';
        $ok = 0;
    }

    if(empty($error) && $ok == 1)
    {
        if(move_uploaded_file($_FILES['file']['tmp_name'], $target_file))
        {
            $date = date('m-d-Y');
            $stmt = $db->prepare('INSERT INTO gallery_photos (photo_filename,photo_caption,photo_category,postdate) VALUES (?,?,?,STR_TO_DATE(?, "%m-%d-%Y"))');
            if($stmt)
            {
                $stmt->bind_param('ssss',$_FILES['file']['name'],$caption,$category,$date);
                if($stmt->execute())
                {
                    $success .= '<p class="success">File successfully uploaded to the gallery.</p>';
                }
                else
                {
                    $error .= '<p class="error">Error code 89. Please contact the site administrator.</p>';
                }
            }
            else
            {
                $error .= '<p class="error">Error code 86. Please contact the site administrator.</p>';
            }
        }
        else
        {
            $error .= '<p class="error">An error occured while uploading your file.</p>';
        }
    }
}

?>

<div id="form">
    <form action="" method="post" enctype="multipart/form-data" name="upload_form">
    <table cellspacing="2" cellpadding="2" width="500">
        <tr><th colspan="2">Upload Image</th></tr>

        <tr><td colspan="2">
        <?php
        if($error)
        {
            echo $error;
        }

        if($success)
        {
            echo $success;
        }

        if($caption)
        {
            echo $caption;
        }
        ?>

        <p>Only PNG files are allowed.</p>
        </td></tr>

        <tr>
            <td align="right"><label for="gallery">Gallery</label></td>
            <td>
                <select name="gallery">
                    <option value="">Select One...</option>
                    <?php
                    $result = $db->query('SELECT * FROM gallery_category');
                    if(is_object($result) && $result->num_rows > 0)
                    {
                        while($row = $result->fetch_array())
                        {
                            echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
                        }
                    }
                    ?>
                </select>
            </td>
        </tr>

        <tr>
            <td align="right"><label for="file">Image</label></td>
            <td><input type="file" name="file" /></td>
        </tr>

        <tr>
            <td align="right"><label for="caption">Caption</label></td>
            <td><input type="text" name="caption" /></td>
        </tr>

        <tr><td align="center" colspan="2"><input type="submit" name="submit" value="Upload Image"</td></tr>
    </table>
    </form>
</div>

任何帮助识别这个问题的人都会非常感激,因为我似乎无法找到它。在我的日志或相关页面上不会抛出任何错误,并且它会被插入到数据库中而没有任何错误,并且图像上传时没有任何问题。

2 个答案:

答案 0 :(得分:1)

问题来自您的支票if($caption = '')if($gallery = '')。因为=是赋值运算符,而不是比较。它会将您的$caption分配给'',并且预计会显示空白标题的结果。您应该更改为if($caption == '')if($gallery == '')

答案 1 :(得分:0)

1)你分配了$ caption和$ gallery而不是检查

 if($caption = ''){ }

将$ caption设置为&#39; &#39;并且不会检查它,因为单=。因此标题将为空

你应该这样检查

 if($caption == ''){ }

with ==

也许你也应该尝试

 if($caption == NULL){ }

 if(empty($caption)){ }

2)$category = trim($_POST['gallery']);

我不确定你是否想要这样,也许你应该看看它