0

I am trying to make a PHP form that will only allow the user to update the MySQL Table column photo, if the photo column is blank. Currently, the form will still update the photo column even if there is data other than "blank" data. For example, the photo column contains the data "columbia.jpg" and the user submits the form with the image "Jefferson.jpg" in the first input. The image column's data gets replaced from columbia.jpg to jefferson.jpg when it is not supposed to replace it at all. Instead it should return an error message stating that the user must first delete the old image before adding a new one. The column data should only get replaced when the column data is equal to "blank". (Not the word "blank" but "".)

Here is my full PHP page code:

<?php
if (isset($_GET["id"])) {
    $sn = (int)($_GET["id"]);
?>
<!DOCTYPE html>
<head>
    <title>MySQL file upload example</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    <form action="<?php $_PHP_SELF ?>" method="post" enctype="multipart/form-data">
        Photo 1: <input type="file" name="photo"><br>
        <input name="add_image" id="add_image" type="submit" value="Upload file">
    </form>
    <p>
        <a href="pdfget.php">See all files</a>
    </p>
</body>
</html>

<?php
if(isset($_POST['add_image']))
{
$dbLink = new mysqli('daom', 'sm', 'aer', 'kabm');

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 
 $pic=($_FILES['photo']['name']); 

$query = "SELECT photo FROM used_trailers WHERE id = $sn";
$result = mysqli_query($dbLink, $query);
$array=mysqli_fetch_assoc($result);

if($query = " "){
//Writes the information to the database 
        $query1 = 
            "UPDATE used_trailers ".
       "SET photo = '$pic' ".
       "WHERE id = $sn" ;
        // Execute the query
        $results = $dbLink->query($query1);

        // Check if it was successfull
        if($results) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded to Photo 1, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your image."; 
 }
} else {
echo '<p>You must first delete the "Photo 1" image, $array, in order to add a new photo. This is to prevent the server from overloading with too many images.</p>';
}
}
}
               echo "$query1";
?>

Thank you for any help. All help is appreciated.

5
  • 2
    Sidenote: Are you sure you have your bracing done correctly? I.e.: if (isset($_GET["id"])) { $sn = (int)($_GET["id"]); it's closing at the end of your code without anything in between to make it come in and out of PHP. Commented Feb 19, 2014 at 19:39
  • Regardless of bracing, the code is functioning as purposed. Commented Feb 19, 2014 at 19:47
  • Ok. I thought it might have had something to do with it. Glad you found your solution below. Commented Feb 19, 2014 at 19:51
  • 1
    Thanks for the heads up, though. Cheers. Commented Feb 19, 2014 at 19:54
  • You're welcome Michael, cheers! Commented Feb 19, 2014 at 19:56

1 Answer 1

1

There are some errors in your script. First of all if ($query = " ") will always return true, because you are assigning the variable $query the string " ". To correctly check this, you'd need to use if ($query == " ").

However, this won't solve your problem as $query is the query - not the result. This should work

$query = "SELECT photo FROM used_trailers WHERE id = $sn";
$result = mysqli_query($dbLink, $query);
$array = mysqli_fetch_assoc($result);

if (empty($array['photo'])){
    //etc.
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.