how to show images in php page from link saved in DB - php

So if you feel the question is repeated ,I have tried other solutions but not working.I have link of images stored in Database(phpMyAdmin) as string.I want to load the image as a part of the second column of a table from DB using the links.
<table style="width:100%" id="to1">
<tr>
<th>ID</th>
<th>Featured Image</th>
<th>Title</th>
<th>Brand Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Shipping Cost</th>
<th>Delivery Time </th>
<th>Delivery Distance</th>
</tr>
<?php
$conn = mysqli_connect("localhost","root", "", "products");
if ($conn-> connect_error){
die("Connection Failed: ". $conn-> connect_error);
}
$sql = "SELECT id,featured_image,title,brand_name,price,quantity,shipping_cost,delivery_time,delivery_distance from products";
$result = $conn-> query($sql);
if ($result-> num_rows > 0){
while ($row = $result-> fetch_assoc()){
$val = $row["featured_image"];
echo "<tr><td>".$row["id"] ."</td><td>". '<img src='<?= **HERE** ?>' alt="">' ."</td><td>".$row["title"] ."</td><td>".$row["brand_name"] ."</td><td>".$row["price"] ."</td><td>".$row["quantity"] ."</td><td>".$row["shipping_cost"] ."</td><td>".$row["delivery_time"] ."</td><td>".$row["delivery_distance"] ."</td></tr>";
}
echo "</table>";
}
else{
echo "0 tables";
}
$conn-> close();
?>
I have shown the place as here .please suggest some ways so that I can show images instead of links in the table.
Here is what I want it to look like.

I think, you had quotes problems:
UPDATED:
echo "<tr><td>".$row["id"] ."</td><td><img src='".$row["featured_image"]."' alt=''></td><td>".$row["title"]."</td><td>".$row["brand_name"]."</td><td>".$row["price"]."</td><td>".$row["quantity"]."</td><td>".$row["shipping_cost"]."</td><td>".$row["delivery_time"]."</td><td>".$row["delivery_distance"] ."</td></tr>";

echo "
<tr>
<td>.$row["id"].</td>
<td>.<img src='$row["featured_image"]' alt="">.</td>
<td>.$row["title"].</td>
<td>.$row["brand_name"].</td>
<td>.$row["price"].</td>
<td>.$row["quantity"].</td>
<td>.$row["shipping_cost"].</td>
<td>.$row["delivery_time"].</td>
<td>.$row["delivery_distance"].</td>
</tr>"
Try this

Related

How to Add update button in html table for editing table data?

Following code extracting values from MySQL table using PHP. I want to add an update button in the last column of this table. I have done some coding, but when I am clicking on update its showing white screen. I want HTML table to become editable when the user clicks on update button and after submitting the values will be changed in MySQL table.
Here is code:
<?php
$connection = mysqli_connect ('localhost', 'user', 'password', 'testdb');
if (!$connection){
echo 'Not connected to server';
}
$select_db = mysqli_select_db($connection, 'testdb');
if (!$select_db){
echo 'Not connected to database';
}
$sql= "SELECT * FROM students";
$query = mysqli_query($connection, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($connection));
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE `students` SET `elected_subject`='$ElectedSubject'";
mysql_query($UpdateQuery, $con);
};
?>
<h1><strong>Student Record</strong></h1>
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Father Name</th>
<th>Class</th>
<th>Selected Subject</th>
<th>View/Update</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
echo "<form action=update_students.php method=post>";
$student = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$row['student_id'].'</td>
<td>'.$row['student_name'].'</td>
<td>'.$row['father_name'].'</td>
<td>'.$row['class'].'</td>
<td>'.$row['elected_subject'].'</td>
<td>' . "<input type='submit' name='update' value='update'>" . '</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
update_students.php is the same file which is having this code.

Search Results display

i have a website project, where customers will need to search and the results will display, so i have done all that, but the problem is when a search results comes up,if there's more than 1 row,it dublicate the results on the same row,here are the code.
<h1 style="font-family:calibri;color:#13CC0D;text-align:center;">BODABODA SEARCH RESULTS</h1>
<table cellpadding="1" cellspacing="1" id="resultTable">
<thead>
<tr>
<th style="border-left: 1px solid #C1DAD7"> Region</th>
<th> District </th>
<th> Ward </th>
<th> Street</th>
<th> Driver Name </th>
<th>Identification Type</th>
<th>Identification Number</th>
<th>Motorcycle Type</th>
<th>Motorcycle Reg No</th>
<th>Phone Number</th>
</tr>
</thead>
<?php
$conn = mysqli_connect('localhost', 'efalococ_calcia', '*ad4#zNQ=nfN') or die('can not connect to the server'.mysqli_error);
mysqli_select_db($conn, 'efalococ_safirii');
if(isset($_POST['search'])){
$query = $_POST['region'];
$query1 = $_POST['district'];
$query2 = $_POST['ward'];
$query3 = $_POST['street'];
$results = mysqli_query($conn,"SELECT * FROM bodaboda WHERE (`region` LIKE '%".$query."%') && (`district` LIKE '%".$query1."%') && (`ward` LIKE '%".$query2."%') && (`street` LIKE '%".$query3."%')") or die(mysql_error());
if(mysqli_num_rows($results) >0){
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['Region']."</td>" ;
echo "<td>".$row['District']."</td>";
echo "<td>".$row['Ward']."</td>";
echo "<td>".$row['Street']."</td>";
echo "<td>".$row['DriverName']."</td>";
echo "<td>".$row['IdentificationType']."</td>";
echo "<td>".$row['IdentificationNumber']."</td>";
echo "<td>".$row['MotorcycleType']."</td>";
echo "<td>".$row['MotorcycleRegNo']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
}
}else{
echo '<span style="color:red;font-family:cursive;font-size:14px;">No results found, Please Modify your search </span> Click here'.mysqli_error($conn);
}
}
?>
</table>
And the results shows like this!
Can you please help on that? any idea?
You just need to use the <tr> for every row.
The tag defines a row in an HTML table.
Learn more about HTML tr tag
while($row = mysqli_fetch_array($results)) {
echo "<tr>";
echo "<td>".$row['Region']."</td>" ;
echo "<td>".$row['District']."</td>";
echo "<td>".$row['Ward']."</td>";
echo "<td>".$row['Street']."</td>";
echo "<td>".$row['DriverName']."</td>";
echo "<td>".$row['IdentificationType']."</td>";
echo "<td>".$row['IdentificationNumber']."</td>";
echo "<td>".$row['MotorcycleType']."</td>";
echo "<td>".$row['MotorcycleRegNo']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
echo "</tr>";
}

PHP With Row Number And If Else

I am running a sql query and getting a count of the results using PHP. The below is the relevant parts of my syntax and what I want to do is if the count returned is >= 1 then display the grid (as it will be populated with 1 or more rows), but if the row count returned is 0 then display a warning on screen notifying the user.
My issue with the code is that it always returns the grid!
<?php
//Connection to server to run sql query and return results
$numofrows = mssql_num_rows($tsql);
?>
if ($numofrows >= 1) {
//Build out HTML Table
} else {
//write on screen there are no results to display
}
EDIT
This is how I am setting it up which appears to be the same as the link in the comments
<?php
//Connection to server to run sql query and return results
$numofrows = mssql_num_rows($tsql);
?>
if ($numofrows >= 1) {
<table border="1">
<thead>
<tr>
<th >Header 1 </th>
<th>Header 2 </th>
<th>Header 3 </th>
<th>Header 4 </th>
<th>Header 5</th>
<th>Header 6 </th>
</tr>
</thead>
<tbody>
<?php
foreach( $query as $res ) {
print "<tr>";
print "<td>" .$res->Field1."</td>";
print "<td>" .$res->Field2."</td>";
print "<td>" .$res->Field3."</td>";
print "<td>" .$res->Field4."</td>";
print "<td>" .$res->Field5."</td>";
print "<td>" .$res->Field6."</td>";
print "</tr>";
}
?>
} else {
echo "<strong>No Results</strong>"
}
Here's an approximate example, to help you out.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select Database
mysqli_select_db($conn, "test");
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body bgcolor="#06160F">
<table frame="hsides" style="color:#ffffff;">
<!-- Headers of the table -->
<tr>
<th>Header 1 </th>
<th>Header 2 </th>
<th>Header 3 </th>
<th>Header 4 </th>
<th>Header 5</th>
<th>Header 6 </th>
</tr>
</table>
</div>
<div>
<div>
<table border="1" width="960px">
<?php
$sql = "SELECT * FROM abc";
$result = mysqli_query($conn,$sql);
if($result)
{
if ($result->num_rows > 0)
{echo"<table>";
// output data of each row
while($row = $result->fetch_assoc())
{
echo"<tr>";
echo"<td width=120px align=\"center\">".$row["feild1"]."</td>";
echo"<td width=170px align=\"center\">".$row["feild2"]."</td>";
echo"<td width=120px align=\"center\">".$row[""feild3"]."</td>";
echo"<td width=170px align=\"center\">".$row["feild4"]."</td>";
echo"<td width=420px align=\"center\">".$row["feild5"]."</td>";
echo"<td width=420px align=\"center\">".$row["feild6"]."</td>";
echo"</tr>";
}
echo"</table>";
}
else
echo "<h3 align=\"center\">Nothing to show.</h3>";
}
else
echo "<br> Database error.";
$conn->close();
?>
</table>
</div>
<br>
</div>
</body>
</html>
By the way I'm using MySQL.
You can also check this answer: Checking for empty result (php, pdo, mysql)
I think also, you should use PDO.
Your if loop is outside the php section, so it won't be evaluated

DataTables warning (table id = 'My Table'): Requested unknown parameter '0' from the data source

I have a datatable to show fields from a MYSQL database however I keep getting the error:
DataTables warning (table id = 'My Table'): Requested unknown
parameter '0' from the data source
Here is my code:
<table class="table table-striped table-bordered" id="sample_1">
<thead>
<tr>
<th style="width:8px;"><input type="checkbox" class="group-checkable" data-set="#sample_1 .checkboxes" /></th>
<th>Username</th>
<th class="hidden-phone">Email</th>
<th class="hidden-phone">Points</th>
<th class="hidden-phone">Joined</th>
<th class="hidden-phone"></th>
</tr>
</thead>
<tbody>
<?php
$con = mysql_connect("localhost","cl49-XXX","XXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cl49-XXX", $con)or die( "Unable to select database");
$result=mysql_query("SELECT * FROM products ")or die('You need enter a catagory ' );
echo "<tr>"; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$prodname = $row['prodname'];
$prodID = $row['prodID'];
$catagory = $row['catagory'];
echo "<tr class='odd gradeX'>
<td><input type='checkbox' class='checkboxes' value='1'/></td>
<td class='hidden-phone'>$prodID</td>
<td class='hidden-phone'>$prodname</td>
<td class='hidden-phone'>$catagory</td>
<td class='hidden-phone'><a href='deleteproduct.php?q=$prodID'><button class='btn btn-danger'type='submit'><font color='white'>Delete</font> </a>
";
echo "</tr><tr>"; // it's time no move to next row
}
echo "</tr>"; // last row ending
?>
</tbody>
</table>
Does anyone know what this error means?
The number of td's should match what you have given while calling out the datatable();
Quick fix(if you do not want to customize the default functionalities) would be :
$('#sample_1').dataTable();
If you have given anything inside datatable({/configs/}) just remove it completely and it should work well.

error when using php to connect to mysql

I am trying to dynamically build a table on my page through ajax, using jquery and php. The following is my php code:
<?php
$con = mysql_connect("127.0.0.1","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("tbl_name", $con);
$result = mysql_query("SELECT `Book ISBN` FROM tbl_name");
echo "<table id='booklist'><tr>
<th>Edit</th>
<th class='coursename'>Course Name</th>
<th class='startdate'>Start Date</th>
<th class='booktitle'>Book Title</th>
<th class='author'>Book Author</th>
<th class='isbn'>Book ISBN</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>
<td><input type='checkbox'></input></td>
<td class='coursename'>" . $row[`Course Name`] . "</td>
<td class='startdate'>" . $row[`Start Date`] . "</td>
<td class='booktitle'>" . $row[`Book Title`]. "</td>
<td class='author'>" . $row[`Book ISBN`]. "</td>
<td class='isbn'><input class='ISBN_number' type='text' value='' size='13' maxlength='13'></input></td>
</tr>";
}
echo "</table>";
mysql_close($con);
?>
(Note: the row names have spaces in them (thats how the database came). I used back ticks instead of single quotes because someone online said thats how you can keep the spaces... If I get over this issue I'll see if that works.)
Next my jquery script:
$(document).ready(function()
{
$("#build_table").click(function()
{
$.get("build_table.php",
function(table)
{ console.log("Entered table function");
$("#input_table").replaceWith("<div id='input_table'>" + table + "</div>");
});
});
});
and the html:
<input id="build_table" style="clear:both" type="button" value="Submit"></input>
When I click on this button, though, I get the following php error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:...pathname...\build_table.php on line 13
What is this error?
run this query this way
$sql = "SELECT `Book ISBN` FROM tbl_name";
$result = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
and see
The problem lies in your query. Are you sure that your database and table is named tbl_name? Change your statement into this:
$result = mysql_query("SELECT `Book ISBN` FROM tbl_name") or die(mysql_error());
And you'll get a error message from MySQL.

Categories