How to view images from database on to the website document? - php

I'm trying to show pictures stored in a database but when I run the code, a brunch of text of the picture comes up. (shown in the image below)
here's my code for displaying the contents of the database
<?php
include ("connectdb.php");
try {
$rows = $db -> query("SELECT animal_id, title, animal_dob, animal_type, animal_description,picture_of_animal FROM animals");
?> <table>
<tr>
<th> Animal id <th> Title </th> <th> Date of birth </th> <th> Type </th><th>Description</th><th>Picture</th>
</tr> <?php
foreach ($rows as $rows => $r) {
?>
<tr>
<td><?php echo $r['animal_id']; ?></td>
<td><?php echo $r['title']; ?> </td>
<td><?php echo $r['animal_dob']; ?> </td>
<td><?php echo $r['animal_description']; ?> </td>
<td><img src "<?php echo $r['picture_of_animal']; ?>" </td>
</tr>
<?php
}
?>
</table>;
<?php
}catch (PDOException $ex) {
?>
<p>Sorry, a database error occurred.</p>
<p> Error details: <em> <?= $ex->getMessage() ?> </em></p>
<?php
}
?>
Heres a picture of my datatable

you missed equal sign so add it and alao image close tag
<td><img src= "<?php echo $r['picture_of_animal']; ?>"/> </td>

Related

Retrieve Server Data from a mysql db

I have question about how i shall do this.
First i have taken out a list from a mysql database which.
What I want to do is to be able to click on one of these items in the list so that I can get the underlying facts for this particular item.
Now i want to use this as a link to get one row from the database.
The database is done and the list i can see in a website but i dont get the last part to work
show_all_symbol.php
//This show the list
<?php
include 'connect.php';
$sql = "SELECT * FROM t1_symbolism ";
$q = $pdoconn->query($sql);
$q ->setfetchmode(PDO::FETCH_ASSOC);
?>
<table>
<head>
<tr><th>Symbol</th><th>Alternativtnamn</th> <th>Symboltyp</th></tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<td><img src="<?php echo "/symbol/",$linksbild,($row['symb_type']),"/",($row['symb_pic']); ?>"/></td>
<tr>
<td> <a id="leftside_meny" href= <?php echo "/symbol/show_symbol.php";?> target= "texten"> <?php echo ($row['symb_name']); ?></a></td>
<td><?php echo ($row['symb_name_other']); ?></td>
<td><?php echo ($row['symb_type']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
show_symbol.php
<body>
<?php
$funamn = trim($_POST['symbname']);
include 'connect.php';
$sqlanswer = $pdoconn->query(" SELECT * from t1_symbolism where symb_name '$funamn'");
$row = $sqlanswer->fetch();
?>
<table >
<thead>
<tr><th>Symbol</th><th>Symbolnamn</th><th>Alternativtnamn</th> <th>Symboltyp</th><th>Fakta</th></tr>
</thead>
<tr>
<td><img src="<?php echo "/symbol/",$linksbild,($row['symb_type']),"/",($row['symb_pic']); ?>" width="250" height="250"/></td>
<td><?php echo ($row['symb_name']); ?></td>
<td><?php echo ($row['symb_name_other']); ?></td>
<td><?php echo ($row['symb_type']); ?></td>
<td><?php echo ($row['symb_history']); ?></td>
</tr>
</table>
</body>
What i want to do is to use this part as Use this part as a clickable link so I can view a record in the table.
<td> <a id="leftside_meny" href= <?php echo "/symbol/show_symbol.php";?> target= "texten"> <?php echo ($row['symb_name']); ?></a></td>
Thank in the advance!
it looks like you are looking for a way to pass this value $row['symb_name'] to this value $_POST['symbname']. Here's what i think you should try :
in your show_all_symbol.php
<td>
<a id="leftside_meny" href="<?php echo "/symbol/show_symbol.php?symb_name=" . $row['symb_name'];?>" target="texten">
<?php echo ($row['symb_name']); ?>
</a>
</td>
in your show_symbol.php
$funamn = trim($_GET['symb_name']);
If you want to use $_POST then you will have to either figure a way to make a form out of each clickable element and configure it to make a POST or do it using javascript or jquery.

Inserting foreign keys into a table from the website using php (phpMyAdmin)

So, I have 3 tables in my database (phpMyAdmin) bikes, book and users. I have made bike_id and user_id a foreign key in the book table. Now I want these 2 to get inserted into the book table from the web page.
I've got 3 php files which are book1.php, book2.php and book3.php. In book1, you can select a bike which you want to book which results in showing that particular bike in new page. In book2, you select the date when you want to book and when you go to book3, the user_id and bike_id should be automatically inserted into the book table? But I only get ERROR when I press book in book2 page.
Can someone help me plz.. Below is the php codes for 3 php files.
book1.php:
<?php
require 'connect.php';
$select_posts = "select * from bikes ";
$run_posts = mysql_query($select_posts);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th>Select Bike</th>
</tr>
<?php while($row=mysql_fetch_array($run_posts)){ ?>
<tr>
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><a href="book2.php?id=<?php echo $row[bike_id];?>">Select</td>
</tr>
<?php } ?>
book2.php:
<?php
session_start();
?>
<?php
require 'connect.php';
if(isset($_GET['id'])){
$took_id = $_GET['id'];
$select_query = "select * from bikes where bike_id='$took_id'";
$run_query = mysql_query($select_query);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th></th>
<th>Select Date</th>
<th>Book</th>
</tr>
<?php while($row=mysql_fetch_array($run_query)){ ?>
<tr>
<form action="book3.php" method="POST">
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><?php echo $took_id; ?></td>
<td>Select Date: <input type="text" id="datepicker" name="datepicker"></td>
<td><input type="submit" name="Submit" value="Book"></td>
</tr>
<?php }} ?>
</table>
book3.php:
<?php
session_start();
?>
<?php
require 'connect.php';
// Get values from form
$datepicker = $_POST['datepicker'];
$sql="INSERT INTO $tbl_name(datepicker)VALUES('$datepicker')";
$sql="INSERT INTO $tbl_name(user_id)VALUES(select user_id from users)";
$sql="INSERT INTO $tbl_name(bike_id)VALUES(select bike_id from bikes)";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
?>

Dynamic rowspan table in PHP

I have a problem getting a table to work.
I want to adjust a printable form for order list (opencart),
but I want address box on left and products ordered on right.
The problem is the number of products varies per order, so using rowspan on the address box does not work (I left the rowspan in the code).
<thead>
<tr>
<td><b><?php echo $text_to; ?></b></td>
<td><b><?php echo $column_product; ?></b></td>
<td class="text-center"><b><?php echo $column_quantity; ?></b></td>
</tr>
</thead>
<tbody><tr>
<td rowspan="10"><?php echo $order['shipping_address']; ?><br/><br/>
<?php echo $order['telephone']; ?>
<br />
<?php if ($order['shipping_method']) { ?>
<b><?php echo $text_shipping_method; ?></b> <?php echo $order['shipping_method']; ?><br />
<?php } ?></td></tr>
<?php foreach ($order['product'] as $product) { ?>
<td><?php echo $product['name']; ?>
<?php foreach ($product['option'] as $option) { ?>
<br />
<small> - <?php echo $option['name']; ?>: <?php echo $option['value']; ?></small>
<?php } ?></td>
<td class="text-center"><?php echo $product['quantity']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You have all the information available it seems so you just need to change:
<td rowspan="10">
to:
<td rowspan="<?php echo count($order['product']); ?>">
However, you do need to check the generated html as it looks like you are not generating valid rows so you might need to change the logic a bit.

How: HTML table row click results in POST form linking to subsequent page

Evening all, I have the form, which is populated from a sql database.
<table class="sortable" border="2" cellspacing="2" cellpadding="2">
<tr>
<th> Serial Number </th>
<th> Date </th>
<th> Time </th>
<th> Color </th>
<th> Design </th>
<th> Result </th>
</tr>
<?php
$i = 0;
while ($i < $num)
{
$serial = mysql_result($results, $i, 'serial_number');
$date = mysql_result($results, $i, 'date');
$time = mysql_result($results, $i, 'time');
$airport = mysql_result($results, $i, 'color');
$terminal = mysql_result($results, $i, 'design');
$result = mysql_result($results, $i, 'result');
?>
<tr>
<td> <?php echo $serial; ?> </a></td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
<?php
$i++;
}
?>
What I would like to do is have each row of the table clickable. When each row is clicked, one cell of data from that row (the first cell) is sent via (POST) to the next page. Can a form be integrated into each tr??
You specify jQuery in your tags, so I assume you can use that.
// when any row is clicked
$('table').on('click', 'tr', function () {
// get the value
var value = $(this).find('td:first').text();
// redirect the user with the value as a GET variable
window.location = nextPage + '?data=' + value;
});
Where nextPage is the URL of the page you want to redirect to.
The short answer: yes, a form can be integrated into each tr.
The long answer - use just as you did before:
<tr>
<td>
<form method="post" target="details.php">
<input type="submit" name="more" value="<?php echo $serial; ?>"
</form>
<?php echo $serial; ?>
</td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
But GET is easier, make a series of links that go details.php?number=123, or whichever:
<td>
<a href="details.php?number=<?php echo $serial; ?>"
<?php echo $serial; ?>
</a>
</td>
Although get can use a form, the data send is not user-customised, so the form data can be generated to use like a link.
Try with that code in echo when create your table:
<td><?php echo(''.$serial.'');?></td>
For each data that you have!
OTHER SOLUTION WITHOUT ACTION
<td><?php echo(''.$serial.'');?></td>
my_page.php - where to send the data
?[variable_name] - where is stored the data

Display Form Fields Based on User Input

I'm displaying a set of rows here for every car I have in my database.
Each row has a form field where a logged in user can submit an offer.
When a user has made an offer for any car, the form field is replaced by a text displaying the value of the offer submitted.
What I'm experiencing, however, is less than ideal results.
If I make an offer for one row, great, the logic works. If I go ahead and make another offer for another row then the logic works, except the fact that the previous row now displays the form again.
I can provide more details if necessary but perhaps someone is familiar with this already.
Thanks in advance.
<?php
require("db-connect.php");
$display = "SELECT filename, car_id, make, model, year, mileage, vin, description, GROUP_CONCAT(filename) FROM scraplis_cars LEFT JOIN scraplis_images USING (car_id) GROUP BY car_id ORDER BY date_time DESC";
$dResult = mysql_query($display) or die('error:' . mysql_error());
$offer = "SELECT car_id, user_id, offer_id, value FROM scraplis_offers WHERE user_id = '".$_SESSION['user_id']."'";
$oResult = mysql_query($offer) or die('Error ' . mysql_error());
$oRow = mysql_fetch_array($oResult);
if(!isset($_SESSION['access'])){
header("location:index.php");
}
?>
<?php if($dResult): ?>
<table class="post">
<thead>
<tr>
<?php if(isset($_SESSION['email']) && $_SESSION['access'] == 0) : ?>
<th scope="col">Images</th>
<th scope="col">Make</th>
<th scope="col">Model</th>
<th scope="col">Year</th>
<th scope="col">Mileage</th>
<th scope="col">VIN #</th>
<th scope="col">Description</th>
<th scope="col">Offer</th>
</tr>
</thead>
<tbody>
<?php while($dRow = mysql_fetch_array($dResult)) : ?>
<?php $str = $dRow[8]; ?>
<?php $images = explode(',', $str); ?>
<tr>
<td>
<ul>
<?php if(!empty($str)) : ?>
<?php foreach($images as $value) :?>
<li>
<a href="images/<?php echo $value; ?>" rel="lightbox[<?php echo $row['car_id']; ?>]">
<img src="images/<?php echo $value; ?>"/>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
<ul>
</td>
<td><?php echo $dRow['make']; ?></td>
<td><?php echo $dRow['model']; ?></td>
<td><?php echo $dRow['year']; ?></td>
<td><?php echo number_format($dRow['mileage']); ?></td>
<td><?php echo $dRow['vin']; ?></td>
<td><span><?php echo $dRow['description']; ?></span></td>
<td>
<?php if($oRow['car_id'] == $dRow['car_id']) : ?>
Offer pending approval - $<?php echo $oRow['value']; ?>
<?php else : ?>
<form id="offer" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="text" id="price" name="offer" />
<input type="hidden" name="submitted" value="<?php echo $dRow['car_id']; ?>" />
<input type="submit" name="price" value="Submit" />
</form>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
<?php else : ?>
<th scope="col">Delete</th>
<th scope="col">Images</th>
<th scope="col">Make</th>
<th scope="col">Model</th>
<th scope="col">Year</th>
<th scope="col">Mileage</th>
<th scope="col">VIN #</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<?php while($dRow = mysql_fetch_array($dResult)) : ?>
<?php $str = $dRow[8]; ?>
<?php $images = explode(',', $str); ?>
<tr>
<td>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="checkbox" name="record" value="<?php echo $row['car_id']; ?>" />
<input type="submit" name="delete-car" value="Delete" />
</form>
</td>
<td>
<ul>
<?php if(!empty($str)) : ?>
<?php foreach($images as $value) :?>
<li>
<a href="images/<?php echo $value; ?>" rel="lightbox[<?php echo $row['car_id']; ?>]">
<img src="images/<?php echo $value; ?>"/>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</td>
<td><?php echo $dRow['make']; ?></td>
<td><?php echo $dRow['model']; ?></td>
<td><?php echo $dRow['year']; ?></td>
<td><?php echo number_format($dRow['mileage']); ?></td>
<td><?php echo $dRow['vin']; ?></td>
<td><span><?php echo $dRow['description']; ?></span></td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</tbody>
</table>
<?php endif; ?>
One important thing for security first:
SEARCH:
if(!isset($_SESSION['access'])){
header("location:index.php");
}
REPLACE WITH:
if(!isset($_SESSION['access'])) {
header("Location: index.php");
exit;
}
Take a look in the PHP documentation for header() or exit() - both describe the need (or security issue) of exit() here afair.
To your question:
You just have the first row of $oResult in $oRow - so you have (for example) 1000 cars but just one offer. You need to fetch the results of $oResult within a loop (while(), for(),... - what you prefer...) and then check wether you can find car_id (within $dRow also in the offers).
code sample (very easy for understanding):
<?php
// ...
// get the offers
// info: user_id would not be necessary here ;-)
$offer = "SELECT car_id, user_id, offer_id, value FROM scraplis_offers WHERE user_id = '".$_SESSION['user_id']."'";
$oResult = mysql_query($offer) or die('Error ' . mysql_error());
$oRows = array();
while($oRow = mysql_fetch_array($oResult)) {
$oRows[$oRow['car_id']] = array(
'offer_id' => $oRow['offer_id'],
'value' => $oRow['value']
);
}
// looping the through the cars
// just the while()-loop based on your code
while($dRow = mysql_fetch_array($dResult)) {
// check if offer exists
if(array_key_exists($dRow['car_id'], $oRows)) {
// H A V E an offer for that car ;-) - show offer details
} else {
// H A V E N O offer that car - show form
}
}
// ...
?>
I hope I didn't get you wrong, made no mistakes (needed to get up early) and this helps you ;-).

Categories