Home » Php » i tried atli’s manual and the picture is not appearing? – PHP

i tried atli’s manual and the picture is not appearing? – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

here’s the code for retrieving the info in the pic..but the info in filedata only showing a set of strings..


<?php
$Link = mysqli_connect("localhost","root","tupi", "amyak_maleh");
if(mysqli_connect_errno()){
die("Mysql connection failed:".mysqli_connect_errno());
}
//$result1 = mysqli_query($Link, "SELECT filedata FROM filestorage ORDER BY form_no DESC LIMIT 1");
$result1 = mysqli_query($Link, "SELECT fileid, filename, filemime, filesize, created, filedata from filestorage");
if($result1)
{
if(mysqli_num_rows($result1) == 0){
echo "<p> there are no files in the database</p>";
}
else
{
echo "<table width = '100%'><tr>";
echo "<td><b>Name</b></td>";
echo "<td><b>Mime</b></td>";
echo "<td><b>Size(bytes)</b></td>";
echo "<td><b>Created</b></td>";
echo "<td><b>Filedata</b></td>";
while($row = mysqli_fetch_array($result1))
{
echo "<tr><td>".$row['filename']."</b></td>";
echo "<td>".$row['filemime']."</b></td>";
echo "<td>".$row['filesize']."</b></td>";
echo "<td>".$row['created']."</b></td>";
echo "<td><input type=\"image\" src=".$row['filedata']."/></b></td></tr>";
}
echo "</table>";
}
mysqli_free_result($result1);
}
else
{
echo "error! sql query failed";
echo "<pre>".$link->error."</pre>";
}
mysqli_close($Link);
?>

A(Answer):

as for any image in html, the src attribute specifies an URI to the image, not the image data itself.

A(Answer):

how could i convert the strings into an image? what function should i use? to convert it?

thanks

A(Answer):

you’re not getting the point here. the image data belong into a separate file.

// html
<input type="image" src="path/to/image.php"/>
// image.php
// ...
echo $row['filedata'];

if you closely examine Atli’s code samples, you’ll notice that he doesn’t mix (binary) image data with html code.

A(Answer):

i see.. it should be in a different page? ..so that it will be a valid code in src?

A(Answer):

@Paul NIcolai Sunga

the URI of that page

A(Answer):

Here, save this as image.php


header("Content-Type: image/jpeg");
$fileid = isset($_REQUEST['fileid']) ? $_REQUEST['fileid'] : false;
if($fileid)
{
$Link = mysqli_connect("localhost","root","tupi", "amyak_maleh");
if(mysqli_connect_errno())
{
die("Mysql connection failed:".mysqli_connect_errno());
}
$result = mysqli_query($Link, "SELECT filedata from filestorage where fileid={$fileid}");
if(is_resource($result))
{
$row = mysql_fetch_row($result);
echo $row['filedata'];
}
}

Then call:

<input type="image" src="image.php?fileid=YOUR_FILE_ID">

A(Answer):

the result is

Submit Query

why is that?

thanks

A(Answer):

here’s the code, and it appears only a this string: Submit Query


<input type="image" src="image.php?fileid=<?Php $form_no; ?>" />

thanks

A(Answer):

any other suggestions please?

thanks

A(Answer):

where does "Submit Query" come from?

A(Answer):

from the image.php.

thanks

A(Answer):

whats this?when i click the submit query which is the one appeared insted of the image, it jumps to the image.php page then this is what appears:

http://localhost/image.php?x=&y=

A(Answer):

@Dormilich

I mean, where dou you define such a string (which line of code causes that to appear)?

A(Answer):

this is the code..


<form action="image.php" method="post">
<input type="image" src="image.php?form_no=<?Php echo $form_no; ?>" />
</form>

A(Answer):

well, it does not explain where the string comes from, but it explains the strange submit.

obviously image.php is designed to be an image (representation), thus if you put it in your form’s action it will be executed onsubmit (say, if you submit the form, you’ll be returned an image (which is possible, but doesn’t make sense)). solution: put the form handling script in the action attribute.

A(Answer):

solution: put the form handling script in the action attribute.

what exactly i will put in the action attribute?

A(Answer):

@Paul NIcolai Sunga

…………………

the script (resp. its file name) that processes the form data (may be the same as the current page)

A(Answer):

what shall i do? it is still not working..

A(Answer):

what is not working, the form or the image?

A(Answer):

the image is not showing.. i tried to show it through the src itself and its okei but when the image that came from the database it is not showing,.. maybe the problem is in the database or in the code, but there is no sign error in the codes.

A(Answer):

ok, then you have to make sure that the picture is saved correctly in the DB . there are some MySQL GUIs out, but unless you have a Mac I can’t recommend anything.

(If that’s possible I could try connecting to your DB and look myself)

A(Answer):

Here’s one I use… SQLYog

A(Answer):

@Canabeez

does it show you any pictures?

A(Answer):

i tried another code:
here’s the code. it only displays a blank photo. i mean an x that is usually seen in a blank photo..

<?php
$Link = mysqli_connect("localhost","root","tupi", "amyak_maleh");
if (!$Link)
{
trigger_error("Could not connect", E_USER_ERROR );
}
$result=mysqli_query($Link, "SELECT * FROM pix");
while($row=mysqli_fetch_assoc($result))
{
header("Content-type: image/jpeg");
echo "<img src=\"".$row['imgdata']."\" alt=\"".$row['title']."\" width=\"100\" length=\"100\"/>";
}
?>

A(Answer):

@Paul NIcolai Sunga

this is completely wrong, the src attribute has to be an url!

A(Answer):

I’ll give you a working example

HTML

<h4>
<a class="hook" id="mord">Mord an Bord</a>
</h4>
<p>11. Januar 2009, SachsenSonntag</p>
<div class="mitte">
<img src="img.php?iid=sas091" width="150" height="99" alt="Bild des Zeitungsartikels" title="Wohl bekomm’s! Ein Schlückchen zur Begrüßung der Passagiere" id="img_sas091"/>
</div>

img.php

<?php
require 'load.img.php';
$img = new ArticleIMG;
$img->display();
?>

methods

public function display()
{
if (!$this->requestImage())
{
exit;
}
if (!empty($this->DBimg['img_mime']) and !empty($this->DBimg['img_size']) and !empty($this->DBimg['img_data']))
{
header('Content-Type: ' . $this->DBimg['img_mime']);
header('Content-Length: ' . $this->DBimg['img_size']);
echo $this->DBimg['img_data'];

}
}
protected function requestImage()
{
$this->load();
if ($this->checkID($this->IID))
{
return $this->getImageData();
}
return false;
}
private function getImageData()
{
try
{
$PS = aDB::execute('img', array(':id' => $this->IID));
$this->DBimg = $PS->fetch(PDO::FETCH_ASSOC);
}
catch (PDOException $pdo)
{
ErrorLog::logException($pdo, __CLASS__, __METHOD__);
return false;
}
return true;
}

A(Answer):

is this correct?


$pic = $_FILES['Browse'];
$formid = mysqli_query($Link, "Select form_no from registration_form where last_insert_id() = form_no");
$formnum = mysqli_fetch_row($formid);
$querypic = mysqli_query($Link, "INSERT INTO pix(pid, imgdata) values('$formnum', '$pic')");

i am not certain with this code..

A(Answer):

@Paul NIcolai Sunga

completely unnecessary (have a close look at what you want to do here… ;))

otherwise I can’t tell (too less info)

A(Answer):

okei.. im trying a lot of codes that is why i am so confused.. the issue about the displaying picture from mysql is still unsolved..

A(Answer):

@Paul NIcolai Sunga

so, did you verify that the picture is in the DB (and not corrupted)?

A(Answer):

yeah.. in atli’s code there were files in the DB but when i tried to check my own, there was no files.. im going to start again.. hope i can do it.. i’ll try to fix the codes.. the insert query maybe has an error..
here’s now my code:


<?php
error_reporting (E_ALL ^ E_NOTICE);
$pid = base64_decode($_GET['id']);
if(isset($_FILES['uploaded_file']))
{
if($_FILES['uploaded_file']['error'] == 0)
{$Link = mysqli_connect("localhost","root","tupi", "amyak_maleh");
if(mysqli_connect_errno()){
die("Mysql connection failed:".mysqli_connect_errno());
}
$name = mysqli_real_escape_string($Link, $_FILES['uploaded_file']['name']);
$data = mysqli_real_escape_string($Link, file_get_contents($_FILES ['uploaded_file']['tmp_name']));
$query = "INSERT INTO pix(pid, title, imgdata) values('$pid', '$name', '$data')";
$result = mysqli_query($Link, $query);
if($result)
{
$asd = "Succes! YOur file was successfully added!";
$alert = base64_encode($asd);
header("Location:registration.php?alert=$alert");
exit();
}
else
{
$asd = "Error! An error occurred while the file was being uploaded.
Error code:".$_FILES['uploaded_file']['error'];
$alert = base64_encode($asd);
header("Location:picul.php?alert=$alert");
exit();
}
mysqli_close($Link);
}

it is not inserting..

A(Answer):

@Paul NIcolai Sunga

with this code the data has inserted.. but now im having a problem retrieving the photo..

A(Answer):

i used this:


<input name="image" type="image" src="asdf.php?imgid=<?Php echo $form_no; ?>" width="110" height="100"/>

asdf.php where the image come from.


error_reporting (E_ALL ^ E_NOTICE);
$id = $_GET['imgid'];
$Link = mysqli_connect("localhost","root","***", "***");
if (!$Link)
{
trigger_error("Could not connect", E_USER_ERROR );
}
if(!isset($id) || empty($id)){
die("Please select your image!");
}else{
$query = mysqli_query($Link, "SELECT * FROM pix WHERE pid='$id' ");
$row = mysqli_fetch_assoc($query);
$content = $row['imgdata'];
header('Content-type: image/jpg');
echo $content;
}

and i tried this one: it still not showing:


header('Content-Type: image/jpeg');
$id = $_GET['imgid'];
$id = isset($_REQUEST['imgid']) ? $_REQUEST['imgid'] : false;
if($id)
{
$Link = mysqli_connect("localhost","root","***", "***");
if(mysqli_connect_errno())
{
die("Mysql connection failed:".mysqli_connect_errno());
}
$result = mysqli_query($Link, "SELECT imgdata from pix where pid = '$id' ");
if(is_resource($result))
{
$row = mysql_fetch_row($result);
header('Content-Type: image/jpeg');
echo $row['imgdata'];
}
}

the photo is not showing..which do you think the good code should i use and enhance? so that i will not get confused…

thanks a lot…

A(Answer):

help. which code should i use?