Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAris Constantinou
2,687 PointsPHP upload and display images issue
Hi, i am kinda rookie with PHP but i want to create an upload function for images on my website and once the image is uploaded then i want to view the image inside my website.
I watched this Youtube and wrote from start his code but for some reason when i try to upload an image it seems to be working but it wont upload at all, and inside my php admin of my hosting service (one.com) i get this message "MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0004 sec )" By the way, i left the connections parameteres as (localhost,root and "") but in the actual code they are the correct for my database. Code:
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<DOCTYPE html!>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header class="main-header">
<div class="search">
<div class="logo">
<img src="img/nklogo.png"></div>
<div class="searchme">
<form><input type="search" id="name" name="name" placeholder="Search" autofocus autocomplete="on">
</form>
</div>
</div>
<div class="main-content">
<div class="primary col">
<h1>Today Jobs</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Sync"/>
</div>
</div>
<div class="secondary col">
<h1>Upcoming Jobs</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Send"/>
</div>
</div>
<div class="third col">
<h1>To-do list</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Send"/>
</div>
</div>
</div>
<div class="sub-content">
<div class="primary col">
<h1>Draw</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Sync"/>
</div>
</div>
<div class="secondary col">
<h1>Sync</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Sync"/>
</div>
</div>
<div class="third col">
<h1>Backup</h1>
<img src="img/database.png">
<form method="post" enctype="multipart/form-data">
<br/>
<input type="file" name="image" />
<br/><br/>
<div class="button">
<input type="submit"name="submit" value="Upload"/>
</div>
</form>
<?php
if(isset($_POST['submit'])){
if(getimagesize($_FILES['image']['tmp_name'])){
echo "Please select an image.";
}
else{
$image=addslashes($_FILES['image']['tmp_name']);
$name=addslashes($_FILES['image']['name']);
$image=file_get_contents($image);
$image=base64_encode($image);
saveimage($name,$image);
}
}
displayimage();
function saveimage($name,$image){
$mysql_hostname="localhost";
$mysql_user="root";
$mysql_password="";
$mysql_database="";
$bd=mysql_connect($mysql_hostname,$mysql_user,$mysql_password)or die("Bad Connection");
mysql_select_db($mysql_database,$bd)or die("Bad Connection");
$qry="insert into images (name,image) values ('$name','$image')";
$result=mysql_query($qry,$bd);
if($result){
echo "<br/>Image Uploaded.";
}
else{
echo "<br/>Image not Uploaded.";
}
}
function displayimage(){
$mysql_hostname="localhost";
$mysql_user="root";
$mysql_password="";
$mysql_database="";
$bd=mysql_connect($mysql_hostname,$mysql_user,$mysql_password)or die("Bad Connection");
mysql_select_db($mysql_database,$bd)or die("Bad Connection");
$qry="select * from images";
$result=mysql_query($qry,$bd);
while($row = mysql_fetch_array($result)){
echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
}
mysql_close($bd);
}
?>
</div>
</div>
</header>
</body>
</html>