SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If Conditional on PDO query

    I have a section where I want a user to be able to post a comment, or post a comment with an image. I have gone from one query to two trying to get the conditional statement to work.

    At this point, the first query is ignored whether I have an image file to upload or not.

    Can anyone see where I am going wrong?

    Thank You

    Gary


    PHP Code:
    $image $_POST['image'];

    if(isset(
    $_POST['image'])){                         // have had if(isset($_POST['com'])) did not work either

    $comments trim($_POST['comments']);

    $image_file $_FILES['image']['name'];
    $image_type $_FILES['image']['type'];
    $image_size $_FILES['image']['size']; 

        if(!empty(
    $image_file)){
          if ((
    $image_type == 'image/gif') || ($image_type == 'image/jpeg') || ($image_type == 'image/pjpeg') || ($image_type == 'image/png') && ($image_size 3000000))  {
            if (
    $_FILES['image']['error'] == 0) {
              
    // Move the file to the target upload folder
              
    $target 'imagesmemorial''/' $image_file;
               if(
    move_uploaded_file($_FILES['image']['tmp_name'], $target)){
              
    $sqlic "INSERT INTO uimages (userid, mem_id, image, comments) VALUES (:userid, :mem_id, :image, :comments)"
    $qic $conn->prepare($sqlic);
    $qic->execute(array(':userid'=> $userid_m':mem_id'=>$mem_id":image"=>$image_file':comments'=>$comments));
                }
            }
        }
    }

    }else{

    if(isset(
    $_POST['com']) && !isset($_POST['images'])){ // have had set as empty()
    $comments trim($_POST['comments']);

    $sqlicn "INSERT INTO uimages (userid, mem_id,comments) VALUES (:userid, :mem_id,  :comments)"
    $qicn $conn->prepare($sqlicn);
    $qicn->execute(array(':userid'=> $userid_m':mem_id'=>$mem_id,  ':comments'=>$comments));
    }


  2. #2
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gwpaul View Post
    I have a section where I want a user to be able to post a comment, or post a comment with an image. I have gone from one query to two trying to get the conditional statement to work.

    At this point, the first query is ignored whether I have an image file to upload or not.

    Can anyone see where I am going wrong?

    Thank You

    Gary


    PHP Code:
    $image $_POST['image'];

    if(isset(
    $_POST['image'])){                         // have had if(isset($_POST['com'])) did not work either

    $comments trim($_POST['comments']);

    $image_file $_FILES['image']['name'];
    $image_type $_FILES['image']['type'];
    $image_size $_FILES['image']['size']; 

        if(!empty(
    $image_file)){
          if ((
    $image_type == 'image/gif') || ($image_type == 'image/jpeg') || ($image_type == 'image/pjpeg') || ($image_type == 'image/png') && ($image_size 3000000))  {
            if (
    $_FILES['image']['error'] == 0) {
              
    // Move the file to the target upload folder
              
    $target 'imagesmemorial''/' $image_file;
               if(
    move_uploaded_file($_FILES['image']['tmp_name'], $target)){
              
    $sqlic "INSERT INTO uimages (userid, mem_id, image, comments) VALUES (:userid, :mem_id, :image, :comments)"
    $qic $conn->prepare($sqlic);
    $qic->execute(array(':userid'=> $userid_m':mem_id'=>$mem_id":image"=>$image_file':comments'=>$comments));
                }
            }
        }
    }

    }else{

    if(isset(
    $_POST['com']) && !isset($_POST['images'])){ // have had set as empty()
    $comments trim($_POST['comments']);

    $sqlicn "INSERT INTO uimages (userid, mem_id,comments) VALUES (:userid, :mem_id,  :comments)"
    $qicn $conn->prepare($sqlicn);
    $qicn->execute(array(':userid'=> $userid_m':mem_id'=>$mem_id,  ':comments'=>$comments));
    }

    Looks like this place has died, time to move to stackoverflow.

  3. #3
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you tried checking what the actual data being posted looks like? There really isn't enough code provided to tell if the conditions are being provided.

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by robawtic View Post
    have you tried checking what the actual data being posted looks like? There really isn't enough code provided to tell if the conditions are being provided.

    Rob

    Thank you for your reply. The script I posted is the entirety of the code, I don't know what else I could have posted (both statements work on their own, the connection works fine, the rest of the page also works fine).

    It would seem the only part that is failing is the conditional, and I have tried empty(). !empty() isset(), !isset(). !=='', ==''.

    I had it all contained within one query, then separated the queries in case the empty INSERT was the issue (which should not have been the case). I set conditionals for if the $image was empty to call it NULL.

    Thank you again for your reply, I will post the answer once I find it. I do have it working now but am getting double entries on each post.

    Thanks

    Gary

  5. #5
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you tried doing a var_dump($_POST); before the conditional statement?
    Try filling out the form and adding the var_dump($_POST);
    Then post the result. It will help determine how the structure of your form data is actually laid out. The provided code looks fine to me as far as logically processing so it makes me think that you maybe have a typo in one of the inputs you are trying to parse.

    So the script is running to execute the query it's just giving you the desired results?

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The var_dump was not productive, it would show NULL even with the image being processed as desired.

    I finally solved the issue by setting the conditional to ask if the first query was true. The code is below incase you were interested.

    Thank you for taking the time to reply.

    Gary
    PHP Code:
    if(isset($_POST['com'])){
    $comments trim($_POST['comments']);
    $image $_POST['image'];
    }
    if(
    $_POST['image']!==''){
    $image_file $_FILES['image']['name'];
    $image_type $_FILES['image']['type'];
    $image_size $_FILES['image']['size']; 
    }
        if(!empty(
    $image_file)){
          if ((
    $image_type == 'image/gif') || ($image_type == 'image/jpeg') || ($image_type == 'image/pjpeg') || ($image_type == 'image/png') && ($image_size 3000000))  {
            if (
    $_FILES['image']['error'] == 0) {
              
    // Move the file to the target upload folder
              
    $target 'imagesmemorial''/' $image_file;
               if(
    move_uploaded_file($_FILES['image']['tmp_name'], $target)){

    $sqlic "INSERT INTO uimages (userid, mem_id, image, comments) VALUES (:userid, :mem_id, :image, :comments)"
    $qic $conn->prepare($sqlic);
    $qic->execute(array(':userid'=> $userid_m':mem_id'=>$mem_id":image"=>$image_file':comments'=>$comments));
                    }
                }
            }
        }


    if (
    $sqlic ==true){
    //
    }else{
    $comments trim($_POST['comments']);
    $sqlicn "INSERT INTO uimages (userid, mem_id, comments) VALUES (:userid, :mem_id,   :comments)"
    $qicn $conn->prepare($sqlicn);
    $qicn->execute(array(':userid'=> $userid_m':mem_id'=>$mem_id,  ':comments'=>$comments));


  7. #7
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, if the var_dump($_POST) is returning null, it makes me think that the actual form is sending a $_GET instead of post. glad to hear that you got it working though.

  8. #8
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could try var_dump($_REQUEST) to see if you are getting anything at all.
    What I lack in acuracy I make up for in misteaks


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •