无法使用PHP获取图像

时间:2017-05-22 09:46:18

标签: php

我已经在这个项目上工作了很长一段时间,有一天,当我走过这段令我头痛的代码时,我正在做一些代码。所以我得到了我的HTML和PHP代码,但每当我尝试将图像上传到我的数据库时,图像变为空,我做错了什么?

<?php include "connection.php"; ?>
<?php
$n=$_POST["num"];
$t=$_POST["texto"];
$i=$_POST["imagem"];
$img = mysql_query("SELECT imagem2 from segurancaofensiva where nmr=$n");

$file = $_FILES['imagem']['tmp_name'];
$image = addslashes(file_get_contents($file));

$count = $connect->query("SELECT COUNT(DISTINCT nmr) FROM segurancaofensiva")->fetch_row()[0];

if ($connect->connect_error){
    die("Connection failed: " . $connect->connect_error);
}

$sql = "UPDATE segurancaofensiva SET texto='$t', imagem='{$image}', imagem2='{$image}' where nmr=$n" ;
$sql1 = "UPDATE segurancaofensiva SET texto='$t', imagem='$img' where nmr=$n";

if ($_FILES['imagem']['name']!=='' && $connect->query($sql) !== false )
    {
        if ($n <= $count) {
            echo "actualizou\n\n";
            var_dump($n);
            var_dump($i);
            var_dump($image);
        }
        else
        {
            echo "nao atualizou numero fora dos limites";
        }

    } else {
        if ($connect->query($sql1) !== false){
            echo "atualizou\n\n";
        } else {
            echo "errp";
        }
    }
    $connect->close();

    ?>
<?php include 'connection.php'; ?>
<?php 

$campo = $_POST['selected'];
$query = "SELECT campo FROM segurancaofensiva";
$result1 = mysqli_query($connect, $query);
$stored = $campo;

$obterquery = "SELECT * FROM segurancaofensiva where campo ='$campo'";
$x = $connect->query ($obterquery) or die ("Erro na variavel resultado");
$final = $x->fetch_array (MYSQL_ASSOC);

?>

<html>
<body>
<head>
<link rel="stylesheet" type="text/css" href="css/styleBO.css">
</head>
<div class="formulario" style="width: 100%; height: 100%;">
<form name="form2" method="POST" action="">
<h6>Campo:</h6>  <select name ="selected" id="selected" >
            <?php while($row1 = mysqli_fetch_array($result1)):;?>
            <option value="<?php echo $row1[0];?>"><?php echo $row1[0];?></option>
            <?php endwhile;?>
        </select>
<input type="submit" id="load" class="load" name="load" value="Carregar">
<input type="hidden" name="selectedValue" value="0"/><br>
</form>
</div>
<div class="formulario" id="form2" style="width: 100%; height: 100%;">

<form name="form1" target="apresenta" method="POST" action="menu3.php">
<label> Atualizar dados </label><br>
<h6>Texto:</h6><textarea name="texto" id="texto"><?php echo htmlspecialchars($final['texto']);?></textarea><br>
<h6>Imagem:</h6><input type="file" name="imagem"><br>
<input type="hidden" value="<?php echo htmlspecialchars($final['nmr']);?>" name="num">
<input type="submit" name="submit" value="Enviar" class="topo">
<input type="reset" value="Limpar" class="topo">
</form>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

更改表单html,如下所示。如果没有这个,PHP将无法检测文件对象。

<form enctype="multipart/form-data">

根据您的要求添加表单属性。

答案 1 :(得分:0)

您在表单标记

中缺少enctype属性
enctype='multipart/form-data'

如果您发布数据并尝试上传文件,则必须使用enctype