2014-04-14 5 views
0

사용자가 입력 한 데이터를 mysql 데이터베이스에 유효성을 확인하고 싶습니다. 그래서 사용자는 숫자가 필요한 필드에 문자를 입력 할 수 없습니다. 유효성을 확인한 후 데이터를 데이터베이스에 입력해야합니다. 내가 그 코드를 작성했지만, 그것은 작동하지 않습니다 .. 그것은 번호 필드PHP에서 사용자가 입력 한 데이터 유효성 확인

<?php 
include 'testinput.php'; 
error_reporting(E_ERROR | E_PARSE); 

if(isset($_POST['submitted'])){ 
    $con = mysqli_connect("localhost","USERNAME","PASSWORD","DATABASE"); 
    if(mysqli_connect_errno()){ 
     echo "Failed to connect"; 
     } 

     $card1 = test_input($_POST['card']); 
     $first1= $_POST['first']; 
     $last1= $_POST['last']; 
     $id1 = test_input($_POST['id']); 
     $mob1 = test_input($_POST['mobile']); 
     $vis1 = test_input($_POST['visit']); 

     $query = "INSERT INTO aftness1_clients.clients (card, first, last, id, mobile, visit) VALUES ('$card1','$first1','$last1','$id1', '$mob1','$vis1')"; 

     if(!mysqli_query($con, $query)){ 
      echo "Error ". mysqli_error($con); 
      echo "<br>"; 
     } 
     $newrecord ="<b>One client added Successfully !</b>"; 

}// end of main if 


?> 


<html> 
    <header> 
     <title> 
      Add a Client 
     </title> 
    </header> 
<body bgcolor="#F0FFFF"> 
<br/> <br/> <center><font size="5" color="#4EE2EC"> Clients Information </font> </center> <br/> 
<b>All fields are required:</b> <br/> <br/> 

    <form action = "insert.php" method="post"> 
     <input type="hidden" name="submitted" value="true"/> 
     <fieldset> 

      <legend>New Client</legend> 
      <lable><font size="3" color="#38ACEC"><b>Card Number:</b></font></lable> 
      <input type="text" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px;" size="10" maxlength="30" name="card"/><br/> 
      <lable><font size="3" color="#38ACEC"><b>First Name:</b></font></lable>  
      <input type="text" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px;" size="10" maxlength="30" name="first"/><br/> 
      <lable><font size="3" color="#38ACEC"><b>Last Name:</b></font></lable>  
      <input type="text" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px;" size="10" maxlength="30" name="last"/><br/> 
      <lable><font size="3" color="#38ACEC"><b>ID Number:</b></font></lable>  
      <input type="text" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px;" size="10" maxlength="30" name="id"/><br/> 
      <lable><font size="3" color="#38ACEC"><b>Mobile Number:</b></font></lable> 
      <input type="text" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px;" size="10" maxlength="30" name="mobile"/><br/> 
      <lable><font size="3" color="#38ACEC"><b>Visits:</b></font></lable> 
      <input type="text" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px;" size="10" maxlength="30" name="visit"/><br/> 
     </fieldset> 
     </br> 
      <font color="#FFFFFF"> <input type ="Submit" name="submit" value = "Add" align="right"/></font> 
    </form> 
<?php 
error_reporting(E_ERROR | E_PARSE); 
echo $newrecord 
?> 
</body> 
</html> 

<br/><br/><br/> 
<a href="index.php">Main Page</a> 

을 위해 편지를 받아이 내 코드에 문제가있는 testinput 기능

<?php 
function test_input($data) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 

이며,이다 거기에 데이터의 유효성을 확인하는 다른 방법은?

+1

어디에서 유효성을 검사합니까? 숫자 값 유효성 검사를 위해 regex 또는 is_int()를 사용해야합니다. –

+0

랜덤 입력에'stripslashes()'를 사용하지 마십시오. 그냥 부패 할거야! –

답변

0

유효성 확인을 위해 이것을 확인하십시오. ctype_alpha는 [A-Za-z] 또는 preg_match ("/^[a-zA-Z \ s] + $ /", $ data) 문자 만 검사합니다.

0
당신이 정규식 사용 is_int() 또는 is_numeric()

function test_input($data) 
{ 
    $data = trim($data); 
    if(is_int($data)) { 
    return $data; 
    } 
    else { 
    //do your stuff may be redirect to again input page and display error msg. 
    } 
} 

같은 정규식 또는 PHP 숫자 체크 기능을 사용하여 입력 데이터의 유효성을 검사 할 필요가

: -이 내가 검증을 위해 쓴 PHP 클래스가

if (preg_match('/^[0-9]+$/', $str)) { 
    // contains only 0-9 
} else { 
    // contains other stuff 
} 
0

입니다 이렇게. 부담없이 사용하십시오!

<?php 
class filterInput { 

    public function __construct() { 


    } 

    public function filterAllButNumbers($string) { 

     return $this->returnedValue($string, preg_replace("[^0-9]", "", $string)); 

    } 

    public function filterAllButLetters($string) { 

     return $this->returnedValue($string, preg_replace("[^A-Za-z]", "", $string)); 

    } 

    public function filterAllButNumbersAndSpaces($string) { 

     return $this->returnedValue($string, preg_replace("[^0-9 ]", "", $string)); 

    } 

    public function filterAllButLettersAndSpaces($string) { 

     return $this->returnedValue($string, preg_replace("[^A-Za-z ]", "", $string)); 

    } 

    public function filterAllButNumbersAndLetters($string) { 

     return $this->returnedValue($string, preg_replace("[^A-Za-z0-9]", "", $string)); 

    } 

    public function filterAllButNumbersLettersAndSpaces($string) { 

     return $this->returnedValue($string, preg_replace("[^A-Z a-z0-9]", "", $string)); 

    } 

    public function filterHex($string) { 

     return $this->returnedValue($string, preg_replace("[^A-Fa-f0-9]", "", $string)); 

    } 

    public function filterSlashes($string) { 

     return $this->returnedValue($string, stripSlashes($string)); 

    } 

    public function filterTags($string) { 

     return $this->returnedValue($string, strip_tags($string)); 

    } 

    public function filterEmail($email) { 
     if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      return true; 
     } else { 
      return false; 
     } 

    } 

    public function filterLength($string, $minimum, $maximum) { 

     if(strlen($string) >= $minimum && strlen($string) <= $maximum) { 
      return true; 
     } else { 
      return false; 
     } 

    } 

    public function boolify($filteredString) { 
     if(strcmp($filteredString, "true") == 0) { 
      return true; 
     } else { 
      return false; 
     } 

    } 

    private function returnedValue($string, $filteredString) { 
     if (strcmp($string, $filteredString) != 0) { 
      return $filteredString; 
     } else { 
      return "true"; 
     } 

    } 


} 





?> 

문자열이 조건과 일치하면 문자열 형식으로 true를 반환합니다.

0

코드에서 일부 자바 스크립트 유효성 검사

여기 간단한 예제가 있습니다.

<!DOCTYPE html> 
    <html lang="en"> 
    <head> 
     <title></title> 
     <style type="text/css"> 
      body 
      { 
       font-size: 9pt; 
       font-family: Arial; 
      } 
     </style> 
    </head> 
    <body> 
     Numeric Value: <input type="text" id="text1" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" /> 
     <span id="error" style="color: Red; display: none">* Input digits (0 - 9)</span> 
     <script type="text/javascript"> 
      var specialKeys = new Array(); 
      specialKeys.push(8); //Backspace 
      function IsNumeric(e) { 
       var keyCode = e.which ? e.which : e.keyCode 
       var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1); 
       document.getElementById("error").style.display = ret ? "none" : "inline"; 
       return ret; 
      } 
     </script> 
    </body> 
    </html> 

희망은 당신을 도울 것입니다!