Jump to content

Fishcakes

Members
  • Posts

    40
  • Joined

  • Last visited

Posts posted by Fishcakes

  1. Thanks I knew there would be a much better way to do it

    I've tried translating the above to mine but getting an sql error can you see what I'm missing?

    select Threads.id as ThreadId, Title, LEFT(ThreadBody) as body, date_format(Thread_date, '%D %b %Y %l:%i %p') as ftime, count(Posts.IdOfThread) as num_posts from thread Threads Left join post Posts on Threads.id = Posts.IdOfThread group by Threads.id order by Thread_date desc;

     

  2. So I output the Threads for my forum onto the main screen like so 

    <div class ='grid-container'>
    <?php
    
    
    //Get the Threads and output them onto the screen in a grid container
    $query = mysqli_query($conn, "SELECT * FROM Threads order by id desc")
       or die (mysqli_error($conn));
    
    $GetPostsQuery = mysqli_query($conn, "SELECT Count(*) FROM Posts")
       or die (mysqli_error($conn));
    
    while ($row = mysqli_fetch_array($query)) {
            $imageURL = 'upload/Thumbnails/'.rawurlencode($row["filename"]);
    $PostBody = nl2br($row['ThreadBody']);
      echo
      
       "  <div class ='grid-item'>
    	<div class='ThreadComment'> Comments: <br> </div> 
    	<div class='ThreadNumber'> Post {$row['id']}<br> </div> 
          	<h2><a href='viewthread.php?id={$row['id']}'>  {$row['Title']} </a></h2> 
    		<div class ='img-block'> 
    		 <img src={$row['$imageURL']}$imageURL alt='' />		
    		</div> 
    		
           <p>$PostBody </p>
          </div> 
    
        \n";
    }
    ?>

    However I don't keep the count of comments against the threads on my Threads table. I've attached a "describe" of both my comments (named Posts) and my Threads table (name Threads)

    I want to output the count of the comments against each Post in the above While loop

    To obtain the amount of comments against a thread I can get this from SQL by doing

    select count(IdOfThread) from Posts where id='169' ; 

    But how would I access a query in SQL for the Posts table whilst it's already querying the Threads table? How would I implement this into the above while loop?

    Many thanks (and my db design maybe incorrect I'm very new :P )

    posts table.png

    threads table.png

  3. I also tried declaring the variable ($testvar)  outside the echo statement but the page still won't load even then

     

    while ($row = mysqli_fetch_array($query)) {
    //May need this later to output pictures   
    //     $imageURL = 'upload/'.rawurlencode($row["filename"]);
    $testvar = nl2br({$row['CommentText']}) ; 
      echo
    
      
       "  <div class='divTableRow'>
    <div class='divTableCell'>{$row['User']} ;</div>
    <div class='divTableCell'>  $testvar ;</div>
    </div>
    
    
        \n";
    }
    
    echo $_SESSION['id'] ; 
    ?> 

     

  4. Yes I tried this using single quotes which just outputs it as a string

    <div class='divTableCell'> ' . nl2br({$row['CommentText']}) . ';</div>

    And if I use double quotes as you've done it causes my page not to load

     

    while ($row = mysqli_fetch_array($query)) {
    //May need this later to output pictures   
    //     $imageURL = 'upload/'.rawurlencode($row["filename"]);
      echo
      
       "  <div class='divTableRow'>
    <div class='divTableCell'>{$row['User']} ;</div>
    <div class='divTableCell'> " .  nl2br({$row['CommentText']}) . ";</div>
    </div>
    
    
        \n";
    }

     

  5. Hi

    I am trying to use the nl2br function like this

    while ($row = mysqli_fetch_array($query)) {
    //May need this later to output pictures   
    //     $imageURL = 'upload/'.rawurlencode($row["filename"]);
      echo
      
       "  <div class='divTableRow'>
    <div class='divTableCell'>{$row['User']} ;</div>
    <div class='divTableCell'>nl2br({$row['CommentText']});</div>
    </div>
    
    
        \n";
    }

    However the output just looks like the attached picture.

    When I check in the sql db I can see the line breaks when doing a select * from Table ;

     

    image.png.6dc8f81905f12de97a48e47c6babb2bc.png

  6. SOLVED: I'm having trouble with an SQL  query via php in building my Viewthread.php file

    So on the main page you have a list of the Threads and the link I created is like the below

    <h2><a href='viewthread.php?id='{$row['id']}'>  {$row['Title']} </a></h2> Post {$row['id']}<br> 

    If I set the query to an ID that exists the page loads fine

    $query = mysqli_query($conn, "SELECT * FROM Threads where id='156'")

    however if I set the id to the GET command the page doesn't load (which should've got the id)

    $query = mysqli_query($conn, "SELECT * FROM Threads where id=$_GET['id']")

    I tried casting this to a variable first too

    $Number = "$_GET['id']"
    $query = mysqli_query($conn, "SELECT * FROM Threads where id='$Number'")

     

  7. 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

  8. OK I added some error checking

    $output=null;
    $retval=null;
     exec("convert $targetFilePathArg -resize 300x200 testresize.jpg",$output, $retval);
    echo "REturned with status $retval and output:\n" ; 
    if ($retval == null) { 
    echo "Retval is null\n" ; 

     

    which returns

    REturned with status 1 and output: The file test.jpg has been uploaded successfully.

    status 1 would be an error no?

     

    I also then went to create a php file with just this in

    <?php 
      exec("convert arrow.png -resize 300x200 ./testresize.jpg",$output, $retval);
    ?>
    

    Then run this at terminal and I get the following.

    convert-im6.q16: unable to open image `testresize.jpg': Permission denied @ error/blob.c/OpenBlob/2874.

    But I've already provided permissions to this folder via chmod -R 707 upload

    937203654_screenshotofpermissiosn.png.d35c2b1689bc0968ef0f86b6d4848cef.png

  9. Thanks I did try that and it reached the correct IF statement to produce "The file has been uploaded successfully"

     

    However it did not create the file.

    I even amended your statement to

    $targetFilePathArg = escapeshellarg($targetFilePath);
    exec("convert $targetFilePathArg -resize 300x200 .jpg");

    Just to check it was not failing at finding the folder for some reason.

    Bigger block of code below

    // File upload path
    $targetDir = "upload/";
    $fileName = basename($_FILES["file"]["name"]);
    $targetFilePath = $targetDir . $fileName;
    $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
    
    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);
    exec("convert $targetFilePathArg -resize 300x200 .jpg");
     }else{
                    $statusMsg = "File upload failed, please try again.";
                } 

     

  10. Hi

    I'm trying to resize pictures for thumbnails

    I can do this correctly at linux command line with the following command

    convert ideologies.jpeg -resize 300x200 ./Thumbnails/testphp.jpg

    However trying to wrap this in PHP doesn't seem to work

    // 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.";
    exec("convert {['fileName']} -resize 300x200 wankertestphp.jpg");
                }

    If you look above I do successfully hit the "The File has been uploaded successfully" line so it is entering that If statement.

    I've also checked the Threads table with a Select * from Threads and I can see the $fileName variable is entering there.  I've tried numerous different ways around the $fileName variable (removing the brackets...removing the brackets and the apostrophes")

     

    Any help is appreciated.

×
×
  • 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.