在插入mysql之前,如何让单词lenth判断? 自定义插入单词的每个部分必须大于 3 个字母且少于 50 个字母?谢谢。
//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($addClient) or die(mysql_error());
//}
你的意思是这样?
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());
}
?>
建议:记得清理从 $_POST['名字'] 和 $_POST['姓氏'] 收到的输入
这是正确的方法,但使用多字节strlen()并过滤信息(第一个)。
多字节示例:mb_strlen($_POST["firstname"], "UTF-8") 而不是 strlen($_POST["firstname"])。