1

I have a php which will include my datas inside my database.

But in my page I have a div which can be replicated, so I send this informations into an array (imploded with a "#!@" to avoid any kind of wrong explode when I insert it on my database).

My problem is that if the user doesn't insert anything on the first div content fields I shall not do the insert, and it still does.

    if ($_GET['action_ent'] != "#!@#!@#!@")
    {
    $myInputs = $_GET['action_ent'];
     foreach ($myInputs as $eachInput) 
     {

            $valores = $eachInput;
            print_r($valores);
            $dummy = explode('#!@', $valores);

            $acao = $dummy[0];
            $resp_acao = $dummy[1];
            $inic_plan_acao = $dummy[2];
            $fim_plan_acao = $dummy[3];


            $inicio_acc = explode("/", $inic_plan_acao);
            $fim_acc = explode("/", $fim_plan_acao);

            $inicio_action = $inicio_acc[2]."-".$inicio_acc[1]."-".$inicio_acc[0];
            $fim_action = $fim_acc[2]."-".$fim_acc[1]."-".$fim_acc[0];




                $result2 = mysql_query("INSERT INTO `demv3`.`entraves_action` (`action_id`, `ent_id`, `resp_ent`, `data_fim`,`action_desc`,`action_resp`,`action_comeco`,`action_fim`) VALUES ('0', '$ent_id', '$resp_ent', '$data_fim', '$acao', '$resp_acao', '$inicio_action', '$fim_action')");

     }

    }
    else
    {
        echo "NOTHING";
    }
2
  • If the input is coming from a form the place required onto the first input if you need them to input something, otherwise just check the $_GET for the first input and see if it is empty. Commented Nov 11, 2016 at 10:17
  • Your code depends entirely on what $_GET['action_ent'] has. And that's something you don't have full control on. To begin with, what does var_dump($_GET) show? Commented Nov 11, 2016 at 13:08

1 Answer 1

1

Try checking the first item in the foreach:

if ($_GET['action_ent'] != "#!@#!@#!@")
        {
        $myInputs = $_GET['action_ent'];
         foreach ($myInputs as $eachInput) 
         {
                if(empty($eachInput)) {
                      echo 'NOTHING';
                      break;
                }

                $valores = $eachInput;
                print_r($valores);
                $dummy = explode('#!@', $valores);

                $acao = $dummy[0];
                $resp_acao = $dummy[1];
                $inic_plan_acao = $dummy[2];
                $fim_plan_acao = $dummy[3];


                $inicio_acc = explode("/", $inic_plan_acao);
                $fim_acc = explode("/", $fim_plan_acao);

                $inicio_action = $inicio_acc[2]."-".$inicio_acc[1]."-".$inicio_acc[0];
                $fim_action = $fim_acc[2]."-".$fim_acc[1]."-".$fim_acc[0];




                    $result2 = mysql_query("INSERT INTO `demv3`.`entraves_action` (`action_id`, `ent_id`, `resp_ent`, `data_fim`,`action_desc`,`action_resp`,`action_comeco`,`action_fim`) VALUES ('0', '$ent_id', '$resp_ent', '$data_fim', '$acao', '$resp_acao', '$inicio_action', '$fim_action')");

         }

        }
        else
        {
            echo "NOTHING";
        }

Just be aware that if any other input besides the first one is empty it will break the loop. In order to avoid major changes in your logic you can resolve this with a counter or a boolean flag:

  if(empty($eachInput) && $counter == 0) {
     echo 'NOTHING';
     break;
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.