Retrieve image for each search data from database using php - php

I am trying to access image for each company, when the company details is got through search. This is part of my coding.
<?php
$result = mysql_query("SELECT * FROM enquiry where product like '%$title%' ") or die("error");
$found = mysql_num_rows($result);
if ($found > 0) {
while ($row = mysql_fetch_array($result)) {
echo ("<img src='try5.php?id=" . $row['id'] . "' height='100' width='100'><br>");
echo ("$row[mainproduct]$row[Companyname],$row[District]<br><br>$row[description].<a href=$row[Website]>Read more...</a><br><brContact Person:$row[ContactPerson]<br>Mobile no :$row[Mobile]<br>Website:<a href=$row[Website]>$row[Website]</a><br><br><hr><br><br>");
}
} else {
echo "<li>No results Found<li>";
}
//Coding of try5.php
$link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("test") or die(mysql_error());
$sql = "SELECT image FROM imtest";
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
header("Content-type: image/jpeg");
echo mysql_result($result, 0);
?>
I am getting same image for all company details. For each company I need a different image. I have stored the images in different tables not along with data, is it right? How shall I do it using php?

Your SQL statment will always return the same result. You forgot to include WHERE, like:
$sql = "SELECT image FROM imtest WHERE id=" . $_GET['id'];

Related

Trying to delete specific row in a list of items from a MySQL database using PHP

I have a list of items that is being output via PHP / MySQL. I also have an Edit button and a Delete button in one column. I am trying to figure out how to delete a list item on a specific row by clicking the Delete button. I have tried the following:
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
} else {
echo "Cannot delete";
}
}
This of course does not work. Can anyone help me out with this?
UPDATE
This is the code for the entire page:
https://pastebin.com/raw/qjnZkUU2
UPDATED CODE
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}
What I am confused about is how does the query know which item to delete? Should I be appending the ID to the URL to pass the ID?
UPDATE
Ok I think I get it....In the delete button, I need to echo the ID of that row so when the query runs from clicking the delete button, it knows which ID to delete correct?
RESOLVED
Alright. I got it figured out!
I have a button that references the ID of the list item:
echo "
I then pass that ID to deletelist.php
if (!isset($_GET['id'])) {
echo 'No ID was given...';
exit;
}
if ($db->connect_error) {
die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}
$sql = "DELETE FROM lists WHERE id = ?";
if (!$result = $db->prepare($sql)) {
die('Query failed: (' . $db->errno . ') ' . $db->error);
}
Item gets deleted.
you have to execute query for any action in database
mysqli_query($db, $query);
so execute a delete query and then try it again
You have to execute the query. There is no query execution code. Try this
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}
In order to delete a specific row what I needed to do was echo the ID within a link/button. This would then pass the ID to the needed PHP to delete the row from the database.
Echoing the row ID in the button
echo "
The PHP to delete the action row
<?php
include("db.php");
if (!isset($_GET['id'])) {
echo 'No ID was given...';
exit;
}
if ($db->connect_error) {
die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}
$sql = "DELETE FROM lists WHERE id = ?";
if (!$result = $db->prepare($sql)) {
die('Query failed: (' . $db->errno . ') ' . $db->error);
}
if (!$result->bind_param('i', $_GET['id'])) {
die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}
if (!$result->execute()) {
die('Execute failed: (' . $result->errno . ') ' . $result->error);
}
if ($result->affected_rows > 0) {
echo "The ID was deleted with success.";
} else {
echo "Couldn't delete the ID."; }
$result->close();
$db->close();
header('Location: ../account.php');
?>
This deletes the item row and then returns the user to account.php. Which in this case, the page never really changes.
Try like below
if(isset($_POST["deletelist"]) && isset($_GET['id']) ) {
$id = $_GET['id'];
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '".mysqli_real_escape_string($db,$id)."'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}

Conditional mysqli date range check to print a price in sql

This time I'm trying to make some php code to work with mysqli in order to check if today date is between a range of dates in a Mysql table, if the condition is true I need to print a price from a table otherwise it shuould print a different price from another table. so I already have all sql connections set in another php file, the problem is that when I try the code, it just shows nothing, a blank page only. This is the code im using:
<?php
$currentdate = date("Y/m/d");
//basic include files
require_once('/home/user/public_html/folder/db.php');
$seasonalpricedate = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1' AND $currentdate >= 'seasonal_from' AND $currentdate <= 'seasonal_to';");
$result = ($seasonalpricedate) or die(mysqli_error());
if (mysqli_num_rows($result) != 0) {
$standardprice = mysqli_query($conn, "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");
if(! $standardprice ){
die('Could not get data: ' . mysqli_error());
}
while($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC)){
echo "$ {$standard['room_price']} ";
}
} else {
if(! $seasonalpricedate ){
die('Could not get data: ' . mysqli_error());
while($standard2 = mysqli_fetch_array($seasonalpricedate, MYSQL_ASSOC)){
echo "$ {$standard2['seasonal_price']} ";
}
}
}
?>
I already tried both codes with standardprice and seasonalprice working without a conditional, but when I try to do it like this, it does not show anything.
PostData: Im still trying to learn english, so please appologize me if I fail some words, thanks in Advance.
UPDATE: Ok so in this way it works if there is no values true its ok, it shows the standardprice, but if match the date, dont show anything, here is the code changed:
<?php
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
$currentdate = date("Y-m-d");
//basic include files
require_once('/home/trankilo/public_html/book/db.php');
$seasonalpricedate = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1' AND '$currentdate' >= seasonal_from AND '$currentdate' <= seasonal_to");
$result = ($seasonalpricedate) or die(mysqli_error());
if (mysqli_num_rows($result) != 0) {
$seasonalprice = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1'");
if(! $seasonalprice )
{
die('Could not get data: ' . mysqli_error());
while($standard2 = mysqli_fetch_array($seasonalprice, MYSQL_ASSOC))
{
echo "$ {$standard2['seasonal_price']} ";
}
}
} else {
$standardprice = mysqli_query($conn, "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");
if(! $standardprice )
{
die('Could not get data: ' . mysqli_error());
}
while($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC))
{
echo "$ {$standard['room_price']} ";
}
}
mysqli_close($conn);
?>
So close to make it work, thanks to
UPDATE: Because you are also updated your OP, now i found what causes the problem. Ther proble were when you says: die('Could not get data: ' . mysqli_error()); And after that you want to try a while loop. while won't executed, because you terminated the script with a die(); Move the while to the else case of your if condition. See my comments.
Use this:
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
//Also display your errors.
display_errors(true);
$currentdate = date("Y-m-d");
//basic include files
require_once('/home/trankilo/public_html/book/db.php');
//Put your query into a variable, so you can dump / print it.
$sql = "SELECT `seasonal_price`"
. " FROM `hotel_seasonal_price`"
. " WHERE room_type_id = '1'"
. " AND '" . $currentdate . "' >= seasonal_from"
. " AND '" . $currentdate . "' <= 'seasonal_to'";
echo $sql;
//Try to run it in the sql directly. Is it gives back you any result?
//Do not need to
$result = mysqli_query($conn, $sql) or die(mysqli_error());
if (mysqli_num_rows($result) != 0) {
//Check if we have result by echoing some dummy text
echo "Yes, we have result!";
$sql = "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1'";
//Do the same as the previous query. Does it gives you back anything?
$seasonalprice = mysqli_query($conn, $sql);
if (!$seasonalprice) {
//I do not really get what happens here. If you have no seasonalprice,
//then you can not fetch_array on that!
//Move this whole section.... You've say die, and after that do a while?
die('Could not get data: ' . mysqli_error());
} else {
while ($standard2 = mysqli_fetch_assoc($seasonalprice)) {
echo "$ " . $standard2['seasonal_price'] . " ";
}
}
} else {
//Same as previous
$sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";
$standardprice = mysqli_query($conn, $sql);
if (!$standardprice) {
die('Could not get data: ' . mysqli_error());
//same here, move the while to the else...
} else {
while ($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC)) {
echo "$ {$standard['room_price']} ";
}
}
}
mysqli_close($conn);

Take a single value from a MySQL field and store it in a variable

I am a beginner to PHP and MySql. What I wish to do is take a single field value from a MySql table and store it into a php variable. I tried this code but it does not seem to work:
//Get Role ID
$con= mysqli_connect("localhost","root","","darrenvellaedp2");
$result = mysqli_query($con,"SELECT userRoleID FROM tbl_users");
while($row = mysqli_fetch_array($result)) {
echo $row['userRoleID'];
echo "<br>";
}
//make the connection
$con = mysqli_connect("localhost","root","","darrenvellaedp2") or die("Error: " . mysqli_error($con));
//create the query
$result = "SELECT userRoleID FROM tbl_users" or die("Error: " . mysqli_error($con));
//execute the query
$res = $con->query($result);
while($row = mysqli_fetch_array($res)) {
//this will print the userroleid out to the screen
echo $row['userRoleID'];
//this will store it in a variable
$UserRoleID = $row['userRoleID'];
}
It would have been easier for you to just google it as there's a whole section about this on PHP.NET

Updating field in Database with if else statement

I am working on recieveing an item from another application with the use of $_POST and I am trying to see if that item already exists in the database. If it does, then $count increases by one. If it does not exist in the database, then it will added in with the use of INSERT INTO.
Here is my code:
<?php
date_default_timezone_set('Asia/Manila');
$today = date('m-d-Y');
echo $today;
$con= mysqli_connect("******","******","******")
or die ('Error: ' . mysql_error());
mysqli_select_db($con,"a3656574_opacmin");
$sql= "SELECT keyWord FROM searchedWords";
$result= mysqli_query($con,$sql);
if($result==$_POST[keyWord])
{
$upD="UPDATE searchedWords SET countr = countr + 1";
while (!mysqli_query($con,$upD))
{
die('Error: ' . mysqli_error($con));
}
}
else
{
$insertIn="INSERT INTO `searchedWords`( `keyWord`, `countr`) values ('$_POST[keyWord]',1)";
while (!mysqli_query($con,$insertIn))
{
die('Error: ' . mysqli_error($con));
}
}
?>
I don't know what's wrong. No items are sent to the database at all. Does anyone know how to fix it?
Change your code like this...
$result= mysqli_query($con,"SELECT keyWord FROM searchedWords");
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if($row['keyWord']==$_POST[keyWord])
{
$upD="UPDATE searchedWords SET countr = countr + 1";
while (!mysqli_query($con,$upD))
{
die('Error: ' . mysqli_error($con));
}
}
$result==$_POST['keyWord'] won't work becase $result is object there so...
After this line
$result= mysqli_query($con,$sql);
You have to fetch the data
$keyword = '';
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$keyword = $row["keyWord"];
}

using dynamic data in a javascript method

I'm new at php and JavaScript so I hope you can help me!
below is a php code! it take data from my db and display my photos and photo's title. what I want to do is if you click on one of this photos and than a new php expe: photo.php opens and you can see the same photo alone not with the other ones.
<?php
$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC;";
$results = mysql_query($query, $link) or die ("you comand can't be executed!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > " . "<br /><br /><br />";
}
}
?> '
<?php
$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC;";
$results = mysql_query($query, $link) or die ("you comand can't be executed!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
//send photo_id (id of photo from your table) from url NOTE: I've put link here
echo $row['title']. "<a href='index.php?photo_id=".$row['id']."'><img src=/1/" . $row['location']. " width=580px ></a> " . "<br /><br /><br />";
}
if(isset($_GET['photo_id'])) {
//here you get only one id of photo now retrieve the data from database and display the image
}
}
?>
You will get the photo by the help of the url value here photo_id.
do this way you are getting results from database and you are
displaying them with tile and image your aim is to click on
image or image title go to a new page and display image alone
the image title as link in first page
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
<a href='new page url?id=<?php echo $row["id"] ?>'>
echo $row['title'];
echo "</a>"
}
}
where $row['id'] is database unique id if have no it is always preferred
to create a database with unique id and in new page get the uid
using $id=$_GET['id'] and
do as below
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
if($id==$roe['id']{
echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > ";
}
}
}

Categories