【发布时间】:2018-06-08 07:31:53
【问题描述】:
问题说明:我正在使用 Swift 和 PHP 的 POST 请求在 MYSQL 数据库中上传图像。我能够在数据库中插入选定的图像。但无法显示。
MYSQL 表格式:
现在我将这些图像从数据库显示到我的本地主机。这给了我如下结果......
Swift 文件:
@IBAction func uploadOnServer(sender: AnyObject)
{
if Description.text == "" || imageView.image == nil
{
}
else
{
let img:UIImage = imageView.image!
let request = NSMutableURLRequest(URL:NSURL(string: "http://localhost/RESTAPI/UploadImage.php")!)
request.HTTPMethod = "POST"
let postString = "img=\(img)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{
data, response, error in
print("response =\(response)")
if error != nil
{
print("error=\(error)")
return
}
}
task.resume()
Description.text = ""
imageView.image = nil
}
}
PHP 文件:
<?php
$host = "localhost";
$username = "scott";
$password = "tiger";
$dbname = "mydb";
$link = mysqli_connect($host, $username, $password, $dbname);
$arr = array();
if($link == true)
{
//Displaying images from mysql
$select_image="select * from images";// where id=1";
$var=mysqli_query($link,$select_image);
echo "<table>";
while($row=mysqli_fetch_array($var))
{
//$desc = $row["Description"];
$img = $row['image'];
echo "<tr>";//<td><b>$desc</b></td>";
echo "<td><img src = '$img' width = 100 height = 100></td></tr>";
}
}
else
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$img = $_REQUEST['img'];
$sql1 = "INSERT INTO images (image) VALUES ('$img')";
if(mysqli_query($link, $sql1))
{
echo "Image added successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql1.mysqli_error($link)";
}
}
mysqli_close($link);
?>
【问题讨论】:
-
您正在将原始图像文件输出到 img 标签...
-
请帮助我应该在哪里进行更改。