Jump to content

Updating SQL code is not working via php


Fishcakes

Recommended Posts

Hi

So my thumbnail upload.php file is now working. However I'd like to understand why my code isn't being run here.

//Update SQL db by setting the thumbnail column to equal $Thumbnail
$update = $conn->query("update Threads set thumbnail where filename = '$Thumbnail'");
            if($update){
               $statusMsg = "Updated the thumbnail to sql correctly.";
        echo "\nUpload thumbnail if statement" ;  }

It doesn't echo out the echo statement so it's not hitting the if statement?

My full upload.php file is below

Initially I tried to update the Thumbnail into SQL in the insert into statement... but that didn't work so figured maybe I'd just enter it afterward.

<?php

$servername = "localhost";
$username = "user";
$password = "password";
$db = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
else { echo "";}

$statusMsg = '';

$Title = $conn -> real_escape_string($_POST['Title']) ;
    $BodyText = $conn -> real_escape_string($_POST['ThreadBody']) ;
    

// File upload path
$targetDir = "upload/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
$Thumbnail = "upload/Thumbnails/'$fileName' ";

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','gif','pdf');
    if(in_array($fileType, $allowTypes)){

        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){

            // Insert image file name into database
            $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')");
            if($insert){
               $statusMsg = "The file ".$fileName. " has been uploaded successfully.";

$targetFilePathArg = escapeshellarg($targetFilePath);
$output=null;
$retval=null;
 //exec("convert $targetFilePathArg -resize 300x200 ./upload/Thumbnails/'$fileName'", $output, $retval);
 exec("convert $targetFilePathArg -resize 300x200 $Thumbnail", $output, $retval);
echo "REturned with status $retval and output:\n" ;
if ($retval == null) {
echo "Retval is null\n" ;
echo "Thumbnail equals $Thumbnail\n" ;

}

            }else{
                $statusMsg = "File upload failed, please try again.";
            }
        }else{
            $statusMsg = "Sorry, there was an error uploading your file.";
        }
    }else{
        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
    }
}else{
    $statusMsg = 'Please select a file to upload.';
}

//Update SQL db by setting the thumbnail column to equal $Thumbnail
$update = $conn->query("update Threads set thumbnail where filename = '$Thumbnail'");
            if($update){
               $statusMsg = "Updated the thumbnail to sql correctly.";
        echo "\nUpload thumbnail if statement" ;  }


// Display status message
echo $statusMsg;
?>

Attached a shot of my db so you can see it's not updating the thumbnail column

db.png

Link to comment
Share on other sites

Or, as the OP is using mysqli

print_r($conn->error_list);

However a better way is to set mysql error reporting when you make the connection to the server EG

    mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); 
    $conn = mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
    $conn->set_charset('utf8');

If using PDO (recommended) the equivalent is to set the errmode option on connecting

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 

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.