简体   繁体   English

MySQL PHP不会将所有数据插入数据库

[英]MySQL PHP not inserting all data into database

So... this problem is causing me to lose hair at this very moment. 所以...这个问题正在此刻让我掉头发。

I have a form on my site that allows users to upload images to the gallery folder, and it works fine. 我的网站上有一个表单,允许用户将图像上传到Gallery文件夹,并且工作正常。 It uploads the file, inserts it into the database, and I can then go to the gallery and it appears. 它上传文件,将其插入数据库,然后我可以去画廊并显示它。

The problem, is that for some reason that escapes me, it will not insert the $_POST['caption'] variable into the database. 问题在于,出于某种原因,我无法将$ _POST ['caption']变量插入数据库。 It doesn't even capture it when you hit submit. 当您点击“提交”时,它甚至无法捕获。 So now I have several images with no caption listed, even though one was entered into the box. 所以现在我有几张没有标题的图像,即使已在框中输入了一张。 (Please note that I have a check in place to make sure that field is not empty, and no errors are thrown when running the checks). (请注意,我已经进行了检查,以确保该字段不为空,并且在运行检查时不会引发任何错误)。

Here is my code for the php and form sections: 这是我的php和form部分的代码:

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>

Any help at identifying this problem would be greatly appreciated, as I cannot seem to locate it. 识别此问题的任何帮助将不胜感激,因为我似乎找不到它。 No errors are thrown whatsoever in my logs, or on the page in question, and it is inserted into the database with no errors, and the image is uploaded with no problems. 我的日志或相关页面中都不会引发任何错误,并且它没有错误地插入到数据库中,并且图像上传没有问题。

the problem comes from your check if($caption = '') and if($gallery = '') . 问题来自您的检查if($caption = '')if($gallery = '') Because = is an assignment operator, not comparison. 因为=是赋值运算符,而不是比较。 It will assign your $caption to '' and the result of blank caption is expected. 它将把您的$caption分配给''并且期望空白标题的结果。 You should change to if($caption == '') and if($gallery == '') 您应该更改为if($caption == '')if($gallery == '')

1) You assigned $caption and $gallery instead of checking 1)您分配了$ caption和$ gallery而不是进行检查

 if($caption = ''){ }

will set $caption to ' ' and will not check it, because of the single =. 由于单个=,会将$ caption设置为'',并且不会对其进行检查。 Thus caption will be empty 因此字幕将为空

you should check this way 你应该这样检查

 if($caption == ''){ }

with == 与==

Maybe you should also try 也许你也应该尝试

 if($caption == NULL){ }

or 要么

 if(empty($caption)){ }

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

I'm not sure you want it this way, maybe you should check it out 我不确定您是否要这样,也许您应该检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM