Pass Variable Through An Image Hyperlink? - php

how do i hyperlink an image and also pass a variable through it as well? my incorrect code is as follows:
<?php
$result=mysql_query("select serial, name, price from products ORDER BY RAND() LIMIT 3");
while($row=mysql_fetch_array($result))
{
echo '<img src="getImage.php?id=' . $row['serial'] .'"/>'.' '.' ';
}
echo "</tr>";
echo "</table>";
?>
thanks for any help guys

as Book of Zeus said, you have a syntax error:
<?php
$result=mysql_query("select serial, name, price from products ORDER BY RAND() LIMIT 3");
while($row=mysql_fetch_array($result))
{
echo '<img src="getImage.php?id=' . $row['serial'] .'"/>'.' '.' ';
}
echo "</tr>";
echo "</table>";
?>

Related

Displaying 2 records in a column using php

So I have a code
<?php
$showorder = "SELECT order_number FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
$ord = mysqli_fetch_array($orderesult);
?>
in my database customer number 522 has 2 order numbers, when i tried to show the result, it only shows 1.
Here's my other code
echo "<table>";
echo "<th>Order Number</th><th>Order date</th>";
echo "<tr><td>";
echo $ord["order_number"];
echo "</td><td>";
echo $ord["order_date"];
echo "</td></tr>";
You just need to use while() here for getting all records, something like:
while($ord = mysqli_fetch_array($orderesult)){
//echo all value here
}
Also note that, if you want to print $ord["order_date"] than you must need to select column also in your query.
Otherwise, $ord will only contain order_number value.
Your SQL is missing the extra column.
Current SQL:
SELECT order_number FROM orders WHERE customer_number=522
Change to:
SELECT order_number, order_date FROM orders WHERE customer_number=522
Put mysqli_fetch_array($orderesult); in a while loop.
while($ord = mysqli_fetch_array($orderesult)) {
echo $ord["order_number"];
# code
}
Replace your code with the below code and then try again
<?php
$showorder = "SELECT order_number, order_date FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
echo "<table>";
echo "<tr>";
echo "<th>Order Number</th><th>Order date</th>";
echo "</tr>";
while($ord = mysqli_fetch_array($orderesult)) {
echo "<tr>";
echo "<td>$ord['order_number']</td>";
echo "<td>$ord['order_date']</td>";
echo "</tr>";
}
echo "</table>";
?>
echo "<table>";
echo "<th>Order Number</th>";
while($ord = mysqli_fetch_array($orderesult)) {
echo "<tr><td>";
echo $ord["order_number"];
echo "</td></tr>";
}
you must use loop to show all result , and you can use echo one time .
while($ord = mysqli_fetch_array($orderesult)) {
echo "<table>
<th>Order Number</th><th>Order date</th>
<tr><td>".
$ord["order_number"]."
</td></tr>";
}

Diplay All Records, But Distinct?

I'm trying to display all the records in tbInventory, but there are multiple duplicates on the records, specifically under the colItem column. Now what I'm trying to show are all the records in tbInventory but I want it in a way that it will only show an item once if it has a duplicate.
Now my current code displays this:
As you can see, its printing all the items with the colItem values of 'Access Point'. I only want the code to display the item 'Access Point' only once even if it has duplicates.
Now I can't simply use the DISTINCT function as I'm also using the code block for the printing of the data. Here's my current code:
$con=mysqli_connect("localhost","root","","dbDssInventory");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL " . mysqli_connect_error();
}
$result=mysqli_query($con,"SELECT * from tbInventory ORDER BY colItem");
if($result != NULL){
while($row=mysqli_fetch_array($result))
{
echo "<table border=0 style=\"table-layout:fixed;\">";
echo "<tr>";
echo "<td align=left width=15 style=\"word-wrap:break-word;\">";
echo "<div class=\"hover_img\">";
echo " <a href=\"#\">";
echo " <img src='MAGNIFYINGGLASS.png' alt='Point' width='70%'><em>
<object data=" .$row['colImage']. ".PNG width='850%' type=\"image/png\">
<img src=\"default.png\" width='850%'>
</object>";
echo "</td>";
echo "<td align=left width=160 style=\"word-wrap:break-word;\" style=\"word-wrap:break-word;\">";
echo $row['colItem'];
echo "</td>";
$itemname= $row['colItem'];
Is there a way to execute this?
Instead of
$result=mysqli_query($con,"SELECT * from tbInventory ORDER BY colItem");
Try this:
$result=mysqli_query($con,"SELECT * from tbInventory GROUP BY colItem ORDER BY colItem");
You will get the item 'Access Point' only once even if it has duplicates. Hope it helps!

PHP - Image output: strange string

I have to output a series of random images previously saved inside a DB.
At the moment of the output, instead print a picture, the code print a strange string (meaby dumpfile?):
(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢¢’ú~ü±®æIüa¤Äû[SÓÕºàܦ'$·›ØÒ¢°o>)xvÀŸ7ZÓדþULünð®ì.±„ðFíŸÉj=¬íìæöLꨮ6ã㦇êW8b³|Ä⪟ÚL-…Óu¦ÇRmÕ#ÿǪ^"’ûH¯cQô;Ê+ÏeøýC£Ý:ó÷çHÏåTnh[­¿»ÐãRz/Ïýò¦£ë”—_ÌW©Øõ +Écý µIæùltuà¯Údfúgg_´­ÿh1ÿMÓ?O&à¹ü™ùÒXÊ/¯æSÂÔ]H¢¼õi £mœÝ[°ë¹Pãòlþ•¥iñïÂwmµudVÎèd~;qúÖ‘ÄR{IèT_e…‹iñ#Ã÷»|½kKfnŠnQXþ浭ŒFßµ¦´ÖëöÏôpND;W?ÝL…V9à'5ÏSK–GE<,çhŸNîWϺoÇu·\ëW–¹É˜Í ¯ÕÖRºgß½^“Äñ_D¬ºÕ×™ÈÿM‘”þ$°üy¬þ¹…}R]Ot¨n5;{?õ×Åþû…þuóî¦öîònû»,ñ³Ãþ†¨ËtÖŸô €ò“üÿLGþ5/ýÒヿSè¼s¢Û¶×Ö4µoF»ŒZ‚‰:°ùµK^ÿp—þUàªI(eä{z ³œÁyú`U9V5>c¤…T‚7ØÉz®EGץصƒ]Yï|n𼃪+Ù ‘赿41þ«í·î#WÿBÅxŒ­ªmÿH½ó±)_ å³O·˜Jî±ÚÝ6:eb…‰õû£ô¨xéô±_S‡™ì§ãréÚ§# 7’3ÿ‘*´¿­óˆ´é™²Ïqÿ"kËžÂiUzÛ«­rƒóÀþ•Nh$óTIçÎPmn§Àú€ê?J—Œ¨RÂÀõi~:ÜÿË=Y¼oT5Vããn´Ì='O…›§uŸýšòñ£GqüVJ­ü|Óϳ1Ç¥8h&7GjÁ;maíÇ9ÅfñU{”°Ô»_yè—µhŸC·œ€ÿÌ·ôª³üsÔ¤ÿW¨i¬Ýˆ+üëˆ&ÞÏ8¸’>p°R3þî)ñ[­óñ%ño{–eÿЇYªú—õzk¡ÖIñ{W½UªH¥€ÿWj}ú*§7Å#îó5«ÍÃÑZ1ü¿•ssè6¨Û¦U™¾ñÖ }2ÇóÅ8-¸m¶²dÿN¨ÓŒÔ:•V5NšÙÒüGšBÊ××ó?ç´Ÿ úV|þ&þЙn$QÔõÇJlSB]•¡‡o\;·õëS‰]¢†=¸àˆœ¨={ŸåëÞ£ß{²’ŠÙ÷¬Ï»ì3Éè¼ûöÜjIDiá¤Ä«Û|hÌ?J-æ˜Ûå)çæ…rãüõ©Ròt’B­œÿ¬‰8úýj,Ê¿b8šêÕwCco©þ€h¼Ôµgý\kï'ò;³ú~U$º
My code:
<?php
include_once('conn.php');
$n="SELECT COUNT('id_product')
FROM 'products'";
$value=mysql_query($n);
do
{
$selectionASC='SELECT id_product
FROM products
ORDER BY id_product ASC
LIMIT 1';
$selectionDESC='SELECT id_product
FROM products
ORDER BY id_product DESC
LIMIT 1';
$ASC=mysql_query($selectionASC)
or die ('Impossible execute the query <br />').mysql_error();
$DESC=mysql_query($selectionDESC)
or die ('Impossible execute the query <br />').mysql_error();
//____________________________________________________________________
$ASC = mysql_num_rows($ASC);
$DESC = mysql_num_rows($DESC);
$rand_n=rand(($ASC-1),($DESC+1));
//____________________________________________________________________
$selected='SELECT id_product,name, price, img
FROM products
WHERE id_product='.$rand_n;
$selected = mysql_query($selected);
//____________________________________________________________________
while($row=mysql_fetch_row($selected))
{
echo "Product'id: &nbsp"; echo $row[0];
echo '<br />';
echo "Name: &nbsp"; echo $row[1];
echo '<br />';
echo "Price:: &nbsp"; echo $row[2];
echo '<br />';
echo "Immage: <img src='images/".$row['3']."'alt='Image'>";
echo '<hr> <br />';
$value--;
}
}
while ($value==0)
?>
The rest of the output is coherent with the code. Anyone know why it happen? And how to fix it? Thanks!
You cannot output image like what you did , first of things
when you fetch image from db .
for example if my image filed stored in img field i will call it like this
$id=$_GET['id'];
$query = mysql_query("select * from img where img id=$id ");
$row = mysql_fetch_array($query);
$img = $row['img'];
header("Content-type: image/jpeg");
print $img;
so this page only for displaying the pictures and when you want to use it you can do
<img src="display_img.php?id=2" />
it's not possible to keep every things in one page but also you can divide your page like this
if($$_GET['action']=="display_img"){
// show img code
}

display more than one record >curdate

Attempting to display different data in a table, but am only gettting the most recent result. The only thing I have changed in my code is adding the >CURDATE rather than the most simple *. it has stopped displaying all of the results. can you help?
$result = mysqli_query($con,"SELECT * FROM tripdata WHERE date > CURDATE()");
while($row = mysqli_fetch_assoc($result))
{
echo "<table class='mainpics'>";
echo "<tr><td><a class='mainpic' href='". $row['pageurl'] ."'><img class='mainpic' src='" . $row['mainpic'] . "'/></a></td></tr>";
echo "</table>";
}
mysqli_close($con);
?>

How to select 2 tables when using $_GET for page information

I have 2 pages maincontent.php and showcontent.php. When someone visits maincontent.php they are presented with 2 images that they can click on. If they click on image 1 showcontent.php will grab more information about that image and display. This works fine but i would like to be able to grab data from 2 tables not just one.
Here is maincontent.php
<?php include('includes/connect.php');?>
<div id="maincontent_holder">
<div id="newest_shows">
<?php
$query = "SELECT * FROM tv_shows ORDER BY id DESC LIMIT 6";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
echo "<div id='lastest'>";
echo "<a href='show.php?show_name=$row[show_name]'><img src='$row[show_cover]' width='110' height='160' alt='$row[show_name]'> </a>";
echo "</div>";
}
?>
</div>
</div>
Here is showcontent.php
<?php
include('includes/connect.php');
?>
<div id="maincontent_holder">
<?php
$show_name= $_GET['show_name'];
$sql1="SELECT * FROM tv_shows WHERE show_name = '$show_name'
UNION
SELECT * FROM show_episodes WHERE show_name = '$show_name'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "<div id='cover_img'>";
echo "<img src='$row1[show_cover]' width='110' height='160' alt='$row1[show_name]'>";
echo "</div>";
echo "<div id='show_title'>";
echo $row1['show_name'];
echo "</div>";
echo "<div id='show_info'>";
echo $row1['show_info'];
echo "</div>";
echo "<div id='show_airs'>";
echo $row1['show_airs'];
echo "</div>";
echo "<div id='show_status'>";
echo $row1['show_status'];
echo "</div>";
echo "<div id='show_top_adzone'>";
echo "</div>";
echo "<div id='show_desc'>";
echo $row1['show_desc'];
echo "</div>";
//end of show desc
echo "<div id='episode_list'>";
echo "<a href='blaa'>$row1[episode_name]</a>";
echo "</div>";
}
?>
</div>
This is the error i get from showcontent.php
Warning: mysql_fetch_assoc() expects parameter 1 to be resource,
boolean given in C:\xampp\htdocs\includes\layout\showcontent.php on
line 15
You should to use LIKE '%{$show_name}%', before SQL quering do mysql_real_escape_string($show_name) for exclude SQL injection. Althow you should be sure of stame collumn structore of tv_shows and show_episodes. I think it's not use join:
SELECT s.*, e.* FROM tv_shows AS s JOIN show_episodes e ON e.show_id = s.id;
Change your query to something like this
$sql1="SELECT tv_shows.*, show_episodes.* FROM tv_shows, show_episodes
WHERE tv_shows.show_name = '$show_name' and show_episodes.show_name = '$show_name'
and tv_shows.show_name = show_episodes.show_name";
This will do the trick

Categories