这是我的PHP文件,用于将数据插入数据库
我需要验证输入的pmob_number
是否小于10位,然后再将其存储到数据库中。
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect ($servername , $username , $password) or die("unable to connect to host");
$sql = mysqli_select_db ($conn,'vcet') or die("unable to connect to database");
//code to insert into db...
$user_name=$_POST['user_name'];
$roll_number=$_POST['roll_number'];
$user_batch=$_POST['user_batch'];
$user_from=$_POST['user_from'];
$user_year=$_POST['user_year'];
$user_level=$_POST['user_level'];
$F_name=$_POST['F_name'];
$m_name=$_POST['m_name'];
$g_name=$_POST['g_name'];
$mob_number=$_POST['mob_number'];
$addressline1=$_POST['addressline1'];
$addressline2=$_POST['addressline2'];
$city=$_POST['city'];
$pmob_number=$_POST['pmob_number'];
$msg="";
$number_valid=$_POST['pmob_number'];
if(strlen($number_valid)!=10){
$msg='<span class="error">The number you enteres was not 10 digit long</span>';
}else
{
echo"valid";
}
$land_number=$_POST['land_number'];
$cutoff=$_POST['cutoff'];
$dept=$_POST['dept'];
$medium = $_POST['medium'];
$locality=$_POST['locality'];
$intrest=$_POST['intrest'];
$scholar=$_POST['scholar'];
$income=$_POST['income'];
$user_caste=$_POST['user_caste'];
$user_admit=$_POST['user_admit'];
$user_stay=$_POST['user_stay'];
$user_info = "INSERT INTO studentinfo"." (user_name,roll_number,user_batch,user_from,user_year,user_level,F_name,m_name,g _name,mob_number,addressline1,addressline2,city,pmob_number,land_number,cutoff,d ept,medium,locality,intrest,scholar,income,user_caste,user_admit, user_stay)". "VALUES ('$user_name', '$roll_number','$user_batch','$user_from','$user_year','$user_level','$F_name','$m_name','$g_name','$mob_number','$addressline1','$addressline2','$city','$pmob_number','$land_number','$cutoff','$dept','$medium','$locality','$intrest','$scholar','$income','$user_caste','$user_admit','$user_stay')";
if (!mysqli_query($conn,$user_info)) { die('Error: ' . mysqli_error($conn)); } echo "Your information was added to the database.";
mysqli_close($conn);
?>
答案 0 :(得分:1)
您可以使用strlen()
进行服务器端验证
编辑:这必须做到这一点
$num_length = strlen((string)$pmob_number);
if($num_length == 10) {
$user_info = "INSERT INTO studentinfo"." (user_name,roll_number,user_batch,user_from,user_year,user_level,F_name,m_name,g _name,mob_number,addressline1,addressline2,city,pmob_number,land_number,cutoff,d ept,medium,locality,intrest,scholar,income,user_caste,user_admit, user_stay)". "VALUES ('$user_name', '$roll_number','$user_batch','$user_from','$user_year','$user_level','$F_name','$m_name','$g_name','$mob_number','$addressline1','$addressline2','$city','$pmob_number','$land_number','$cutoff','$dept','$medium','$locality','$intrest','$scholar','$income','$user_caste','$user_admit','$user_stay')";
if (!mysqli_query($conn,$user_info)) {
die('Error: ' . mysqli_error($conn));
}
echo "Your information was added to the database.";
mysqli_close($conn);
} else {
$msg='<span class="error">The number you enteres was not 10 digit long</span>';
}