正在上传文件,但在MySQL中未执行查询

时间:2019-05-25 08:57:06

标签: php mysql

我正在为我的博客开发一个后端,用于上传图像的代码按预期工作,但是在数据库中什么也没写。当我提交时,它会上传图像并将其移至指定的文件夹,但在数据库中未插入任何记录。这是用于上传的php。有什么办法可以解决这个问题?


<form method="POST" action="functionabout.php" enctype="multipart/form-data">

                <div class="col-md-9">
                    <!-- general form elements -->
                    <div class="box box-primary">                         <div class="box-header with-border">
                            <h3 class="box-title">Add About Content</h3>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-body">
    <div class="form-group">
        <label for="heading">Heading</label>
        <input placeholder="Write Heading" class="form-control" name="heading" type="text" id="heading" required>
            </div>
    <div class="form-group">
        <label for="subheading">Sub Heading</label>
        <textarea class="form-control" placeholder="Write Sub Heading" name="subheading" cols="50" rows="10" id="subheading" required></textarea>
            </div>
    <div class="form-group">
        <label for="message">Message</label>
        <textarea placeholder="Write Details" class="form-control" name="message" cols="50" rows="10" id="message" required></textarea>
            </div>
        <div class="form-group">
        <label for="Filename">Signature</label>
        <input name="Filename" type="file">
            </div>
</div>
                        <div class="box-footer fboxm">
                            <button type="submit" class="btn btn-primary"><i class="fa fa-check icheck"></i>Submit</button>
                            <button type="reset" class="btn btn-warning"><i class="fa fa-undo icheck"></i>Reset</button>
                        </div>
                    </div>
                </div>


                    </div>
                    <!-- /.box -->

                </div>
                <!-- right column -->
            </form>
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['Filename']['name']);

//This gets all the other information from the form
$Filename=basename( $_FILES['Filename']['name']);
$heading=$_POST['heading'];
$subheading=$_POST['subheading'];
$message=$_POST['message'];


//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['Filename']['name']). " has been uploaded, and your information has been added to the directory";
    // Connects to your Database
    $conn = mysqli_connect('localhost', 'root', '', 'db');
    //Writes the information to the database
    mysqli_query($conn,"INSERT INTO about (subheading,heading,message,signature)
    VALUES ('$subheading',$heading','$message','$Filename')") ;

} else {
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}



?>
id int(11) PK
subheading varchar(255)
heading varchar(255)
message varchar(255)
signature varchar(255)

我希望同时上传文件和更新数据库。

1 个答案:

答案 0 :(得分:0)

尝试一下:

$conn = mysqli_connect('localhost', 'root', '', 'db');
    //Writes the information to the database
    mysqli_query($conn,'INSERT INTO about (subheading,heading,message,signature)
    VALUES ("'.$subheading.'","'.$heading.'","'.$message.'","'.$Filename.'")') ;
相关问题