开发者

php insert into mysql after a word lenth judge

开发者 https://www.devze.com 2023-03-04 00:01 出处:网络
Before insert into mysql, how to make a word lenth judge ? that the custom insert each part of word must be more than 3 letters and less than 50 letters? Thanks.

Before insert into mysql, how to make a word lenth judge ? that the custom insert each part of word must be more than 3 letters and less than 50 letters? Thanks.

//if(strlen($_POST['firstname'])>=3 & strlen($_POST['lastname'])>=3){
$firstname = htmlspecialchars(trim($_POST['firstname']));
$lastname = htmlspecialchars(trim($_POST['lastname']));
$addClient  = "INSERT INTO contact (firstname,lastname) VALUES ('$firstname','$lastname')";
mysql_query($add开发者_StackOverflow中文版Client) or die(mysql_error());
//}


You mean something like this?

if($_SERVER['REQUEST_METHOD'] == "POST") {
  $fnlenght = strlen($_POST['firstname']);
  $lnlenght = strlen($_POST['lastname']);

  if(($fnlenght > 2 && $fnlength < 51) && ($lnlenght > 2 && $lnlenght < 51)) {
    $addClient = "INSERT INTO contact (firstname,lastname) VALUES ('".mysql_real_escape_string($_POST['firstname'])."','".mysql_real_escape_string($_POST['lastname']."')";
    mysql_query($addClient) or die(mysql_error());
  } else {
    echo "Lenght of first- and/or lastname was not correct.";
  }
}


<?php

   if(strlen($_POST['firstname']) > 3 && strlen($_POST['firstname']) < 50 && strlen($_POST['lastname']) > 3 && strlen($_POST['lastname']) < 50){
      $firstname = htmlspecialchars(trim($_POST['firstname']));
      $lastname = htmlspecialchars(trim($_POST['lastname']));
      $addClient  = "INSERT INTO contact (firstname,lastname) VALUES ('$firstname','$lastname')";
      mysql_query($addClient) or die(mysql_error());
   }

?>

A suggestion: remember to sanitize the input you receive from $_POST['firstname'] and $_POST['lastname']


That's the right way, but use the multibyte strlen() and filter the information (first).

Example for multibyte : mb_strlen($_POST["firstname"], "UTF-8") instead of strlen($_POST["firstname"]).

0

精彩评论

暂无评论...
验证码 换一张
取 消