PHP MYSQL runs the query twice












0















When i run the following script. The row is inserted twice ( the query runs twice ) .



require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

$response = textsanitize($_POST['r']);
$ticket = idssanitize($_POST['t']);

$stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
$stmt->execute(
array(
"ticket_id" => $ticket,
"user_id" => $_SESSION['user_id'],
"time" => time(),
"response" => $response
)
);
if($stmt->execute()){
echo "SUCCESS";
}


When i remove if($stmt->execute()){echo "SUCCESSS";}. It runs in the right way. The row inserted once.



Why does if($stmt->execute()) execute the query again ? I thought that if($stmt->execute()) only returns TRUE || FALSE. I want to ensure that the query was executed successfully.










share|improve this question


















  • 3





    “Why does if($stmt->execute()) execute the query again ?” - because that’s plain and simple what this method does, it executes the statement. You called this method twice, so it performs its job twice. Surprise. Catch the result of the first call in a variable, and then use that variable in your if condition.

    – misorude
    Nov 23 '18 at 9:27








  • 1





    You need to put your if() around the first (and only) $stmt->execute() with all of the parameters you want to use.

    – Nigel Ren
    Nov 23 '18 at 9:28
















0















When i run the following script. The row is inserted twice ( the query runs twice ) .



require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

$response = textsanitize($_POST['r']);
$ticket = idssanitize($_POST['t']);

$stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
$stmt->execute(
array(
"ticket_id" => $ticket,
"user_id" => $_SESSION['user_id'],
"time" => time(),
"response" => $response
)
);
if($stmt->execute()){
echo "SUCCESS";
}


When i remove if($stmt->execute()){echo "SUCCESSS";}. It runs in the right way. The row inserted once.



Why does if($stmt->execute()) execute the query again ? I thought that if($stmt->execute()) only returns TRUE || FALSE. I want to ensure that the query was executed successfully.










share|improve this question


















  • 3





    “Why does if($stmt->execute()) execute the query again ?” - because that’s plain and simple what this method does, it executes the statement. You called this method twice, so it performs its job twice. Surprise. Catch the result of the first call in a variable, and then use that variable in your if condition.

    – misorude
    Nov 23 '18 at 9:27








  • 1





    You need to put your if() around the first (and only) $stmt->execute() with all of the parameters you want to use.

    – Nigel Ren
    Nov 23 '18 at 9:28














0












0








0








When i run the following script. The row is inserted twice ( the query runs twice ) .



require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

$response = textsanitize($_POST['r']);
$ticket = idssanitize($_POST['t']);

$stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
$stmt->execute(
array(
"ticket_id" => $ticket,
"user_id" => $_SESSION['user_id'],
"time" => time(),
"response" => $response
)
);
if($stmt->execute()){
echo "SUCCESS";
}


When i remove if($stmt->execute()){echo "SUCCESSS";}. It runs in the right way. The row inserted once.



Why does if($stmt->execute()) execute the query again ? I thought that if($stmt->execute()) only returns TRUE || FALSE. I want to ensure that the query was executed successfully.










share|improve this question














When i run the following script. The row is inserted twice ( the query runs twice ) .



require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

$response = textsanitize($_POST['r']);
$ticket = idssanitize($_POST['t']);

$stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
$stmt->execute(
array(
"ticket_id" => $ticket,
"user_id" => $_SESSION['user_id'],
"time" => time(),
"response" => $response
)
);
if($stmt->execute()){
echo "SUCCESS";
}


When i remove if($stmt->execute()){echo "SUCCESSS";}. It runs in the right way. The row inserted once.



Why does if($stmt->execute()) execute the query again ? I thought that if($stmt->execute()) only returns TRUE || FALSE. I want to ensure that the query was executed successfully.







php mysql if-statement pdo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 9:25









JORDAN MIJORDAN MI

486




486








  • 3





    “Why does if($stmt->execute()) execute the query again ?” - because that’s plain and simple what this method does, it executes the statement. You called this method twice, so it performs its job twice. Surprise. Catch the result of the first call in a variable, and then use that variable in your if condition.

    – misorude
    Nov 23 '18 at 9:27








  • 1





    You need to put your if() around the first (and only) $stmt->execute() with all of the parameters you want to use.

    – Nigel Ren
    Nov 23 '18 at 9:28














  • 3





    “Why does if($stmt->execute()) execute the query again ?” - because that’s plain and simple what this method does, it executes the statement. You called this method twice, so it performs its job twice. Surprise. Catch the result of the first call in a variable, and then use that variable in your if condition.

    – misorude
    Nov 23 '18 at 9:27








  • 1





    You need to put your if() around the first (and only) $stmt->execute() with all of the parameters you want to use.

    – Nigel Ren
    Nov 23 '18 at 9:28








3




3





“Why does if($stmt->execute()) execute the query again ?” - because that’s plain and simple what this method does, it executes the statement. You called this method twice, so it performs its job twice. Surprise. Catch the result of the first call in a variable, and then use that variable in your if condition.

– misorude
Nov 23 '18 at 9:27







“Why does if($stmt->execute()) execute the query again ?” - because that’s plain and simple what this method does, it executes the statement. You called this method twice, so it performs its job twice. Surprise. Catch the result of the first call in a variable, and then use that variable in your if condition.

– misorude
Nov 23 '18 at 9:27






1




1





You need to put your if() around the first (and only) $stmt->execute() with all of the parameters you want to use.

– Nigel Ren
Nov 23 '18 at 9:28





You need to put your if() around the first (and only) $stmt->execute() with all of the parameters you want to use.

– Nigel Ren
Nov 23 '18 at 9:28












3 Answers
3






active

oldest

votes


















1














One of the good uses of prepared statements in any language is that you can prepare it once and then execute it as many times as needed.



So in your case you execute() the statement twice, but it's possible that you could insert a whole bunch of data with the same prepared statement in a loop. Each time you call execute() you can just pass a new set of values to run the prepared statement. In your case it is an INSERT, so this is run twice.



In your case you probably just need...



$stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");

if($stmt->execute(array(
"ticket_id" => $ticket,
"user_id" => $_SESSION['user_id'],
"time" => time(),
"response" => $response))) {
echo "SUCCESS";
}





share|improve this answer































    1














    It is because it is calling the $stmt->execute() function twice. Once before the if statement and once as the condition in the if statement.
    So, you need to remove one instance of it.



    I believe that you need to check if the statement has executed correctly (hence the if). So, the code can be like...



    require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
    require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

    $response = textsanitize($_POST['r']);
    $ticket = idssanitize($_POST['t']);

    $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
    $values = array(
    "ticket_id" => $ticket,
    "user_id" => $_SESSION['user_id'],
    "time" => time(),
    "response" => $response
    );
    if($stmt->execute($values)){
    echo "SUCCESS";
    }





    share|improve this answer































      1














      You are executing $stmt->execute() twice, so it's simply inserting two rows. no rocket science here.



      if you want to check if the query ran successfully or not do it in the first statement itself.



      require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
      require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

      $response = textsanitize($_POST['r']);
      $ticket = idssanitize($_POST['t']);

      $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
      $isSuccessful = $stmt->execute(
      array(
      "ticket_id" => $ticket,
      "user_id" => $_SESSION['user_id'],
      "time" => time(),
      "response" => $response
      )
      );
      if($isSuccessful){
      echo "SUCCESS";
      }





      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443824%2fphp-mysql-runs-the-query-twice%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        One of the good uses of prepared statements in any language is that you can prepare it once and then execute it as many times as needed.



        So in your case you execute() the statement twice, but it's possible that you could insert a whole bunch of data with the same prepared statement in a loop. Each time you call execute() you can just pass a new set of values to run the prepared statement. In your case it is an INSERT, so this is run twice.



        In your case you probably just need...



        $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");

        if($stmt->execute(array(
        "ticket_id" => $ticket,
        "user_id" => $_SESSION['user_id'],
        "time" => time(),
        "response" => $response))) {
        echo "SUCCESS";
        }





        share|improve this answer




























          1














          One of the good uses of prepared statements in any language is that you can prepare it once and then execute it as many times as needed.



          So in your case you execute() the statement twice, but it's possible that you could insert a whole bunch of data with the same prepared statement in a loop. Each time you call execute() you can just pass a new set of values to run the prepared statement. In your case it is an INSERT, so this is run twice.



          In your case you probably just need...



          $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");

          if($stmt->execute(array(
          "ticket_id" => $ticket,
          "user_id" => $_SESSION['user_id'],
          "time" => time(),
          "response" => $response))) {
          echo "SUCCESS";
          }





          share|improve this answer


























            1












            1








            1







            One of the good uses of prepared statements in any language is that you can prepare it once and then execute it as many times as needed.



            So in your case you execute() the statement twice, but it's possible that you could insert a whole bunch of data with the same prepared statement in a loop. Each time you call execute() you can just pass a new set of values to run the prepared statement. In your case it is an INSERT, so this is run twice.



            In your case you probably just need...



            $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");

            if($stmt->execute(array(
            "ticket_id" => $ticket,
            "user_id" => $_SESSION['user_id'],
            "time" => time(),
            "response" => $response))) {
            echo "SUCCESS";
            }





            share|improve this answer













            One of the good uses of prepared statements in any language is that you can prepare it once and then execute it as many times as needed.



            So in your case you execute() the statement twice, but it's possible that you could insert a whole bunch of data with the same prepared statement in a loop. Each time you call execute() you can just pass a new set of values to run the prepared statement. In your case it is an INSERT, so this is run twice.



            In your case you probably just need...



            $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");

            if($stmt->execute(array(
            "ticket_id" => $ticket,
            "user_id" => $_SESSION['user_id'],
            "time" => time(),
            "response" => $response))) {
            echo "SUCCESS";
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 9:32









            Nigel RenNigel Ren

            28.3k62034




            28.3k62034

























                1














                It is because it is calling the $stmt->execute() function twice. Once before the if statement and once as the condition in the if statement.
                So, you need to remove one instance of it.



                I believe that you need to check if the statement has executed correctly (hence the if). So, the code can be like...



                require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
                require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

                $response = textsanitize($_POST['r']);
                $ticket = idssanitize($_POST['t']);

                $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
                $values = array(
                "ticket_id" => $ticket,
                "user_id" => $_SESSION['user_id'],
                "time" => time(),
                "response" => $response
                );
                if($stmt->execute($values)){
                echo "SUCCESS";
                }





                share|improve this answer




























                  1














                  It is because it is calling the $stmt->execute() function twice. Once before the if statement and once as the condition in the if statement.
                  So, you need to remove one instance of it.



                  I believe that you need to check if the statement has executed correctly (hence the if). So, the code can be like...



                  require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
                  require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

                  $response = textsanitize($_POST['r']);
                  $ticket = idssanitize($_POST['t']);

                  $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
                  $values = array(
                  "ticket_id" => $ticket,
                  "user_id" => $_SESSION['user_id'],
                  "time" => time(),
                  "response" => $response
                  );
                  if($stmt->execute($values)){
                  echo "SUCCESS";
                  }





                  share|improve this answer


























                    1












                    1








                    1







                    It is because it is calling the $stmt->execute() function twice. Once before the if statement and once as the condition in the if statement.
                    So, you need to remove one instance of it.



                    I believe that you need to check if the statement has executed correctly (hence the if). So, the code can be like...



                    require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
                    require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

                    $response = textsanitize($_POST['r']);
                    $ticket = idssanitize($_POST['t']);

                    $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
                    $values = array(
                    "ticket_id" => $ticket,
                    "user_id" => $_SESSION['user_id'],
                    "time" => time(),
                    "response" => $response
                    );
                    if($stmt->execute($values)){
                    echo "SUCCESS";
                    }





                    share|improve this answer













                    It is because it is calling the $stmt->execute() function twice. Once before the if statement and once as the condition in the if statement.
                    So, you need to remove one instance of it.



                    I believe that you need to check if the statement has executed correctly (hence the if). So, the code can be like...



                    require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
                    require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

                    $response = textsanitize($_POST['r']);
                    $ticket = idssanitize($_POST['t']);

                    $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
                    $values = array(
                    "ticket_id" => $ticket,
                    "user_id" => $_SESSION['user_id'],
                    "time" => time(),
                    "response" => $response
                    );
                    if($stmt->execute($values)){
                    echo "SUCCESS";
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 9:32









                    illusionillusion

                    13618




                    13618























                        1














                        You are executing $stmt->execute() twice, so it's simply inserting two rows. no rocket science here.



                        if you want to check if the query ran successfully or not do it in the first statement itself.



                        require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
                        require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

                        $response = textsanitize($_POST['r']);
                        $ticket = idssanitize($_POST['t']);

                        $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
                        $isSuccessful = $stmt->execute(
                        array(
                        "ticket_id" => $ticket,
                        "user_id" => $_SESSION['user_id'],
                        "time" => time(),
                        "response" => $response
                        )
                        );
                        if($isSuccessful){
                        echo "SUCCESS";
                        }





                        share|improve this answer




























                          1














                          You are executing $stmt->execute() twice, so it's simply inserting two rows. no rocket science here.



                          if you want to check if the query ran successfully or not do it in the first statement itself.



                          require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
                          require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

                          $response = textsanitize($_POST['r']);
                          $ticket = idssanitize($_POST['t']);

                          $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
                          $isSuccessful = $stmt->execute(
                          array(
                          "ticket_id" => $ticket,
                          "user_id" => $_SESSION['user_id'],
                          "time" => time(),
                          "response" => $response
                          )
                          );
                          if($isSuccessful){
                          echo "SUCCESS";
                          }





                          share|improve this answer


























                            1












                            1








                            1







                            You are executing $stmt->execute() twice, so it's simply inserting two rows. no rocket science here.



                            if you want to check if the query ran successfully or not do it in the first statement itself.



                            require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
                            require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

                            $response = textsanitize($_POST['r']);
                            $ticket = idssanitize($_POST['t']);

                            $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
                            $isSuccessful = $stmt->execute(
                            array(
                            "ticket_id" => $ticket,
                            "user_id" => $_SESSION['user_id'],
                            "time" => time(),
                            "response" => $response
                            )
                            );
                            if($isSuccessful){
                            echo "SUCCESS";
                            }





                            share|improve this answer













                            You are executing $stmt->execute() twice, so it's simply inserting two rows. no rocket science here.



                            if you want to check if the query ran successfully or not do it in the first statement itself.



                            require_once $_SERVER['DOCUMENT_ROOT'].'/functions/sanitize.php';
                            require_once $_SERVER['DOCUMENT_ROOT'].'/main/config.php';

                            $response = textsanitize($_POST['r']);
                            $ticket = idssanitize($_POST['t']);

                            $stmt = $condb->prepare("INSERT INTO ticket_reponses (ticket_id,user_id,time,response) VALUES (:ticket_id,:user_id,:time,:response)");
                            $isSuccessful = $stmt->execute(
                            array(
                            "ticket_id" => $ticket,
                            "user_id" => $_SESSION['user_id'],
                            "time" => time(),
                            "response" => $response
                            )
                            );
                            if($isSuccessful){
                            echo "SUCCESS";
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 23 '18 at 9:34









                            OneJeetOneJeet

                            962311




                            962311






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443824%2fphp-mysql-runs-the-query-twice%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()