Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何拍摄已上载到数据库的图像,并将其显示在浏览器的表格中,其他行和列中包含文本_Php_Mysql_Image_Mysqli - Fatal编程技术网

Php 如何拍摄已上载到数据库的图像,并将其显示在浏览器的表格中,其他行和列中包含文本

Php 如何拍摄已上载到数据库的图像,并将其显示在浏览器的表格中,其他行和列中包含文本,php,mysql,image,mysqli,Php,Mysql,Image,Mysqli,为了解决我的问题,我一直在寻找答案。但是找不到任何解决我问题的方法。我要做的是把图片和文字上传到数据库。上传工作正常,图像作为二进制数据存储在blob变量中。问题是,我想在一个表中显示文本和图像,作为动态html页面的一部分 我将所有文本正确加载到页面,但由于缺乏更好的描述,图像仅显示为空白方块。如果你有什么想法,我完全赞成。这是所有代码 <form action="http://notactualsite/upload.php" method="post" enctype="multip

为了解决我的问题,我一直在寻找答案。但是找不到任何解决我问题的方法。我要做的是把图片和文字上传到数据库。上传工作正常,图像作为二进制数据存储在blob变量中。问题是,我想在一个表中显示文本和图像,作为动态html页面的一部分

我将所有文本正确加载到页面,但由于缺乏更好的描述,图像仅显示为空白方块。如果你有什么想法,我完全赞成。这是所有代码

<form action="http://notactualsite/upload.php" method="post" enctype="multipart/form-data">
<table border="0">
<tr>
  <td>Select image to upload: </td>
<td><input type="file" name="image" id="image"></td>
</tr>
<tr>
<td>Where is this place?: </td>
<td><input type="text" name="destination" id="destination"></td>
</tr>
<tr>
<td>Why this place?: </td>
<td><input type="textbox" name="reason" id="reason"></td>  
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Upload" name="submit"></td>        
</tr>
</table>
</form>
现在是问题代码

$servername = "";
$username = "";
$password = "";
$database = "";
$port = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database, $port);

if ($conn) {

//echo "<h1>TESTING</h1>";

// Check connection
if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM place2go";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // create table structure
 // output data of each row: a single player with picture and name
while($row = $result->fetch_assoc()) {

echo "<h3>";
echo $row["placeName"]. "</h3>";
 echo "<table border=0>";
  echo "<tr>";
   echo "<td><img src='" . $image . " ' width='50%' /></td>";
   echo "<td valign=top style='font-size:10pt'>";
   echo $row["placeWhy"]. "</td>";
 echo "</tr>";
echo "</table>";    
}  
}
mysqli_close($conn);
}
?>

我建议您使用文件系统,并检查该文件是否确实是图像文件,但如果您确实希望将图像保存为二进制文件,请使用以下方法,而不是:


不将图像存储在db中的原因之一是,不将路径存储在db中,而将提示上的文件存储在名称文件系统中,或者如果必须将图像存储在db中,虽然您不应该调用从db获取图像内容的PHP脚本,并使用header函数将内容类型更改为图像的内容类型,而不是将图像保存到db,但请将其移动到某个位置并保存对该图像的引用。在其名称中添加时间戳和随机字符串或其他内容,以使名称唯一,将其移动到某个位置并保存引用。有关Kaleb Klein答案的详细信息,请参阅此答案。[更改检索到的图像标题][1][1]:我使用的网站是weebly,据我所知,该网站不支持存储信息。如果是这样的话,我可以把它改成上传到那里。今天晚些时候我会尝试大家的建议,并让你知道它是如何工作的。不要在blob上使用mysqli_real_escape_字符串,它不是stringmysqli_real_escape_字符串是二进制安全的-文档说明:如果要插入二进制数据,必须使用此函数。如果不使用绑定值,则所有字符串(无论是否由用户提供)都需要转义。但这超出了这个问题的范围。啊,好吧,我不知道,我从来没有在数据库中存储过二进制数据
$servername = "";
$username = "";
$password = "";
$database = "";
$port = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database, $port);

if ($conn) {

//echo "<h1>TESTING</h1>";

// Check connection
if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM place2go";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // create table structure
 // output data of each row: a single player with picture and name
while($row = $result->fetch_assoc()) {

echo "<h3>";
echo $row["placeName"]. "</h3>";
 echo "<table border=0>";
  echo "<tr>";
   echo "<td><img src='" . $image . " ' width='50%' /></td>";
   echo "<td valign=top style='font-size:10pt'>";
   echo $row["placeWhy"]. "</td>";
 echo "</tr>";
echo "</table>";    
}  
}
mysqli_close($conn);
}
?>
$image = mysqli_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));