We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40122
    • 330 Posts
    I am trying to insert some data into a table, but want to check for duplicate entries first.

    I thought I could do this using:
    if (!is_object($checksaved)) {
    but I cannot get it to work.

    Does anyone know what I am doing wrong?

    <?php
    $user = $modx->getUser();
    $user = $user->get('id');
    $job_id = trim($_REQUEST['job_id']);
    
    // CHECK FOR DUPLICATE
    $checksaved = "SELECT * FROM `saved_jobs` WHERE `job_id` = $job_id AND `user_id` = $user";
    //echo $checksaved;
    $checksaved = $modx->query($checksaved);
    
    if (!is_object($checksaved)) {
       $savejob = "INSERT INTO saved_jobs (user_id,job_id) VALUES ($user,$job_id);";
    //echo $savejob;
    $savejob = $modx->query($savejob);
    echo "<div id='okmessage'><h3>Job saved successfully</h3></div>";
    }
    else {
       echo "<div id='alertmessage'><h3>You have already saved this job.</h3></div>";
    }
    


    Many thanks!
      • 49529
      • 196 Posts
      Hmm. At first glance your code seems to be correct. According to docs (http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query), xPDO query should return object or false, so i think is_object is a correct method. You can try this variant:

      if (!is_object($checksaved) || $checksaved == false) {
           // blah
      }
      


      What gives you var_dump($checksaved) BTW?