Jump to content

Images unlinking but not replaced.


laurenetherington
Go to solution Solved by laurenetherington,

Recommended Posts

Hello Happy Campers.

Wonder if someone can point me in the right direction if it's not too much trouble.

 

I have a script that allows an EU to edit a database entry. The users edits the information and hits submit which then edits the content and it all works spiffingly.

My problem however arises when I go to upload a new image.

The "Add" function uploads images to a directory and the location is saved in the database.

 

When editing the image however, it unlinks it but it does not allow me to upload a new image.

 

The code is as follows:

ini_set('display_errors',1);
error_reporting(E_ALL);

$conn = mysqli_connect("HOST","DB","PWRD","DBTBL")
	or die('Cannot Connect to the database');

$i = mysqli_real_escape_string($conn,$_POST['newsid']);
$t = mysqli_real_escape_string($conn,$_POST['title']);
$st = mysqli_real_escape_string($conn,$_POST['stat']);
$sn = mysqli_real_escape_string($conn,$_POST['snip']);
$s = mysqli_real_escape_string($conn,$_POST['stry']);
$c = mysqli_real_escape_string($conn,$_POST['cap']);
$f = $_POST['oldim'];

$s = nl2br($s);

	if(!is_uploaded_file($_FILES['file']['tmp_name']))
	{
		$naquery = "UPDATE news SET newstitle='$t',newssnip='$sn',newsarticle='$s',newsstatus='$st' WHERE newsid=$i";

	}
else
{
	if ($_FILES['file']['type'] != "image/gif"  && $_FILES['file']['type'] != "image/jpeg"  && $_FILES['file']['type'] != "image/jpg"  && $_FILES['file']['type'] != "image/x-png"  && $_FILES['file']['type'] != "image/png")
	{
		$naquery = "UPDATE news SET newstitle='$t',newssnip='$sn',newsarticle='$s',newsstatus='$st' WHERE newsid=$i";

	}
	else
	{
		$finame = $_FILES["file"]["name"];
		$result = move_uploaded_file($_FILES['file']['tmp_name'], "../news/$finame");
		if ($result == 1)
			{
				$naquery = "UPDATE news SET newstitle='$t',newssnip='$sn',newsarticle='$s',newsimage='$finame' newscaption='$c',newsstatus='$st' WHERE newsid=$i";
				$d = "../news/";
				unlink("$d$f");
			}
		else
			{
				$naquery = "UPDATE news SET newstitle='$t',newssnip='$sn',newsarticle='$s',newsstatus='$st' WHERE newsid=$i";
			}
	}
}

$result = mysqli_query($conn, $naquery);

if($result){

	header('Location: ../news.php');

}

else {
	
	echo "Oh No! Something has gone wrong and the data could not be uploaded";
	echo "<br />";
	echo "click <a href='Link'>here</a> to return to News";
}

mysqli_close($conn);

I am not getting an error message from PHP, I am just getting the generic "Something has gone wrong" that I have coded in myself.

 

Is there anyone who can point me in the right direction please?

 

Cheers

Link to comment
Share on other sites

  • Solution

I have realised my mistake.

There is a comma missing from the query:

$naquery = "UPDATE news SET newstitle='$t',newssnip='$sn',newsarticle='$s',newsimage='$finame' newscaption='$c',newsstatus='$st' WHERE newsid=$i";

Should be

$naquery = "UPDATE news SET newstitle='$t',newssnip='$sn',newsarticle='$s',newsimage='$finame', newscaption='$c',newsstatus='$st' WHERE newsid=$i";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.