HTML提交文件和文本数据到php


html submit file and text data to php

我试图捕获文件和数据并上传到php文件,但没有成功。我不确定我做错了什么,如果有人能看到我做错了什么,请。它将上传文件/图像没有问题,但当我添加一个文本输入张贴它将不起作用,这是我一直使用的模板来试用

<iframe name="my_iframe" src="" id="my_iframe"></iframe>
<form action="http://www.********/upload11.php" method="post" enctype="multipart/form-data" target="my_iframe">
<input type="file"  input id="input" name="image" />
<input type="text" name="Description" value="Image of Time"/>
<div>
<input type="submit" value="Send">
</div>
</form>

upload11.php

<?php
$upload_image = $_FILES["image"][ "name" ];
$folder = "images/";
move_uploaded_file($_FILES["image"]["tmp_name"], "$folder".$_FILES["image"]["name"]);;
$file = 'images/'.$_FILES["image"]["name"];
$uploadimage = $folder.$_FILES["image"]["name"];
$newname = $_FILES["image"]["name"];
$resize_image = $folder.$newname; 
list( $width,$height ) = getimagesize( $uploadimage );
$newwidth = 550;
$newheight = 350;
$thumb = imagecreatetruecolor( $newwidth, $newheight );
$source = imagecreatefromjpeg( $resize_image );
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg( $thumb, $resize_image, 100 ); 
$out_image=addslashes(file_get_contents($resize_image));
$msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
$a = ('" alt="" />');
$b = ("'<img src='"'https://www.******/images/".$_FILES['image']['name']."$a'");
$Description = $_POST['Description'];
$image = $_FILES['image']['tmp_name'];
$img = file_get_contents($image);
$con = mysqli_connect('***','******','******','********');
$sql = "INSERT INTO links (hyper_links, link ) VALUES ($b, $Description)";
$stmt = mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt, "s",$img);
mysqli_stmt_execute($stmt);
$check = mysqli_stmt_affected_rows($stmt);
if($check==1){
    $msg = 'Successfullly UPloaded';
}else{
    $msg = 'Could not upload';
}
mysqli_close($con);
}
?>
<?php
echo $msg;
?>

我已经重写了一半的脚本,以摆脱所有的错误和不必要的变量。这应该行得通。如果没有,它至少应该提供有关正在发生的事情的有用信息:

<?php
if(!empty($_FILES["image"])){
    $imgName = $_FILES["image"]["name"];
    $imgTmpName = $_FILES['image']['tmp_name'];
    $upload_dir = "images/";
    if(move_uploaded_file($imgTmpName, $folder . basename($imgName))){
        $imgPath = $upload_dir . $imgName;
        list( $imgWidth, $imgHeight ) = getimagesize( $imgPath );
        $newImgWidth = 550;
        $newImgHeight = 350;
        $thumbnailImg = imagecreatetruecolor( $newImgWidth, $newImgHeight );
        $originalImg = imagecreatefromjpeg( $imgPath );
        imagecopyresized($thumbnailImg, $originalImg, 0, 0, 0, 0, $newImgWidth, $newImgHeight, $imgWidth, $imgHeight);
        imagejpeg( $thumbnailImg, $imgPath, 100 );
        $mysqli = new mysqli("localhost", "user", "password", "database");
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        if($stmt = $mysqli->prepare("INSERT INTO links (hyper_links, link ) VALUES (?, ?)")){
            if($stmt->bind_param("ss", $html, $descryption)){
                $html = "<img src='"https://www.******/". $imgPath ."'">";
                $descryption = $_POST['Description'];
                if($stmt->execute()){
                    if($stmt->affected_rows > 0){
                        echo "Successfully Uploaded";
                        $stmt->close();
                        $mysqli->close();
                    } else {
                        echo "Could not upload";
                    }
                } else {
                    die("Execute() failed: " . htmlspecialchars($stmt->error));
                }
            } else {
                die("Bind_param() failed: " . htmlspecialchars($stmt->error));
            }
        } else {
            die("Prepare() failed: " . htmlspecialchars($stmt->error));
        }
    } else {
        die("Unable to move uploaded file to upload folder.");
    }
} else {
    die("You did not select a file to upload.");
}
?>

对您现在拥有的内容进行备份,并进行测试。

查看您忘记在$b和$Description中添加' '的代码

$sql = "INSERT INTO links (hyper_links, link ) VALUES ($b, $Description)";

应该是这样的

$sql = "INSERT INTO links (hyper_links, link ) VALUES ('$b', '$Description')";

希望这将工作:)