I'm having a problem on developing my website..
First, I have 2 tables:
1- Products: contains info. about the products & 2- Images: which contains product images.
The idea is that : I want to insert an image or two for a specified product and display that image for that product only..
In the mean time, I can insert images into the database and I can also display them.. but my issue is that when I try to do display.. the code retrieves all images from Images table..
so my question: how can I display image/s for only one product .. not all images in that table? I'm confused in how to make this condition.. need ur help guys :?
Here's my code:
File1: imageUpload.php
<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("myDB");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO Images(imageType ,imageData, product_id)
VALUES('{$imageProperties['mime']}', '{$imgData}','{$_POST['memids']}')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" .
mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post"
class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" required/>
<input type="submit" name="submit" value="Submit" class="btnSubmit" /><br><br>
</form>
</div>
</BODY>
</HTML>
File2: listImages.php
<?ob_start()?>
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("myDB");
$sql = "SELECT imageId FROM Images
ORDER BY imageId DESC";
$result = mysql_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
while($row = mysql_fetch_array($result)) {
?>
<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?> "width="200" /><br/>
<?php
}
mysql_close($conn);
?>
</BODY>
</HTML>
<?ob_flush()?>
File3: imageView.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("myDB") or die(mysql_error());
if(isset($_GET['image_id'])) {
$sql = "SELECT imageType,imageData FROM Images WHERE imageId=" . $_GET['image_id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["imageData"];
}
mysql_close($conn);
?>
Related
I am trying to execute below code but it gives me an error that 'trying to get property of non-object in c....' .
This code should pull up info about 'images' and 'texts' to be displayed on the index.php page. I have tried in all means but couldn't figure out what is the problem; I am a beginner in PHP by the way :) .I will appreciate if you please help me.
<!DOCTYPE html>
<?php
$alert = "";
//if upload button is pressed
if(isset($_POST['upload'])){
//the path to store the uploaded image
$target = "images/".basename($_FILES['image']['name']);
//connect to the database
$conn = new mysqli('localhost', 'imgcms', '', '');
//Get all the submitted data from thye form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO images (image, text) VALUES ('$image', '$text')";
//Move the uploaded image into the folder: images
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)){
$alert = "Image uploaded successfully";
}else{
$alert = "There was a problem uploading the image";
}
}
?>
<html>
<head>
<title>ImageBlogger</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="content">
<?php
//connect to the database to display image from the database
$conn = new mysqli('localhost', 'imgcms', '', '');
$sql = "SELECT * FROM images";
$result = $conn->query($sql);
if($result->num_rows > 0){
//output data of each row: image and text
while($row = $result->fetch_assoc()){
echo "<div id='img_div'>";
echo "<img src='images/".$row['image']."'>";
echo "<p>".$row['text']."</p>";
echo "</div>";
}
}else{
echo "0 results";
}
$conn->close();
?>
<form action="index.php" method="post" autocomplete="off" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols"40" rows="4" placeholder="Content..."></textarea>
</div>
<div>
<input type="submit" name="upload" value="Post the content">
</div>
</form>
</div>
</body>
</html>
Copying your code into an editor it looks like line 47 is;
if($result->num_rows > 0)
Before this line add the following and see if you get an error.
if (!$result) {
echo 'Query Error is: ' . $conn->error;}
I am brand new to PHP, or database programming in general. For a project I have to query a bookstore database for book info (a very small database) and display it on the following page. Below is the code for my bookstore search page:
<?php
$con = mysqli_connect("localhost", "root", "root") or die("Error connecting to database: ".mysqli_error());
mysqli_select_db($con, "bookstore") or die(mysqli_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Book Store</title>
</head>
<body>
<td><h1>Book Search</h1> </td>
<table width="100%" border="0">
<tr>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search By Title">
</form>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="category">
<input type="submit" name="submit" value="Search By Category">
</form>
</tr>
</table>
</body>
</html>
And the following is a simple search.php code that query's my database and returns results. However I am unable to see any results. The only thing that shows up is "Book Title Search Results" with nothing below. Which obviously means my problem is in my while loop.
<?php
$con = mysqli_connect("localhost", "root", "root") or die("Error connecting to database: ".mysqli_error());
mysqli_select_db($con, "bookstore") or die(mysqli_error());
?>
<!DOCTYPE html>
<html>
<head>
<title>Search Results</title>
<meta http-equic="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
if (isset($_POST['name'])){
$query = $_POST['name'];
$sql = mysqli_query($con, "SELECT * FROM books
WHERE (`title` LIKE '%".$query."%')") or die(mysqli_error($con));
if (mysqli_num_rows($sql) > 0) {
echo "</br> Book Title Search Results </br>";
while ($row = mysqli_fetch_array($sql, MYSQL_ASSOC)) {
echo "</br>Title: " .$row['title']. ", Author: " .$row['author'].", Year: " .$row['year'] . ", Price: $" .$row['price'] ."</br>";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['cover'] ).'"/>';
}
}else{ // if there is no matching rows do following
echo "No results";
}
}
?>
</body>
</html>
I have 6 columns in my database: title, author, year, price, category, image (BLOB FILE), and I have checked naming in my query functions but cannot figure anything out. Can anyone push me in the right direction or show me what I'm doing wrong? I'm using MAMP web server.
There is a typo in your code. Use MYSQLI_ASSOC instead of MYSQL_ASSOC. The rest of the code is correct.
try
$con = mysqli_connect("localhost", "root", "root", "bookstore") or die("Error connecting to database: ".mysqli_error());
In your query, remove the braces after the where:
$sql = mysqli_query($con, "SELECT * FROM books WHERE title LIKE '%$query%'") or die(mysqli_error($con));
Then fetch the results with:
while ($row = mysqli_fetch_assoc($sql)) {
//code
}
I have code for image upload and view in php and MySQL. After click on "Submit" button in "imageUpload.php" page image is stored in database. but not displaying in "listImages.php" page. I don't know what's the problem. I see "image not displaying when uploading in php" but its seems different solution for me. here is my code please have a look where i am wrong.
imageUpload.php :
<?php
/* CREATE TABLE IF NOT EXISTS `output_images`
(
`imageId` tinyint(3) NOT NULL AUTO_INCREMENT,
`imageType` varchar(25) NOT NULL DEFAULT '',
`imageData` mediumblob NOT NULL,
PRIMARY KEY (`imageId`)
) */
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysqli_connect("localhost", "root", "");
mysqli_select_db ("test");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData)
VALUES('{$imageProperties['mime']}', '{$imgData}')";
$current_id = mysqli_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>
listImages.php :
<?php
$conn = mysqli_connect("localhost", "root", "");
mysqli_select_db("test");
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";
$result = mysqli_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>
<?php
}
mysqli_close($conn);
?>
</BODY>
</HTML>
imageView.php :
<?php
$conn = mysqli_connect("localhost", "root", "");
mysqli_select_db("test") or die(mysqli_error());
if(isset($_GET['image_id'])) {
$sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
$result = mysqli_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysqli_error());
$row = mysqli_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["imageData"];
}
mysqli_close($conn);
?>
this would help you
<a href="imageView.php?image_id=<?php echo $row["imageId"]; ?>">
<img src="<?php echo $row['imagedata']; ?>" alt="my picture" height="128" width="128" />
</a>
it should be
$conn=mysqli_connect("ur_servername_ex_localhost","ur_username","ur_password","ur_db");
mysqli_query($conn, $sql);
I am trying to insert and retrieve image from database. All is going well and image is being inserted in database in BLOB format, but when I am trying to retrieve those images it does not appear and only broken icon comes to webpage. The code is given below. Please help.
ImageUpload.php
<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("connection");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData)
VALUES('{$imageProperties['mime']}', '{$imgData}')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>
ListImages.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("connection");
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";
$result = mysql_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
while($row = mysql_fetch_array($result)) {
?>
<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>
<?php
}
mysql_close($conn);
?>
</BODY>
</HTML>
imageView.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("connection") or die(mysql_error());
if(isset($_GET['image_id'])) {
$sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["imageData"];
}
mysql_close($conn);
?>
I have written a very simple PHP to search for a record in a field on MySQL database and return all details for that field.
Unfortunately, every time I enter the number to search for anything, nothing comes back, not even any errors.
My Code below:
<?php
mysql_connect("localhost", "root", "") or die("cant connect to db");
mysql_select_db("db name")or die("cant connect.....");
$output='';
//collect
if(isset($_POST['search'])){
$searchq=$_POST['search'];
$query = mysql_query("SELECT * FROM register WHERE licence LIKE '%$searchq%'") or die("cant search....");
$count = mysql_num_rows($query);
if($count==0){
$output='no results';
} else {
while($row=mysql_fetch_array($query)){
$fdriver=$row['driver'];
$flicence=$row['licence'];
$fofficer=$row['officer'];
$fspeed=$row['speed'];
$ffine=$row['fine'];
$fcategory=$row['category'];
$output.='<div> '.$fdriver.' '.$flicence.' '.$fspeed.' '.$ffine.' '.$fcategory.'</div>';
}
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search</title>
</head>
<body>
<h3>Search</h3>
<p>You have to enter your number to search</p>
<form method="post" action="search_start.php" >
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php print("$output"); ?>
</body>
</html>
first change the name of your textbox to search
<input type="text" name="search">
second your query should be like this
$query = mysql_query("SELECT * FROM register WHERE licence LIKE '%".$searchq."%'");
You're checking $_POST['search'], so the name of your text element field should be changed to search.
<input type="text" name="search">