如何通过php和mysql

时间:2017-02-15 17:05:49

标签: php mysql html5 file file-upload

<?php 
$msg='';
//if upload button is pressed
//if (isset($_POST['upload'])){
    if (isset($_POST['Image']['name'])&& isset ($_POST['Upload'])){
        //if (isset($_POST['Image'])){



    //the path to store the uploaded image
    $target = "tutorme5/tutorme/img/".basename($_FILES['Image']['name']);

    //get all the submitted data from the form
    $image = $_FILES['Image']['name'];
    //$image = $_POST['Image']['name'];

    $sql = "INSERT INTO User (Image) VALUES ('$image') where UserID='$userID'";
    mysqli_query($con, $sql); // stores submitted data in database

    // now we will move uploaded image into the folder: images/

    if (move_uploaded_file($_FILES['Image']['$tmp_name'], $target )) {

        $msg =" Image uploaded successfully";
        print_r($_FILES);


    }else{


        $msg ="there was a problemo uploading image";   



    }



}
?>

//从db中检索代码     

$thing="";
$sql = "SELECT * FROM User";
$result = mysqli_query ($con, $sql);
while ($row = mysqli_fetch_array($result)){

    $thing.= "<img src ='".$row['Image']."' >" ;


}

?>

// html和代码部分

<div id="content">
                        <form method="post" action="profile.php" enctype="mulitipart/form-data" >
                            <input type="hidden" name="size" value="1000000">
                            <div>
                            <input type="file" name="Image">
                            </div>
                            <div>
                            <input type ="submit" name="upload" value="Upload">
                            </div>
                            <div>
                            <?php echo $thing; ?>
                            </div>
                            </form>
                            </div>`enter code here`
  

我不知道为什么这不起作用。我看过无数的视频   教程并且已经困惑了两天了。任何帮助都会   非常感谢

1 个答案:

答案 0 :(得分:0)

将$ _POST更改为$ _FILES

    if (isset($_FILES['Image']['name'])&& isset ($_POST['Upload'])){
        //if (isset($_POST['Image']))

此处将$tmp_name更改为tmp_name

   if (move_uploaded_file($_FILES['Image']['tmp_name'], $target )) {

确保图像路径,它应包含路径"tutorme5/tutorme/img/"

    $thing.= "<img src ='".$row['Image']."' >" ;