unlink() function don't work without any error - php

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
}

Related

PHP How to validate URL with id ungiven parameter?

I have a website where I can show a single post with view_post.php?id=1 but when I try to show a post with for example view_post.php?id= or view_post.php?id it shows nothing.
Then I tried to Log the Database Query like this:
$query = "SELECT * FROM posts WHERE id='$postIdFromUrl'";
$stmt = $db->query($query);
if (!$stmt) {
$_SESSION["test"] = $stmt;
redirect_to("Blog.php");
}
It doesnt show anything as the query is valid.
So my question is how to validate that. I tried a lot of things with e.g. URL Validation... nothing worked.
I would be very happy if someone could send me a code that can check for that.
Thanks very much in advance!
just check if id exists or its empty before u do anything
if (isset($_GET['id']) && !empty($_GET['id'])) {
//do your original code
} else {
redirect_to("Blog.php");
}

How to update longblob image field in mysql using 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()

Delete Query returning Value even when there is no record in database

<?php
include "conn.php";
include "session.php";
$name_enterd=$_GET['Name'];
$sql = "DELETE FROM myDB.Mynew WHERE firstname='$name_enterd' OR lastname='$name_enterd'";
echo "<br>";
$result=$conn->query($sql);
if($result==1)
{
echo "<br> Data deleted successfully";
}
else
{
echo "No Data Found<br>";
}
?>
when I run this code 1st time it works properly by deleting the data. But when i run it again it still gives me the same answer" Data Deleted Successfully" even there is no data with that value exists.
i.e $result still gets value1.
Your code should look more like this:
<?php
include "conn.php";
include "session.php";
$name_enterd=$_GET['Name'];
$sql = "DELETE FROM myDB.Mynew WHERE firstname='$name_enterd' OR lastname='$name_enterd'";
echo "<br>";
$result=$conn->query($sql);
if($result->rowCount() > 0)
{
echo "<br> Data deleted successfully";
}
else
{
echo "No Data Found<br>";
}
?>
Specifying rowCount gives you just the number of rows affected by the query
Even when the query only affects 0 rows it has still completed successfully, so you would expect $result to be 1.
You are getting the correct output. When doing that query, you're asking the database to check if there is data with that firstname or lastname and delete it. Even if there is no data with that matches it, the query has still run successfully.
You need to do use
$result->rowCount() == 1
instead of
$result == 1
It really depends what you want to use the result for. If you simply want to tell the user it has been deleted, using what you have is fine. However, if you want to let the user knows if anything has actually been deleted, you need to use my suggestion above or an alternate method to determine if this is the case.
Actually it looks like you might be using mysqli in this code, so maybe you could try using affected_rows instead of rowCount:
see http://php.net/manual/en/mysqli.affected-rows.php.
What does
$result->affected_rows
give you?

MYSQL Tables Picky About Fields?

I am having issues with php and mysql once again. I have a database setup with the table users and I want to make a SELECT COUNT(*) FROM users WHERE {value1} {value2} etc...but the problem is that the 3 fields I want to compare are not in order in the table and when trying the SELECT query, the result vairable($result) is NOT returned properly(!$result). Is there a way to check multiple fields in a mysql table that have fields in between them? Here is an example of what I want to accomplish:
A mysql table called users contains these fields: a,b,c,d,e,f,g,h,i,j,k,l and m.
I want to make a SELECT COUNT(*) FROMusersWHERE a='$_SESSION[user]' and d='$_SESSION[actcode]' and j='$_SESSION[email]' but the statement in quotes is my query and it always executes the if (!$result) { error("An error has occurred in processing your request.");} statement. What am I doing wrong? On the contrary, whenever I try the statement using only one field, ex a, the code works fine! This is an annoying problem that I cannot seem to solve! I have posted the code below, also note that the error function is a custom function I made and is working perfectly normal.
<?php
include "includefunctions.php";
$result = dbConnect("program");
if (!$result){
error("The database is unable to process your request at this time. Please try again later.");
} else {
ob_start();
session_start();
if (empty($_SESSION['user']) or empty($_SESSION['password']) or empty($_SESSION['activationcode']) or empty($_SESSION['email'])){
error("This information is either corrupted or was not submited through the proper protocol. Please check the link and try again!");
} elseif ($_SESSION['password'] != "password"){
error("This information is either corrupted or was not submited through the proper protocol. Please check the link and try again!");
} else {
$sql = "SELECT * FROM `users` WHERE `username`='$_SESSION[user]' and `activationcode`='$_SESSION[activationcode]' and `email`='$_SESSION[email]'";/*DOES NOT MATTER WHAT ORDER THESE ARE IN, IT STILL DOES NOT WORK!*/
$result = mysql_query($sql);
if (!$result) {
error("A database error has occurred in processing your request. Please try again in a few moments.");/*THIS IS THE ERROR THAT WONT GO AWAY!*/
} elseif (mysql_result($result,0,0)==1){/*MUST EQUAL 1 OR ACCOUNT IS INVALID!*/
echo "Acount activated!";
} else {
error("Account not activated.");
}
}
}
ob_end_flush();
session_destroy();
?>
Try enclosing your $_SESSION variables in curly brackets {} and add or die(mysql_error()) to the end of your query -
$sql = "SELECT * FROM `users` WHERE `username`='{$_SESSION['user']}' and `activationcode`='{$_SESSION['activationcode']}' and `email`='{$_SESSION['email']}'";/*DOES NOT MATTER WHAT ORDER THESE ARE IN, IT STILL DOES NOT WORK!*/
$result = mysql_query($sql) or die(mysql_error());
store your session value in another varibles then make query , i think
it's work proper
$usr=$_SESSION['user'];
$acod=$_SESSION['activationcode'];
$eml=$_SESSION['email'];
$sql = "SELECT * FROM `users` WHERE `username`='$usr' and `activationcode`='$acod' and `email`='$eml'";
$result = mysql_query($sql) or die(mysql_error());

PHP & MYSQL Return blank page

I'm working on the PHP and MYSQL for a project and I encountered a weird problem here where I click on the submit button at the form, it will run these codes. However the weird problem is the the page return blank instead of going back to the page with the form. I had searched for few hours for the error but couldn't find it.
Please point out my mistake. Thank you for the help.
<?php
include '../database.php';
if(isset($_POST['submit'])) {
if (isset($_POST['stuid_0'])){
$student = $_POST['stuid_0'];
//query moderator details
$query = mysql_query(" SELECT ModeratorID FROM Student WHERE StuID ='$student' ") or die(mysql_error());
$info = mysql_fetch_assoc ($query);
$dbmoderator = $info['ModeratorID'];
//check for changes of status in supervisor
$query2 = mysql_query(" SELECT SupervisorID FROM Student WHERE StuID ='$student' ") or die(mysql_error());
$value = mysql_fetch_assoc ($query2);
$dbsupervisor = $value['SupervisorID'];
$query3 = mysql_query(" SELECT LectStatus FROM Lecturer WHERE LectID ='$dbsupervisor' ") or die(mysql_error());
$value2 = mysql_fetch_assoc ($query3);
$dbsupervisorstatus = $value2['LectStatus'];
//if no changes in supervisor
if ($dbsupervisorstatus=='2'){
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Moderator can't be promoted')
window.location.href='../committee/committee_supervisor2.php'
</SCRIPT>");
}
else{
//newly assigned a supervisor if previous supervisor status is not active
$query4 = "UPDATE Student SET SupervisorID='$dbmoderator', SupervisorStatus='1', ModeratorID=NULL WHERE StuID='$student'";
mysql_query($query4);
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Successfully updated')
window.location.href='../committee/committee_supervisor2.php'
</SCRIPT>");
}
}
else
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('You must choose a moderator to be promoted')
window.location.href='../committee/committee_supervisor2.php'
</SCRIPT>");
}
?>
UPDATE:
I think the problem happen when the system run at this point
if ($dbsupervisorstatus=='2'){
As i put an echo "Test"; before this line and it still work.
UPDATE 2:
I found that the code can be run when I put
if ($dbsupervisorstatus=='2'){
echo "Moderator can't be promoted";
}
as well as
if($dbsupervisorstatus == 2){
header("location:commitee_supervisor2.php");
}
However I don't see the reason why my original code
if ($dbsupervisorstatus=='2'){
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Moderator can't be promoted')
window.location.href='../committee/committee_supervisor2.php'
</SCRIPT>");
}
Not working.. A little help pls.. :)
FINAL UPDATE
Guys, I know why.
It's because the
window.alert('Moderator can't be promoted')
have 3 apostrophe in it.
I simply remove the word "can't" and its working already.
Thank you guys for the help :)
Using the JavaScript codes code still work but the starting point of your debugging is to echo ordinary texts at all the places where you echo JavaScript. That will help you know at what point you codes started failing. Example
//if no changes in supervisor
if ($dbsupervisorstatus=='2'){
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Moderator can't be promoted')
window.location.href='../committee/committee_supervisor2.php'
</SCRIPT>");
}
Replace with
//if no changes in supervisor
if ($dbsupervisorstatus=='2'){
echo "Moderator can't be promoted";
}
Do this at all echo points then start replacing one after the other with your JavaScript codes again then you'll spot out where your code fails.
Instead of doing it through javascript, try using a much better approach in pure PHP. Something like:
if($dbsupervisorstatus == 2){
header("location:commitee_supervisor2.php");
}
And if you must use JS, then make an AJAX request, then you can manipulate the behaviour through JS anyway you like.
Where do you put your HTML form? Is it in the same page or different page? From your code, looks like there's nothing being done, so I suggest you to check whether your submit button name is correct (submit). The HTML should be like this:
<form name="your_form">
<!-- Your form here -->
<input type="submit" name="submit" value="Submit"/>
</form>

Categories