为什么我的图像没有插入数据库 [英] Why is my images not inserting into the database
本文介绍了为什么我的图像没有插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨所有
i必须插入我的数据库然后显示但由于某种原因它没有插入数据库可以告诉我为什么。
这是我的目标
有人可以看看我的代码并告诉我出了什么问题以及我错过了什么。
我的尝试:
< pre > <? php
$ db_host = ' 本地主机'跨度>;
$ db_username = ' root';
$ db_password = ;
$ con = mysqli_connect($ db_host,$ db_username,$ db_password)或 die(mysqli_connect_error());
mysqli_select_db($ con,' food')或 die(mysqli_error($ con));
if(isset($ _ POST [' submit'])&& isset( $ _GET [' img_id']))
{
$ sql = < span class =code-string> SELECT * FROM tbl_images WHERE img_id = {$ img_id};
$ result = mysqli_query($ con,$ sql)或 die( 错误: .mysql_error($ con));
$ rowcount = mysqli_num_rows($ result);
}
?>
< html >
< ; 正文 >
< 表格 < span class =code-attribute> method = post enctype = multipart / form-data >
< br / >
< 输入 类型 = 文件 名称 = image >
< br / > < br / >
< input type = 提交 名称 = 提交 value = upload >
< / form >
<? php
if(isset($ _ POST [ ' submit'])&& isset($ _ FILES [' file']))
{
if( getimagesize($ _ FILES [' image'] [' tmp_name'])== false)
{
echo < span class =code-string> 请选择图像;
}
else
{
$ name = addslashes($ _ FILES [' image'] [' 名称跨度>]);
$ image = base64_encode(file_get_contents(addslashes($ _ FILES [' image'] [< span class =code-string>' tmp_name'])));
saveimage($ name,$ image);
}
}
displayimage();
函数saveimage($ name,$ image)
{
$ db_host = ' 本地主机跨度>;
$ db_username = ' root';
$ db_password = ;
$ con = mysqli_connect($ db_host,$ db_username,$ db_password);
mysqli_select_db($ con,' food')或死(mysqli_error($ con)) ;
$ query = INSERT INTO tbl_images(name,image)value('$ name,'$ image' )跨度>;
$ result = mysqli_query($ con,$ query);
if($ result)
{
echo < br />图片上传;
}
其他
{
echo < br />图片不上传;
}
}
函数displayimage() // 此函数用于显示db中的图像
{
$ db_host = ' 本地主机'跨度>;
$ db_username = ' root';
$ db_password = ;
$ con = mysqli_connect($ db_host,$ db_username,$ db_password);
mysqli_select_db($ con,' food')或 die(mysqli_error($ con));
$ query = SELECT * FROM tbl_images; // 这是选择图像的地方
$ result = mysqli_query($ con,$ query);
while($ row = mysqli_fetch_array($ result))
{
echo ' < img hieight =250width =250src =data:image; base64,'。$ row [2]。' >';
}
mysqli_close($ con);
}
?>
< / body >
< / html >
解决方案
db_host = ' localhost';
db_username = ' root';
< blockquote> db_password = ;
hi all
i have to insert into my database and then display but for some reason it is not inserting into the database can someome tell me why.
here is what I hve
can someone take a look at my codes and tell me what is wrong and what I am missing.
What I have tried:
<pre><?php
$db_host='localhost';
$db_username='root';
$db_password="";
$con=mysqli_connect($db_host,$db_username, $db_password) or die (mysqli_connect_error());
mysqli_select_db($con, 'food') or die (mysqli_error($con));
if(isset($_POST['submit']) && isset($_GET['img_id']))
{
$sql= "SELECT * FROM tbl_images WHERE img_id={$img_id}";
$result=mysqli_query($con, $sql) or die("Error:" .mysql_error($con));
$rowcount=mysqli_num_rows($result);
}
?>
<html>
<body>
<form method="post" enctype="multipart/form-data" >
<br/>
<input type="file" name="image">
<br/><br/>
<input type="submit" name="submit" value="upload">
</form>
<?php
if(isset($_POST['submit'])&& isset($_FILES['file']))
{
if(getimagesize($_FILES['image']['tmp_name'])== false)
{
echo "Please select an image";
}
else
{
$name=addslashes($_FILES['image']['name']);
$image=base64_encode(file_get_contents(addslashes($_FILES['image']['tmp_name'])));
saveimage($name, $image);
}
}
displayimage();
function saveimage($name,$image)
{
$db_host='localhost';
$db_username='root';
$db_password="";
$con = mysqli_connect($db_host, $db_username, $db_password);
mysqli_select_db($con, 'food')or die(mysqli_error($con));
$query="INSERT INTO tbl_images(name,image) value('$name, '$image')";
$result=mysqli_query($con, $query);
if($result)
{
echo "<br/> Image Upload";
}
else
{
echo "<br/> Image not Upload";
}
}
function displayimage()//this function is used to display the images from the db
{
$db_host='localhost';
$db_username='root';
$db_password="";
$con = mysqli_connect($db_host, $db_username, $db_password);
mysqli_select_db($con, 'food') or die (mysqli_error($con));
$query="SELECT * FROM tbl_images";//this is where images will be selected
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result))
{
echo'<img hieight="250" width="250" src="data:image;base64, '.$row[2]. '">';
}
mysqli_close($con);
}
?>
</body>
</html>
解决方案
db_host='localhost';
db_username='root';
db_password="";
这篇关于为什么我的图像没有插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文