How to update longblob image field in mysql using php - php

if(isset($_POST['submit']) and isset($_GET['slider_id']))
{
$date=date('Y-m-j');
$imgName=$_FILES['image']['name'];
$cont=file_get_contents($imgName);
$cont=addslashes($cont);
if($imgName=="")
{
//$imgData =addslashes(file_get_contents($_FILES['image']['name']));
$res=mysqli_query($connect,'UPDATE `slider_images` SET `image`=\''.$cont.'\' WHERE id=\''.$_GET['slider_id'].'\'');
if($res)
{
echo "Updated";
}
else
{
echo "Not Updated";
}
}
}
Not understanding the real issue behind this and i have refereed many solution's but no success in that.All solution's i found they tell to store images in folder and store the file name in database table.Reason behind storing images in database is, only 4 images are to be stored, so why not to store them in database. Please guide me through this issue. Following is the issue i am talking about.
Warning Message
Thank's in advance.

$_FILES['file']['name'] is the original name of the uploaded file from the user's computer.
$_FILES['file']['tmp_name'] will contain the temporary file name of the file on the server. This is just a temporary placeholder until you process the file.
So you should access the file like this:
$cont=file_get_contents($_FILES['image']['tmp_name']);
Sidenote: Instead of if($res){ ... } use mysqli_affected_rows() to get number of rows affected by this UPDATE query, like this:
mysqli_query($connect,"UPDATE `slider_images` SET `image`='".$cont."' WHERE id='".$_GET['slider_id']."'");
if(mysqli_affected_rows($connect)){
echo "Updated";
}else{
echo "Not Updated";
}
Here's the reference:
mysqli_affected_rows()

Related

php upload file path to mysql

So, I am creating a very simply uploader where I take in a file/name/description and to store to store the file to the server and name/desc/filepath.
This is what my database looks like:
Basically, i take in a file, name, and description and want the file to be stored in the server. While the path/name/desc to be stored into mySQL. I also want an incrementing ID. I have a db named "test". And want to post to "test_table". The database is already created but I need to check if the table exists, if not, create it. I think I have the basics of mySQL correct below, but I need to know how to check for table/create if needed and how to set the path variable. Thanks in advance!
Also, how do I increment the ID variable in mysql with each entry? does that happen automatically or .. ?
solved
you need to upload your file to the server before inserting in the database:
if ($_FILES['fileinput']['name']!="") {
if (is_uploaded_file($_FILES['fileinput']["tmp_name"])) {
$name = date("Y-m-d : H:i:s")." ".$_FILES['fileinput']['name'];
$filevalue = $nameoffile;
$path =$file_save_path.$nameoffile;
if (move_uploaded_file($_FILES["file".$i]["tmp_name"], $uploaddir.$nameoffile)) {
$sql = "INSERT INTO test_table (name, desc, path) VALUES ($name, $desc, $path)";
mysqli_query($conn, $sql)
} else {
echo "File Upload Error. Please Try again";
}
}
}
To check, if a table exists, see this question: check if MySQL table exists or not
Essentially, this is the code to check:
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1)
echo "Table exists";
else echo "Table does not exist";
To autoinc the ID just use the autoincrement attribute to the ID field, see the documentation here for more info: https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
Code:
ALTER TABLE test_tabel MODIFY COLUMN ID INT auto_increment
Hope this helps...

unlink() function don't work without any error

i have problem with my code and unlink function
here my code
if(isset($_GET['delimg'])){
$id= $_GET['delimg'];
$sql = "delete from images_img where id='$id'";
$res=mysqli_query($con,$sql);
$getname="select * from images_img";
$res2=mysqli_query($con,$getname);
$image=mysqli_fetch_array($res2);
$image1=$image['image_url'];
$image2=$image['image_url_big'];
unlink('../../images/photo'.$image1);
unlink('../../images/photo'.$image2);
}
when i run my delete.php in the database the images deleted just fine
but the unlink function don't work and don't delete anything from the path and no error shows !
my thought that the select way return empty value ! as i use
this $image=mysqli_fetch_array($res2); to select the images name
so any error with my code please ?
UPDATE ::
i'm sure that the delete excuted before selecting the data for name to delete >>
so how i would arraying the order ?
UPDATE 2 :::
this is the upload code
$nameimg=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$type=$_FILES['image']['type'];
$size=$_FILES['image']['size'];
$dir="/images/photo/";
if($_POST['upload']){
if(!empty($nameimg)){
if(in_array($type,array('image/png','image/jpg','image/gif','image/jpeg'))){
if(filesize($tmp) <= 20242880){
move_uploaded_file($tmp,$dir.$nameimg) ;
$done ="done";
}
else {$errorsize= "it's bigger than the allowed size";}
}
else {
$errortype= "the file not image,please choose image to upload";
}
}
if(empty($nameimg)){$errorchoose= "no file choosen,please choose file";}
}
echo "<meta http-equiv='refresh' content='5;url=../../admin.php#ajax/add_img.php'>";
?>
check with file exists function like below also enable php errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
$imgPath = ''../../images/photo'.$image1';
if (file_exists($imgPath)) {
unlink($imgPath);
} else {
echo "not exists";
}
ooh it's worked finally !!!
the all problem .. all time we have spend it was for nothing !
from first time and my first code it was work just fine and every code i have given by you guys it was worked .. but just we all [except "Parasad"] forgot [/] after "photo" !!!!!!! to be like this
unlink('../../images/photo/'.$image1)
not like this
unlink('../../images/photo'.$image1)
really i make you guys work hard to try slove my problem and i take your time
so million huge thanks to all of you specially [WordpressCoder] .. thanks a lot
Copy this code and tell me whether it is working or not?
if(isset($_GET['delimg'])){
$id= $_GET['delimg'];
$getname="select * from images_img where id='$id'";
$res2=mysqli_query($con,$getname);
$image=mysqli_fetch_array($res2);
$image1=$image['image_url'];
$image2=$image['image_url_big'];
$sql = "delete from images_img where id='$id'";
$res=mysqli_query($con,$sql);
unlink('../../images/photo/'.$image1);
unlink('../../images/photo/'.$image2);
}
if(isset($_GET['delimg'])){
$id= $_GET['delimg'];
$sql = "delete from images_img where id='$id'";
$res=mysqli_query($con,$sql);
$getname="select * from images_hair";
$res2=mysqli_query($con,$getname);
$image=mysqli_fetch_array($res2);
$image1=$image['image_url'];
$image2=$image['image_url_big'];
unlink('../../images/photo/'.$image1);//check your url
unlink('../../images/photo/'.$image2);//check your url
}

delete an image file using php

I am building a forum with php and MySQL and I want to append current time to each image that users upload for their profile. I used time() function on each image file uploaded and it worked for the image file but I have trouble inserting the new filename into the database. I wanted to give each image a unique name to prevent override.
OK here is what I did: I stored the current time as $time and the filename in a variable, $photo and I tried to insert that variable’s value using $photo .= $time into the database so it has the filename as I did with each uploaded image. However the original filename is inserted into the database in every attempt.
Any workarounds?
$image = $_FILES['photo']['name'];
$time = time();
$image .= $time;
delete the existing photo
delete(image_dir/$row['current_photo.jpg']);
//does not work, but i want something like that
if(move_uploaded_file($_FILES['photo']['tmp_name'], 'image_dir/$image') {
$query = "INSERT INTO profile (user_id, profile_photo) VALUES($id, $image)";
mysqli_query($dbc, $query);
if(mysqli_affected_rows($dbc) == 1) {
echo "added to the database!";
}else {
echo "failed to add photo to the database";
}
}else {
echo "failed to upload photo";
}
how can i give the uploaded image unique the name since the original image name gets inserted in the database in every try i make?
i know the code looks funny :). just want to show the logic.
If you need a unique id, you can use the uniqid function
$name=uniqid();
you may need to use
$filename=uniqid();

MYSQL table won't update

I am building a profile pictures system and for some reason my table doesn't seem to be working with the MYSQL UPDATE query. Below you should just select your image and click upload, it moves the image to the folder but not the directory to the database.
Help please:
if (file_exists("userdata/profile_pics/".#$_FILES["profilepic"]["name"]))
{
echo #$_FILES["profilepic"]["name"]." Already exists";
}
else
{
move_uploaded_file(#$_FILES["profilepic"]["tmp_name"],"userdata/profile_pics/".$_FILES["profilepic"]["name"]);
echo "Uploaded and stored in: userdata/profile_pics/".#$_FILES["profilepic"]["name"];
$profile_pic_name = #$_FILES["profilepic"]["name"];
$profile_pic_query = mysql_query("UPDATE users SET profile_pic='$profile_pic_name' WHERE username={$_SESSION['user_login']}");
}
}
else
{
echo "Invailid File! Your image must be no larger than 1MB and it must be either a .jpg, .jpeg, .png or .gif";
}
}
Try this: add quote to the username variable
$username = $_SESSION['user_login'];
"...WHERE username='$username'";
try session variable put into quotes username='".$_SESSION['user_login']."'

adding user photos to database

my question is on adding the file paths to images to a user's slot in a database. here is a sample, this is the page where the photos are to be chosen:
require_once("script.php");
<form method="post" action='golf.php'>
<?php
require_once("golf_phot.php");
?>
</div>
<input id="butt" type="submit" value="add"/>
</form>
here are the contents of golf_phot.php:
$query="SELECT category_id FROM categories WHERE category_name='golf'";
$resultt=mysql_query($query);
$row=mysql_fetch_array($resultt);
$do=$row['category_id'];
$query2="SELECT photo FROM all_photos WHERE all_photos.category_id='$do'";
$result=mysql_query($query2);
while ($row=mysql_fetch_array($result))
{
$photo=$row['photo'];
echo "<input type=\"checkbox\" name=\"addlist[]\" value=\"".$photo."\">"."<img src=\"".$photo."\">";
echo "<br>";
echo "<br>";
echo "<br>";
}
i already did the mysql connection and database selection, i won't post that but it has no issues. here are the contents of "script.php":
if(isset($_POST['addlist']))
{
$pics=$_POST['addlist'];
$n=count($pics);
//now, to open the database user_info, using the stored session
//make the session variable legit
if(isset($_SESSION['user']))
{
$user=$_SESSION['user'];
}
//get the user's id from the user_info table
$querya="SELECT * FROM user_info WHERE user_info.fb_id='$user'";
$result1=mysql_query($querya);
//now that we have the id, we can add the photos to
//user_photos, using their id as a foreign key
if(mysql_num_rows($result1)>0){
$rowa=mysql_fetch_array($result1);}
$user_id=$rowa['USER_INFO_ID'];
//before we add them, we'll need to see whether they exist or not.
//adding time:
for($i=0;$i<$n;$i++){
$data=$pics[$i];
$queryb="SELECT * FROM user_photos";
$result3=mysql_query($queryb);
if(mysql_num_rows($result3)>0){
while($rowa=mysql_fetch_array($result3))
{
$data2=$rowa['USER_PHOTOS'];
if($data==$data2)
{ $var='exists'; }
else{
$queryb="INSERT INTO user_photos(USER_PHOTOS_ID,USER_INFO_ID,USER_PHOTOS) VALUES('NULL','".$user_id."','".$data."')";
$result2=mysql_query($queryb);
}
}
}
}
// selected photos added to user's gallery!
}
the user data variables are all set as i use them elsewhere and they are successfuly added to the database, above, i include a way to see if the photo already exists, i don't add it if it does(maybe there's an issue with that?) so basically, the page with photos is loaded, the user checks on the photos they want and then they send it to the same page, where "script.php" is supposed to process the data and add it to the database. no photos are added to the database, i really can't tell what's the issue here. i kindly ask for help, even though this is probably an easy question, if you need me to clarify something, kindly ask so, meanwhile, can anybody help? thanks in advance.
One solution for check the picture is same or not is, using a hash algorithm on the pictures to determined the same pics.
i.e: (pseudo code)
$hash=sha1_file('pic-filename');
$sql='select * from image_table where hash_col ='.$hash;
if(num_rows($sql)>0)
//don't save pic.
else
// save the pic and it's hash value.
hash_col is a column in your image table that get the hash value of the particular image.
Please Try this code in your script.php. hoping this may work for you.
I Have removed the condition if(mysql_num_rows($result3)>0){
Code:
if(isset($_POST['addlist']))
{
$pics=$_POST['addlist'];
$n=count($pics);
//now, to open the database user_info, using the stored session
//make the session variable legit
if(isset($_SESSION['user']))
{
$user=$_SESSION['user'];
}
//get the user's id from the user_info table
$querya="SELECT * FROM user_info WHERE user_info.fb_id='$user'";
$result1=mysql_query($querya);
//now that we have the id, we can add the photos to
//user_photos, using their id as a foreign key
if(mysql_num_rows($result1)>0){
$rowa=mysql_fetch_array($result1);}
$user_id=$rowa['USER_INFO_ID'];
//before we add them, we'll need to see whether they exist or not.
//adding time:
for($i=0;$i<$n;$i++){
$data=$pics[$i];
$queryb="SELECT * FROM user_photos";
$result3=mysql_query($queryb);
while($rowa=mysql_fetch_array($result3))
{
$data2=$rowa['USER_PHOTOS'];
if($data==$data2)
{ $var='exists'; }
else{
$queryb="INSERT INTO user_photos(USER_PHOTOS_ID,USER_INFO_ID,USER_PHOTOS) VALUES('NULL','".$user_id."','".$data."')";
$result2=mysql_query($queryb);
}
}
}

Categories