MattDAVM
MattDAVM

Reputation: 515

Prevent insert query with php

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";
    }

Upvotes: 1

Views: 238

Answers (1)

deChristo
deChristo

Reputation: 1878

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;
  }

Upvotes: 1

Related Questions