问题陈述:我使用的是斯威夫特和PHP POST请求上传图片中的MySQL数据库。 我能够在数据库中插入选定的图像。 但无法显示。
MySQL表格式:
现在我显示从数据库这个图片到我的本地主机。 这使我产生如下...
斯威夫特文件:
@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);
?>