cannot display multiple image - php

Image cannot be displayed in html page.
for example code :
<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='$id'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
echo '<img src="'.$location.'" width=30% height=10%>';
echo '<td><div align="center">Delete</div></td>';
echo "<br>";
}
?>

One obvious mistake in your code is that the closing tag '>' is missing for img. So it needs to be
echo "<img src='$location' width='30%' height='10%'>";
Apart from this be sure $location var has the correct absolute or relative path picked from DB to show the image. Then, you are using $id in the query. Be sure this is not the unique ID in the img_homestay table, as that will only return you one row. I believe you want to fetch all the images for a particular post id, so ensure that you are using that ID only for the correct field in the query.
Another suggestion that done switch between double quote and single quote string notation in PHP. This will make your code hard to read and comprehend. In one echo statement you are using
echo " ";
and in next statement you are using:
echo ' ';

Try with this
<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='".$id."'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
?>
<img src="<?php echo $location;?>" width="30%" height="10%">
<td><div align="center">Delete</div></td>
<br>
<?php }
?>

<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='".$id."'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
echo '<img src="'.$location.'" width=30% height=10%>';
echo '<td><div align="center">Delete</div></td>';
echo "<br>";
}
?>

Your processing is wrong. Your query is select * from img_homestay WHERE id='$id' which means it's going to fetch a single row (I assume, because you're making a query based on an ID which, I again assume, is a unique key) so you actually don't need to use the while loop (if you intend to use single image).
Still, if that's not the case, you may need to use <tr> for each row, so maybe, try this:
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id=$id");
while($row=mysql_fetch_array($data)){
$location = $row['location'];
echo "<tr><td><img src='$location' width=30% height=10%>";
echo "<td><div align='center'><a href='#' imgid='$row[imgid]' class='delbutton' title='Click To Delete'>Delete</a></div></td>";
echo "</tr>";
}
?>
Also, I see, you're using td here which means, you're using table but I see no table tags. My guess is, there is some issue in table structure. A better check to see if you're image is being parsed or not would be to check the source code of the rendered HTML page. I'm sure you're getting the img tag in resulting HTML but it is not being rendered due to error in HTML structure so you better check and correct your HTML with respect to the table you're using.

Related

Retrieve mysql value from php for radio buttons not working

I have table address in my database. I put the value=$Id; for my radio button but I have no idea why it's not working. No error shown make me even confuse and I need help since I am still new to php. When I put 2 address and I checked the radio button the address did not change. It only take the latest one that user inserted.
$userid=$_SESSION["userID"];
$sql="SELECT * FROM customer where ID='$userid'";
$result = mysqli_query( $con,$sql);
while($row = mysqli_fetch_array($result))
{
$Id=$row['Id'];
$OrderID=$row['OrderID'];
$ID=$row['ID'];
$Name=$row['Name'];
$Email=$row['Email'];
$Address=$row['Address'];
$PostalCode=$row['PostalCode'];
$City=$row['City'];
$State=$row['State'];
$mobile=$row['mobile_number'];
// $total=$total+$pSubtotal;
// $totalWeight=$totalWeight+$subWeight;
echo "<input type=\"radio\" name=\"select\" value=\"$Id\"> <label>$Name</label><br>";
echo "$Address<br>";
echo "$PostalCode $City<br>";
echo "$State";
echo "<div> </div>";
echo "<img src=\"images/phone-icon.png\" width=\"20\" height=\"20\"> $mobile<br>";
echo "<img src=\"images/email.png\" width=\"20\" height=\"20\"> $Email<br>";
echo "<div> </div>";
echo "<div class=\"col-xs-12 col-lg-12\" style=\"border-bottom:1px solid #ccc;\"></div>";
}
if(isset($_POST["pay"])){
$userid=$_SESSION["userID"];
$final_price=$_POST["final_price"];
$name=$_POST["name"];
$email=$_POST["email"];
$postcode=$_POST["postcode"];
//echo $postcode;
$mobile_number=$_POST["mobile_number"];
//echo $name;
$radio=$_POST['select'];
if(isset($radio)){
$sql="select Id,Name,mobile_number,Email,Address,PostalCode,City,State from customer where Id='$radio'";
echo $sql;
$res = mysqli_query($con,$sql);
if($rows=mysqli_fetch_array($res)){
$idx=$rows['Id'];
$name=$rows['Name'];
$mobile=$rows['mobile_number'];
$email=$rows['Email'];
$address=$rows['Address'];
$postcode=$rows['PostalCode'];
$city=$rows['City'];
$state=$rows['State'];
}
}
}
How are you submiting those information?
You just select the desired radio and press Submit?
Another point is, you do have 2 lines doing the same thing:
$Id=$row['Id'];
$ID=$row['ID'];
You should avoid those kind of thing.
Use the correct uppercase/lowercase according to your database table field name.
Did you use a var_dump($row) to check the correct case ?
That can be the problem.
Sorry if this is not the right answer, I'm not exactly sure what you are asking but I think you mean to use an associative array? Try using mysqli_fetch_assoc.

How to make a variable to show the same row as it is currently showing in PHP?

I really don't know how to explain this question. But, I'm trying to make a product page that shows the same row as it is currently showing (so example "go onto a tech product page and at the bottom it shows all other tech related products").
Here is what I've tried:
<?php
$pType = $row['typeP'];
$sql = "SELECT * FROM products WHERE type=$pType";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($result)){
echo "<a href='products.php?id={$row['productID']}'>";
echo "<img src='uploads/{$row['displayIMG']}'>";
echo "</a>";
}
?>
Your error would be in the $typeP declaration. I am guessing $row is not defined before that. therefor it is empty. Maybe you were trying to use a GET?
<?php
$pType = $_GET['typeP']; //This would get the type id from the querystring
$sql = "SELECT * FROM products WHERE type='".$pType."'";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($result)){
echo "<a href='products.php?id=".$row['productID']."'>";
echo "<img src='uploads/".$row['displayIMG']."'>";
echo "</a>";
}
?>
I should also mention that your code is subject to SQL injection. Please clean your inputs before running them through you database. Otherwise you are asking for trouble.
http://www.wikihow.com/Prevent-SQL-Injection-in-PHP

If database field is empty echo "nothing" if something there echo "something"

looking for a solution to this bit of coding below.
<?php
$nextfive_events = mysql_query("SELECT date_format(date, '%d/%m/%Y') AS formatted_date, title, location, regs FROM events WHERE status = 'ACTIVE'");
if(mysql_num_rows($nextfive_events) == 0) { echo "<p>No events coming up!"; } else {
echo "<table width=\"600\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"left\">
<tr align=\"center\">
<td>Date</td>
<td>Name</td>
<td>Location</td>
<td></td>
</tr>";
$x=1;
while($next_row = mysql_fetch_array($nextfive_events))
{
if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif;
echo "<tr align=\"center\">";
echo "<td>" . $next_row['formatted_date'] . "</td>";
echo "<td>" . $next_row['title'] . "</td>";
echo "<td>" . $next_row['location'] . "</td>";
echo "<td>Regs</td>";
echo "</tr>";
$x++;
}
echo "</table>";
}
?>
I want the row echo "<td> <a href regs .....
To display the word Regs when there is something in 'regs' in the database. Say if there is nothing in that field I want it to be blank and not say Regs.
thanks
You could do Ternary operator:
echo "<td><a href='" . (empty($next_row['regs']) ? "#" : $next_row['regs']) . "'>Regs</a></td>";
Try not to do escape characters, they look confusing, do single quote for href attribute. Also, Did you want
<a href='#'>Regs</a>
to show if it was blank?
If Not, try this:
echo (!empty($next_row['regs']) ? "<td><a href='" . $next_row['regs'] . "'>Regs</a></td>" : "");
You could use a ternary:
echo ( ! empty($next_row['regs'])) ? '<td>Regs</td>' : '<td>&nspb;</td>';
First off, I'd like to show you a really handy trick with PHP that allows you to echo out into HTML without actually saying echo. Just create a break in your PHP code, and in between anything you put will be echoed as HTML.
As for the number of rows returned, you are doing it correctly. Make sure that you are querying properly, that you have an established connection, and that there are no logical errors in your SQL.
Sorry, didn't notice you wanted to check if the column was empty! Just use the function empty(). This function will return true when the data you give it is empty, so simply give it the column from the row of data you wish to check.
<?php
// Lets say this is your database connection variable
$connection = mysqli_connect(//Your info goes here);
// This is the query you want to run
$query = "SELECT something FROM somewhere WHERE something='$something_else'";
// This is where you are storing your results of the query
$data = mysqli_query($connection, $query);
// Here, you can ask for how many rows were returned
// In PHP, ZERO is evaluated to false. We can use a
// Short-hand boolean expression like this
if (mysqli_num_rows($data))
{
// Because zero evaluates to false, we know
// that if we get HERE that there ARE rows
// We can break out of PHP and into our HTML
// by simply ending our PHP tag! Just remember
// that you need to open it back up again though!
?>
<!--You can put your HTML here-->
<p>
We found some rows that you might
need to know about!
</p>
<?php
}
else
{
// But, if we get here, we know it evaluated as
// FALSE and therefore no rows returned
// Here's that HTML trick again
?>
<!--HTML can go here-->
<p>
No rows returned!
</p>
<?php
}
?>

Get the name of the link in the where clause of php query when I click the link?

I have made a website in which the users can select a value from a dropdown menu and get some information from a database. I used ajax to send the request to the database (so the page doesn't get refreshed when I send the request). Here is the part of the jquery function:
$.ajax({
type:'POST',
url:'activities_code.php',
data: {datastr:datastr, datastr1:datastr1},
success:function(response){
$("#msg").html(response);
}});}); // there are other functions before..
The results appear on the main container of the webpage. They are composed of a title and some text. I echo the title in such a way so it is a link. I also give to each element an id and a class so I can call it later. Here is the corresponding code:
echo "<table id=\"container\">";
$num_results = 0;
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Here the columns of title and information are printed
echo "<tr><td>";
echo "".$row['title']."";
echo "<br>";
echo $row['PK'];
echo "</td></tr>";
echo "<tr><td>";
echo $row['Information'];
echo "</td></tr>";
}
What I am trying to do now is: When I click on the title (which is a link), a new page to open in which a php script runs a query and show more information:
Here is what I have:
<?php
include('connect.php');
$query = "SELECT title,Information from activities where title='?????'";
$result = mysqli_query($dbcon, $query) or die('no available data');
echo "<table>";
$num_results = 0;
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Here the columns of title and information are printed
echo "<tr><td>";
echo "".$row['title']." ";
echo "</td></tr>";
echo "<tr><td>";
echo $row['Information'];
echo "</td></tr>";
// Here I sum up the number of the results
$num_results=$num_results+1;
}
?>
I am trying to find a way to put in my query, in the where clause, the name of the title that I selected:
$query = "SELECT title,Information from activities where title='?????'";
Any help would be much appreciated. Let me know if everything is clear or I didn't explain some point clearly.
Thanks.
D.
You can get the title using $_GET global variable and URL parameter. Try changing this line:
echo "".$row['title']."";
to
echo "".$row['title']."";
then you can get the title with these code:
$title = $_GET['title'];
make sure you sanitize the value first. I hope this will help you.
link:
PHP $_GET

Display image with URL from MySQL

I am using this code to display image from URL, but it is not working,
<?php
echo 'me'.'<br/>';
$sql= 'SELECT url_imgsrch FROM p_url_imgsrch';
$result = mysql_query($sql);
echo $result;
$row = mysql_fetch_row($result);
for ($i=0; $i<6; $i++){
//calling rows
echo '<td>';
//calling rows value for debugging purpose so that i can learn the process and check the output
echo $row[$i].'<br/>';
echo '<img name="myimage" src="<?php echo $row[$i]; ?>" width="60" height="60" alt="word" />';
echo '</td>';
}
?>
The result is, I get alt text only, and image is not displayed. also, I get error
Notice: Undefined offset: 1 in D:\wamp\www\demo\login\flashcard.php on
line 31
In my file
What I am trying is, to get 5 img url from database and display them in columns of a table..and what I guess is I am getting same img URL again and again...for 5 times..
Please give me guideline and tell me what I could be missing...
i have never seen someone loop through a query that way, i do it like this:
print "<table>";
$sql= 'SELECT url_imgsrch FROM p_url_imgsrch LIMIT 5';
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
print '<tr>
<td>
<img name="myimage" src="'.$row[column_name_here].'" width="60" height="60" alt="word" />
</td>
</tr>';
}
print "</table>";
change column_name_here to the name of the column which store the image file name
edit: changed mysql_fetch_row to mysql_fetch_array <- that is why u got the same image all 5 times.
With mysql_fetch_row() your fetching the results one row at a time, since you only specified url_imgsrch in the SELECT statement, it will be an array with one single element, the value for url_imgrch. This is the reason for your error message. The for() loop tries to in turn read the first, second, etc up to fifth value of the array, which never gets updated with new information and always has only one element.
mysql_fetch_row() will return false when there is no more data to be read, you need to call it to fetch each new row of data, so what you want is to rewrite your code to something along the lines of this:
$sql = 'SELECT url_imgsrch FROM p_url_imgsrch';
$result = mysql_query($sql);
while (false !== ($row = mysql_fetch_row($result))
{
echo '<td>';
echo '<img name="myimage" src=' . $row[0] . ' width="60" height="60" alt="word" />';
echo '</td>';
}
The first part is just as you've written, setting up the SQL query and fetching a resource pointer ($result) to with mysql_query().
In the next part the while() loop will run for as long as $row is not false, updating $row with new data for each run of the loop. Since mysql_fetch_row() fetches an numerically indexed array, you want to retrieve $row[0], it's the first (and only) column you requested in the SQL query. There are many ways of writing the while() loop, but this way quite common, and easy to read for other developers.
try this:
<?php
echo 'me'.'<br/>';
$sql= 'SELECT url_imgsrch FROM p_url_imgsrch';
$result = mysql_query($sql);
echo $result;
$row = mysql_fetch_row($result);
for ($i=0; $i<6; $i++)
{
//calling rows
echo '<td>';
//calling rows value for debugging purpose so that i can learn the process and check the output
echo $row[$i].'<br/>';
echo '<img name="myimage" src=' . $row[$i] . ' width="60" height="60" alt="word" />';
echo '</td>';
}
?>

Categories