when implement this code i have no image
<?php
include('confing.php');
echo '<img src="getImage.php?id=2" >';
?>
file => getImage
<html >
<body>
<?php
include('confing.php');
$id = $_GET['id'];
$sql="SELECT * FROM images WHERE id=$id ";
$result=mysqli_query($connection,$sql);
$row = mysqli_fetch_assoc($result);
// Set the content type header - in this case image/png
header("Content-Type: image/png");
echo $row['content'] ;
?>
body>
html>
body
html
body>
html>
body
html
body>
html>
body
html
The errors you are getting:
Undefined index ID: It would appear that you are calling it without passing the ID argument. Obviously you should check that. But also, you should add some error checking to your code to ensure that it doesn't crash if the ID is not passed, or if the ID is invalid.
Undefined variable $connection: Your database connection variable is not set. Maybe you config.inc include doesn't actually set the connection? Or maybe you've got the variable name wrong? You need to check your config.inc to find out why this is happening. In any case, this leads directly onto the next error...
mysqli_query expects connection parameter: Because the $connection variable is not set, your DB query can't be run. Some error checking here would be helpful, even if you do sort out the connection variable, as there may be other reasons the connection may fail.
mysqli_fetch_assoc expects result parameter: This occurs because the result variable is not set due to the query not being run. You should add some error checking for this as well, since there may be other reasons why a query fails to run.
The errors didn't even get as far as hitting the header() function call, which would also fail because the program has already output some content. You should remove the <html><body> tags from the top of the program, as they are not necessary for outputting an image; in fact, they would cause the image to be invalid.
Your line:
echo '<img src="getImage.php?id=2">';
will output this into your html page:
<img src="getImage.php?id=2">
Second, it will not load, execute and insert the result of getImage.php.
Here's a solution:
First page:
<?php
include('getImage.php');
$id = $_GET['id'];
$img = getImage($id);
echo "<img src='$img' >";
?>
getImage.php:
<?php
include('confing.php');
function getImage($id){
$sql="SELECT * FROM images WHERE id=$id ";
$result=mysqli_query($connection,$sql);
$row = mysqli_fetch_assoc($result);
return $row['content'] ;
}
?>
Related
Case :
Hi all, actually i use POST and GET method in many times and they're work fine. But this time it isn't. I use POST TYPE form and then do PHP While (Looping) to generate some HTML attribute, i also give name to these HTML attribute (like input field etc). But second script (different page name), just won't detect it. It's like im using 1.php (where POST method applied) and 2.php (where GET method applied).
Experiment :
My experiment with code is like this basically (just to show logic, not the actual code).
1.php
<?php
echo <form method="POST" action="2.php">
while (fetch) {
echo fetch
}
echo </form>
?>
2.php
<?php
$x = $GET['id'] // work fine
$x = $GET['html_attribute'] // result : unidentified index
$query = update tbl_transaction set x='$x' where id='$id'
$query -> execute();
?>
Real Code of 2.php
<?php
include 'config/user_session.php';
include 'config/config.php';
if(isset($_GET['id']))
{
$id=$_GET['id'];
$no_ref=$_GET['no_ref'];
$desc=$_GET['desc'];
$amount=$_GET['amount'];
$via=$_GET['slPayment'];
$date=$_GET['date'];
$cat=$_GET['cat'];
$subcat=$_GET['subcat'];
$query1=mysql_query("update tbl_transaksi set no_ref='$no_ref', desc='$desc', amount='$amount', via='$via', date='$date', cat='$cat', subcat='$cat' where id='$id'");
if($query1)
{
echo '<script language="javascript">';
echo 'alert("Transaction Edited.")';
echo '</script>';
echo "<script>setTimeout(\"location.href = 'edit_transaction.php';\",100 </script>";
}
}
?>
I tried to change GET with POST, the result still same. Unidentified Index.
Desired Output :
I want 2.php GET Function return value from 1.php attribute value. Form method already POST.
Where did i do wrong ? What code i should modify ?
Thank you Stackoverflow Community.
I researched here in stackoverflow trying to find whether someone is also encountering the same problem. I know it's kind of easy and even I really don't know what's the error because there's no problem with my query.
On the previous page, here's my code to retrieve the ID Number so I'll be able to select the data with that ID number:
<?php echo $row['place_name'];?>
I tried first to print the value of the place id and it works fine.
But when it was being called to the Package page, the data I want to show weren't displayed.
I look at the URL and it shows this after the package.php
place_id=
I don't know why it is blank, please check my code if there's missing or just wrong.
In my package page, here's the PHP code:
<?php
include("common/connect.php");
$place_id = $_GET['place_id'];
$result = mysql_query("SELECT * FROM package_items WHERE place_id = '$place_id'");
$row1 = mysql_fetch_array(mysql_query("SELECT place_name FROM packages WHERE place_id = '$place_id'"));
if($result === FALSE) {
die(mysql_error()); // for better error handling
}
?>
In HTML Code:
<h1><?php echo $row1['place_name'];?></h1>
<?php while($row=mysql_fetch_array($result)) {?>
<?php echo $row['item_title'];?>
<br>
Back
<?php } ?>
Please check my codes. Thanks.
You are not printing it.
Change
<?php $row['place_id'];?> // It will output nothing as no echo or print.
To
<?php echo $row['place_id'];?>
Rest of the code looks fine.
Three suggestions:
1)
$place_id = $_GET['place_id'];
Change to
$place_id = ! empty($_GET['place_id']) ? $_GET['place_id'] : ''; // To avoid any warning.
2) Don't feed variable from $_GET or $_POST to any SQL.
3) Don't use mysql_ functions as they are deprecated and will be remove in future versions of PHP.
I have a page structured like below and I am having issues getting the redirect to work. This page gets an ID from the URL and uses it in the query. If there isn't a match, just redirect to another page.
I am getting the "headers already sent error," due to the include. I need the include to be there regardless. Is there a different way I can do the redirect if the query result is empty?
include('somepage.php');
$id = $_GET['id'];
$query = mysql_query("My query is here");
if(mysql_num_rows($query)==0) { header('Location:htp://example.com'); }
I've tried using exit(); and the various stop processing functions.
somepage.php:
<html>
<head>
(standard html)
include('sql-connect.php');
</head>
<body>
(code to format the header portion of the site)
You can put ob_start() at the beginning of the file, so that it looks like this:
<?php
ob_start();
include 'somepage.php';
$id = $_GET['id'];
$query = mysql_query("My query is here");
if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }
Also, you can echo html redirect:
<?php
if(mysql_num_rows($query)==0) { echo '<meta http-equiv="refresh" content="0; url=http://example.com/">'; die(); }
You need to add ob_start() at the beginning of the file and if this still does not work then you also need to add ob_flush(); to fully flush out the old header.
flush(); // Flush the buffer
ob_flush();
header("Location: http://example.com");
You may have a blank line after a closing ?> This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls. Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.
ob_start() may fix your problem.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
"headers already sent error" means that your script (somepage.php) already sent headers to the browser, so you can't change these headers and redirect user to the other URL.
The best solution is to move the "include" operator after your check:
$id = $_GET['id'];
$query = mysql_query("My query is here");
if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }
include('somepage.php');
Also, you can prevent your somepage.php from sending any data to the client.
The second variant is to use the functions ob_start(), ob_get_contents(), ob_end_clean():
ob_start();
include('somepage.php');
$id = $_GET['id'];
$query = mysql_query("My query is here");
if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }
$content = ob_get_contents():
ob_end_clean();
echo $content;
I have found a lot of posts that describe the same problem, but everything I tried failed.
For a profile page I am making people can upload 4 pictures. When no picture is available I want to show a default picture. My complete code is like this now:
$id = $_GET['ID'];
$link = mysql_connect("xxx", "xxx", "xxx");
mysql_select_db("xxx");
$sql = "SELECT * FROM profielen WHERE ID = $id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
if ($row['Foto4'] == NULL){
//echo '<img src="/img/noImage.jpg" />';
echo $row['Foto5'];
}
else {
echo $row['Foto4'];
}
In this form the code works, so I know that the if-statement is correct.
When I try to uncomment the commented line, it shows a broken link as the image.
I have tried with double quotes and then escape the double quotes within the img tag. I have also tried to call it as a variable and without a / before the img-path. And also just tried to say echo "No image", but it seems that if I do anything else than echo $row[x] it just does not work. When I try to display the image in HTML it works fine, so the name of the file is correct.
I am running out of ideas, so maybe someone can help?
The problem with your code is that you're sending HTML to embed an image back, when really, you should be sending image data back. You can solve this by redirecting the user to the "noImage.jpg", in this way the user will get an image back from the request and all will be fine :)
Code for this could look like this:
if ($row['Foto4'] == NULL){
header('Location: /image/noImage.jpg');
} else {
echo $row['Foto4'];
}
If you use the content type image/jpeg, the browser expects the raw binary data of an image. Instead, you give it HTML-code.
You can instead use the Location-header, which tells the browser to open another address.
header("Location: /img/noImage.jpg");
Remember that you cannot output any other data or write any other headers if you use this method.
I have a problem with 'setcookie' in PHP and I can't solve it.
so I receive this error "Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\VertrigoServ\www\vote.php:14) in C:\Program Files\VertrigoServ\www\vote.php on line 86"
and here is the file..
line 86 is setcookie ($cookie_name, 1, time()+86400, '/', '', 0);
is there any other way to do this ??
<html>
<head>
<title>Ranking</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#EEF0FF">
<div align="center">
<br/>
<div align="center"><div id="header"></div></div>
<br/>
<table width="800" border="0" align="center" cellpadding="5" cellspacing="0" class="mid-table">
<tr><td height="5">
<center>
</tr>
</table>
</center>
</td></tr>
<tr><td height="5"></td></tr>
</table>
<br/>
<?php
include "conf.php";
$id = $_GET['id'];
if (!isset($_POST['submitted']))
{
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
</div></td></tr>
<tr><td align="center" valign="top"><img src="images/ads/top_banner.png"></td></tr>
</table>
</form>
<?php
}
else
{
echo '<font color="red">You must select a valid server to vote for it!</font>';
}
}
else
{
$kod=$_POST['kod'];
if($kod!=$_COOKIE[imgcodepage])
{
echo "The code does not match";
}
else
{
$id = mysql_real_escape_string($_POST['id']);
$query = "SELECT SQL_CACHE id, votes FROM s_servers WHERE id = $id";
$result = mysql_query($query) OR die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$votes = $row['votes'];
$id = $row['id'];
$cookie_name = 'vote_'.$id;
$ip = $_SERVER['REMOTE_ADDR'];
$ltime = mysql_fetch_assoc(mysql_query("SELECT SQL_CACHE `time` FROM `s_votes` WHERE `sid`='$id' AND `ip`='$ip'"));
$ltime = $ltime['time'] + 86400;
$time = time();
if (isset($_COOKIE['vote_'.$id]) OR $ltime > $time)
{
echo 'You have already voted in last 24 hours! Your vote is not recorded.';
}
else
{
$votes++;
$query = "UPDATE s_servers SET votes = $votes WHERE id = $id";
$time = time();
$query2 = mysql_query("INSERT INTO `s_votes` (`ip`, `time`, `sid`) VALUES ('$ip', '$time', '$id')");
$result = mysql_query($query) OR die(mysql_error());
setcookie ($cookie_name, 1, time()+86400, '/', '', 0);
}
}
}
?>
<p>[Click here if you don't want to vote]</p><br/>
<p>Ranking.net © 2010-2011<br> </p>
</div>
</body>
</html>
Thanks a lot!
You cannot have any output before header() and setcookie() calls.
https://stackoverflow.com/search?q=+headers+already+sent+by
https://stackoverflow.com/tags/php/info
Any output includes any <html> before the openeing <?php marker, or any print or echoing of content. Another culprit is the UTF-8 BOM http://en.wikipedia.org/wiki/Byte_Order_Mark - which most text editors do not show visibly, but confuses PHP when at the beginning of files.
Setting a cookie requires sending a header to the client, and you can't send headers if the output has already started.
You have to put the PHP code before the HTML markup so that you can call setcookie before any output is sent and you also separate PHP code from presentation which you should do anyway.
You should put the cookie code at the top of the page. A better layout would be something like this:
<?php
//include config
//check posted data (included settings cookies)
//set needed variables
?>
<html>
.....
You could also separated the php code and html. This is generally what i do. My uses generally involve a view class or (in the past) smarty. but a quick example would be to add this code at the bottom of the above php code and get rid of the html:
<?php
if(empty($tpl)) {
$tpl = 'index';
}
if(file_exists("template/{$tpl}.tpl.php")) {
include("template/{$tpl}.tpl.php");
}
else {
header('Location: /');
}
?>
YOu would need to create a directory called 'templates' and add the html code to files that end in the .tpl.php extension.
Really they are just php pages, but the .tpl. part help you remember that its just mark up.
Make them php pages (not html) so you can output variables
Then in your varios parts of your code above you would set $tpl to be the template you want to load.
This is a just a base bit of code, but it should give you a general idea on how to separate this data. THe main idea is that all html and text will be outputted "after" all programming code has been done.
What you need to do is to create a nice buffer to hold all the headers in until you are down processing. ob_start does the job for you and here is a reference to it. http://php.net/manual/en/function.ob-start.php
When you are finished loading all the different headers use ob_get_contents like this
$text = ob_get_contents();
echo($text);
ob_end_clean();
I hope this helps.
You can not use PHP to set a cookie after any thing has been outputted, and the html before the first php tag does count as output. to keep to a purely php method you'd have to move the whole part about determining what to put int he cookie and setting it up to the very top.
or what I do in situations where that would require to much extra work to do it that way is to have php echo out the JavaScript code to set a cookie. now if you make or get a nice JS cookie setting function and either embed it or link it into the page. then all you have to do is have php echo the function call with the proper data in it at that point. then when the page loads while php still will not set the cookie, but when the browser when it runs the js code it will. and so you get what you want the cookie is set. and you did not have to move the stuff up to the top.