I started programming PHP. Until I'm enjoying, but for now things are still a bit confused.
I can record and display images from the database. However I'm trying to do a search through a dropdown, but I am not able to show anything.
Could you possibly help me saying where I am failing please?
If in test2.php I do a search to the last record of the database I can see the image. But if I try to drill through an id of my choice that no longer works.
I apologize for the inconvenience.
Thank you all very much.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<?php
include('config.php');
$query_parent = mysql_query("SELECT idTask from Tasks") or die("Query failed: ".mysql_error());
?>
<body style="background-color:#A4A4A4;">
<?php
if(isset($_POST['search']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '*****';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$idTask = $_POST['idTask'];
$_POST['idTask'] = ctype_digit( (string) $_POST['idTask']) ? (int) $_POST['idTask'] : 0;
$_GET['idTask'] = ctype_digit( (string) $_GET['idTask']) ? (int) $_GET['idTask'] : 0;
echo"<img src='test2.php?image=". $_POST['idTask'] ."'>";
if (!isset($_FILES["image"]["tmp_name"]))
echo "";
else{
$image= addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$image_name = addslashes($_FILES['image']['tmp_name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "That's not an image";
else{
mysql_query("select * from tasks where idTask = '". $_POST['idTask'] ."'");
echo"<img src='test2.php?image=". $_POST['idTask'] ."'>";
}
}
mysql_close($conn);
}
else
{
?>
test2.php
<?php
mysql_connect("localhost", "root", "*****") or die (mysql_error());
mysql_select_db('Database') or die (mysql_error());
$result= mysql_query("select * from tasks where idTask = ". $_GET['idTask']);
if( mysql_num_rows($result) ) {
//No image found...
} else {
$row = mysql_fetch_assoc($result);
$image = $row['image'];
header("Content-type:image/jpeg");
echo $image;
}
?>
When I do a search, a page appears blank.
You've a few mistakes, mainly with array index names.
mysql
query is using ididTask
, which never has a value. (in both files)mysql_query("select * from tasks where idTask = '". $_POST['idTask'] ."'");
$idTask
gets its value from a key with spaces in it, but your form doesn't submit any names with spaces in it.$idTask = $_POST['idTask'];
test2.php
respectively (to use GET
and not POST
)echo"<img src='test2.php?image=". $_POST['idTask'] ."'>";
while
in test2.php
. If you adopt the previous point, change your whole file to become something like<?php
mysql_connect("localhost", "root", "*****") or die(mysql_error());
mysql_select_db('Database') or die(mysql_error());
$result = mysql_query("select * from tasks where idTask = ". $_GET['idTask']);
if( mysql_num_rows($result) ) {
//No image found...
} else {
$row = mysql_fetch_assoc($result);
$image = $row['image'];
header("Content-type:image/jpeg");
echo $image;
}
?>
$_GET
/$_POST
with a simple check$_POST['idTask'] = ctype_digit( (string) $_POST['idTask']) ? (int) $_POST['idTask'] : 0;
$_GET['idTask'] = ctype_digit( (string) $_GET['idTask']) ? (int) $_GET['idTask'] : 0;
base64_encode()
. Then, in test2.php
use;$row = mysql_fetch_assoc($result);
$image = base64_decode($row['image']);
header("Content-type:image/jpeg");
echo $image;
Try this code using ajax it may help you. this is retrieve hero name from the data base to the drop down list.
<?php
mysql_connect("localhost","root","");
mysql_select_db("hero");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function showdetails(str)
{
var xmlhttp;
if (str==0)
{
alert("please select an hero");
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("showresult").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","details.php?id="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
Hero Name:<select name="id" id="id" onchange="showdetails(this.value)">
<option value="0">Select Hero Name</option>
<?php
$sql="select id,name from herophoto";
$qry=mysql_query($sql);
$num=mysql_num_rows($qry);
if($num>0)
{
while($res=mysql_fetch_array($qry))
{
?>
<option value="<?php echo $res['id'];?>"><?php echo $res["name"];?></option>
<?php
}
}
?>
</select>
<div id="showresult" align="justify"></div>
</body>
</html>
and here is the details.php which show you the image what you select from the drop downlist.
<?php
mysql_connect("localhost","root","");
mysql_select_db("hero");
$id = $_REQUEST["id"];
$sql="select * from herophoto where id='$id'";
$qry=mysql_query($sql);
$num=mysql_num_rows($qry);
if($num>0)
{
while($res=mysql_fetch_array($qry))
{
?>
<img src="photo/<?php echo $res['photo'];?>" height="300" width="300"/>
<?php
}
}
?>