Fishcakes Posted March 26, 2021 Share Posted March 26, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312386-updating-sql-code-is-not-working-via-php/ Share on other sites More sharing options...
Barand Posted March 26, 2021 Share Posted March 26, 2021 12 minutes ago, Fishcakes said: set thumbnail ??? where Set thumbnail to what? Quote Link to comment https://forums.phpfreaks.com/topic/312386-updating-sql-code-is-not-working-via-php/#findComment-1585413 Share on other sites More sharing options...
gw1500se Posted March 26, 2021 Share Posted March 26, 2021 Because your query is failing so $update is false. Your query syntax is wrong and if you had error reporting on you would have seen that: error_reporting(E_ALL); You are not setting thumbnail to anything. Quote Link to comment https://forums.phpfreaks.com/topic/312386-updating-sql-code-is-not-working-via-php/#findComment-1585414 Share on other sites More sharing options...
Barand Posted March 26, 2021 Share Posted March 26, 2021 Erm, You need to use mysql error reporting to see that error, not PHP error reporting. Quote Link to comment https://forums.phpfreaks.com/topic/312386-updating-sql-code-is-not-working-via-php/#findComment-1585415 Share on other sites More sharing options...
gw1500se Posted March 26, 2021 Share Posted March 26, 2021 Sorry. My post got messed up. I meant to add: After the query: print_r($conn->errorInfo()); Quote Link to comment https://forums.phpfreaks.com/topic/312386-updating-sql-code-is-not-working-via-php/#findComment-1585418 Share on other sites More sharing options...
Barand Posted March 26, 2021 Share Posted March 26, 2021 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); Quote Link to comment https://forums.phpfreaks.com/topic/312386-updating-sql-code-is-not-working-via-php/#findComment-1585421 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.