无法将图像上传到mySQL数据库

时间:2016-06-17 16:26:53

标签: php html mysqli

仍然是编码的新手。几乎不知道我在做什么。我试图制作一个PHP页面,让我上传和查看图像。不知道出了什么问题。我试图尽可能正确地做到这一点。有人可以帮帮我吗?



<!doctype html>
<html>
<head>
</head>
<body>
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected ";
$imagename= mysqli_real_escape_string($_FILES['image'] ['name']);
$imagefile =mysqli_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$qry = "INSERT INTO image (name,file) VALUES ('$imagename','$imagefile')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
	$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo "$currimage <br/>";
}
?>
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

为了能够捕获post变量,您需要提交表单并处理操作。您的代码的第一个问题是您的表单不完整 - 它缺少结束标记。第二件事,为了能够通过帖子发送文件,你需要多部分表格。您应该添加lat = [] long = [] for l in L : lat.append(l[0]) long.append(l[1]) sum(lat)/len(lat) sum(long)/len(long) -74.07461283333332, 40.76800886666667 作为表单的属性。 所以,而不是

enctype="multipart/form-data"

你需要

 <form method="post">
 <input type="file" name="image"></input>
 <input type="submit" name="submit" value="upload"></input>

答案 1 :(得分:0)

您必须将上传的图像移动到服务器 试试这段代码 - 创建目录/上传/

  <!doctype html>
    <html>
    <head>
    </head>
    <body>
    <form method="post" enctype="multipart/form-data">
    <input type="file" name="image"></input>
    <input type="submit" name="submit" value="upload"></input>
    <?php
    if(isset($_POST['submit']))
    echo "button has been clicked";
    $con = mysqli_connect("127.0.0.1","root","","demo");
    if(!$con)
    echo "didnt connect to database ";
    else echo "connected";
$uploads_dir = '/uploads';
 $tmp_name = $_FILES["image"]["tmp_name"];
        $name = $_FILES["image"]["name"];
  move_uploaded_file($tmp_name, "$uploads_dir/$name");
    $qry = "INSERT INTO image (name,file) VALUES ('$name','$tmp_name')";
    $result = mysqli_query($con,$qry);
    if($result)
    echo "image has been uploaded";
    viewimage();
    function viewimage()
    {$recon = mysqli_connect("127.0.0.1","root","","demo");
        $view = "SELECT * FROM image ";
    $data =mysqli_query($recon,$view);
    $res2 =mysqli_fetch_assoc($data);
    $currimage =$res2['file'];
    echo '<img src="'.$currimage.'" /> <br/>';
    }
    ?>
    </body>
    </html>