2013-02-05 2 views
0

코드에서 논리적 오류를 해결할 수 없습니다. 나는 논리가 올바른지 보이지만 어떤 문제가 있는지 확실하지 않다PHP 폼 코드에서 PHP 논리 오류가 발생했습니다.

이 내 PHP입니다 :

<?php require_once("includes/connection.php"); ?> 
<?php 
include_once("includes/form_functions.php"); 
if(isset($_POST['submit'])) 
{ 
    $errors = array(); 
    if(isset($_POST['txtSpace'])) 
    { 
     $choice_spc_port = $_POST["txtSpace"]; 
    } 
    if(isset($_POST['txtNumber'])) 
    { 
     $choice_no = $_POST["txtNumber"]; 
    } 
    if(isset($_POST['txtLocation'])) 
    { 
     $choice_loc = $_POST["txtLocation"]; 
     if($choice_loc =="txtSetXY") 
     { 
      $x = $_POST["txtXLocation"]; 
      $y = $_POST["txtYLocation"]; 
      if($x == "") 
      { 
       $message = "You forgot to enter X Value"; 
      } 
      elseif($y == "") 
      { 
       $message = "You forgot to enter Y Value"; 
      } 
      else 
      { 
       $choice_loc = $x . "," . $y; 
      } 
     } 
    } 
    $user_name = $_POST["txtUserName"]; 
    $user_email = $_POST["txtUserEMail"]; 
    $animal_name = $_POST["txtAnimalName"]; 
    $disp_msg = $_POST["txtDispMsg"]; 
    $comments = $_POST["txtComments"]; 
    if(!isset($_POST['txtSpace'])) 
    { 
     $message = "Please select Space Portion"; 
    } 
    elseif(!isset($_POST['txtNumber'])) 
    { 
     $message = "Please select the number of animals"; 
    } 
    elseif(!isset($_POST['txtLocation'])) 
    { 
     $message = "Please select the desired location of animal"; 
    } 
    elseif($user_name == "") 
    { 
     $message = "Please enter your name."; 
    } 
    elseif($user_email == "") 
    { 
     $message = "Please enter your email."; 
    } 
    elseif($animal_name == "") 
    { 
     $message = "Please enter the name of the animal."; 
    } 
    elseif($disp_msg == "") 
    { 
     $message = "What message you want to dedicate to the animal?."; 
    } 
    else 
    { 
     // validation 
     $required_fields = array('txtUserName','txtUserEMail','txtAnimalName','txtDispMsg'); 
     $errors = array_merge($errors, check_required_fields($required_fields, $_POST)); 
     $user_name = trim(mysql_prep($_POST['txtUserName'])); 
     $user_email = trim(mysql_prep($_POST['txtUserEMail'])); 
     $animal_name = trim(mysql_prep($_POST['txtAnimalName'])); 
     $disp_msg = trim(mysql_prep($_POST['txtDispMsg'])); 

     if(empty($errors)) 
     { 
      /*if($choice_loc == "txtSetXY") 
      { 
       $x = $_POST["txtXLocation"]; 
       $y = $_POST["txtYLocation"]; 
       $choice_loc = $x . "," . $y; 
      }*/ 
      if($choice_no == "other") 
      { 
       $choice_no = $_POST["other_field"]; 
      } 

      $insert = "INSERT INTO db_form (db_space_portion, db_number, db_location, db_user_name, db_user_email, db_animal_name, db_message, db_comments) VALUES ('{$choice_spc_port}', '{$choice_no}', '{$choice_loc}', '{$user_name}', '{$user_email}','{$animal_name}','{$disp_msg}','{$comments}')"; 
      $result = mysql_query($insert); 
      if($result) 
      { 
       echo("<br>Input data is succeed"); 
      } 
      else 
      { 
       $message = "The data cannot be inserted."; 
       $message .= "<br />" . mysql_error(); 
      } 
     } 
     else 
     { 
      if(count($errors) == 1) 
      { 
       $message = "There was 1 error on the form."; 
      } 
      else 
      { 
       $message = "There were " . count($errors) ." errors on the form."; 
      } 
     } 
    } 
} 
else 
{ 
    $user_name = ""; 
    $user_email = ""; 
    $disp_msg = ""; 
    $comments = ""; 
} 
    ?> 
    <!DOCTYPE html> 
    <html lang="en"> 
    <head> 
    <title>Test Form</title> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href="css/reset.css" type="text/css" media="all"> 
    <link rel="stylesheet" href="css/layout.css" type="text/css" media="all"> 
    <link rel="stylesheet" href="css/style.css" type="text/css" media="all"> 
    <script type="text/javascript" src="js/jquery-1.9.0.min.js" ></script> 
    <script type="text/javascript" src="js/cufon-yui.js"></script> 
    <script type="text/javascript" src="js/cufon-replace.js"></script> 
    <script type="text/javascript" src="js/Copse_400.font.js"></script> 
    <script type="text/javascript" src="js/imagepreloader.js"></script> 
    <script type="text/javascript" src="js/functions.js"></script> 
    <!--[if lt IE 9]> 
    <script type="text/javascript" src="js/ie6_script_other.js"></script> 
     <script type="text/javascript" src="js/html5.js"></script> 
    <![endif]--> 
     </head> 
     <body id="page5"> 
     <!-- START PAGE SOURCE --> 

     <div class="body7"> 
     <div class="main"> 
     <section id="content"> 
     <div class="wrapper"> 
     <article class="col24"> 
     <div class="pad1"> 
     <h4>Kindly Fill the form</h4> 
     <?php if(!empty($message)){ echo $message; } ?> 
     <?php if(!empty($errors)){ echo display_errors($errors);}?> 
     <form id="TestForm" name="TestForm" method="post" action="form.php"> 
      <div> 

      <div class="wrapper"> <strong><span>*</span> Desired Space</strong> 
      <div class="formText"> 
       <input type="radio" name="txtSpace" value="RJ"/>Space Top<br /> 
       <input type="radio" name="txtSpace" value="SM" />Space Bottom<br /> 
       </div> 
      </div> 

      <div class="wrapper"> <strong><span>*</span> Select the Number</strong> 
      <div class="formText"> 
       <input type="radio" name="txtNumber" value="100"/>100 
       <input type="radio" name="txtNumber" value="200"/>200 
       <input type="radio" name="txtNumber" value="500"/>500 
       <input type="radio" name="txtNumber" value="1000"/>1000 
       <input type="radio" name="txtNumber" value="10000"/>10000 
       <input type="radio" name="txtNumber" value="other"/>other 
       <input type="text" name="other_field" id="other_field" onblur="checktext(this);"/> 
      </div> 
      </div> 

      <div class="wrapper"> <strong><span>*</span> Select X & Y Value</strong> 
       <div class="formText"> 
       <input type="radio" name="txtLocation" value="txtSetXY"/> Specify Photo Location<br /> 
        <div style="padding-left:20px;"> 
        X: <input type="text" id="locField" name="txtXLocation"><br /> 
        Y: <input type="text" id="locField" name="txtYLocation"><br /> 
        </div> 
        <input type="radio" name="txtLocation" value="Default"/>Default 
       </div> 

      </div> 


      <div class="wrapper"> <strong><span>*</span> Your Name:</strong> 
       <div class="bg"> 
       <input type="text" class="input" name="txtUserName"> 
       </div> 
      </div> 
      <div class="wrapper"> <strong><span>*</span> Your Email:</strong> 
       <div class="bg"> 
       <input type="text" class="input" name="txtUserEMail"> 
       </div> 
      </div> 
      <div class="wrapper"> <strong><span>*</span> Name of the animal:</strong> 
       <div class="bg"> 
       <input type="text" class="input" name="txtAnimalName"> 
       </div> 
      </div> 

      <div class="wrapper"> 
      <div class="textarea_box"> <strong><span>*</span> The Message you want for your favourite animal:</strong> 
       <textarea name="txtDispMsg" cols="1" rows="1"></textarea> 
      </div> 
      </div> 

      <div class="wrapper"> 
      <div class="textarea_box"> <strong>Comments:</strong> 
       <textarea name="txtComments" cols="1" rows="1"></textarea> 
      </div> 
      </div> 

      <input type="submit" name="submit" value="Submit"> 
      </div> 
     </form> 
     </div> 
    </article> 
    </div> 
</section> 
</div> 
</div> 
</body> 
</html> 

오류 :이 PHP는 바이올린 여기

확인합니다.

행 25. x 텍스트 필드를 공백으로 남겨도 표시되지 않습니다. $ message = "X 값 입력을 잊었습니다.";

줄 29와 동일합니다. y 텍스트 필드를 비워 두어도이 표시되지 않습니다. $ message = "Y 값을 입력하지 않았습니다.";

그러나 x 및 y 텍스트 필드의 값 즉, txtXLocation 및 txtYLocation에 값을 입력하면 유효성 검사를 확인하지 않고 db에 저장됩니다. 사전에

덕분에

+0

'if (empty ($ x)) '를 사용해보세요 – Kamil

+1

스택 오버플로에 오신 것을 환영합니다. 질문 자체에 코드를 게시 할 수 있으며 실제로 그렇게하는 것이 좋습니다. ** {} ** 도구 모음 버튼을 사용하여 간단하게 형식을 지정하십시오. –

+0

@ 카밀 나는 같은 것을 사용했다. 그러나 해결책을 제공하지 않고있다. – Sumit

답변

0

는 폴더를 포함하고 해당 파일에 도달하기 위해 올바른 경로를 준에 connection.php 파일이 있는지 확인하십시오.

+0

이것은 오류가 아닙니다. 모든 경로가 올바른 것입니다 .. ine 25. x 텍스트 필드를 비워 두어도 표시되지 않습니다. $ message = "X 값 입력을 잊어 버렸습니다"; 줄 29와 동일합니다. y 텍스트 필드를 비워 두어도 표시되지 않습니다. $ message = "Y 값을 입력하지 않았습니다."; 그러나 x 및 y 텍스트 필드에 값 (즉, txtXLocation 및 txtYLocation)을 입력하면 db에 저장되므로 유효성 검사를 확인하지 않고있는 것입니다. – Sumit

+0

$ errors 배열에 push 된 $ message를 볼 수 없습니다. 그리고 오류가있을 때 $ message를 덮어 쓰면 문제가 생길 수 있습니다. $ errors 배열에 $ message 넣기 –

관련 문제