doulianglou0898 2015-08-04 23:45
浏览 50

使用PREG_MATCH在PHP中进行表单验证[重复]

This question already has an answer here:

I'm sure I'm just doing something stupid but I think I'm close. What I'm trying to do as add validation to my submission. With my current code regardless of what data I enter into the serial field it always comes up with Invalid Serial. Any suggestions?

<?php

$serial=$_POST['serial'];
$model=$_POST['model'];
$deviceCondition=$_POST['deviceCondition'];
$sealCondition=$_POST['sealCondition'];
$location=$_POST['location'];
$deployDate=$_POST['deployDate'];
$weight=$_POST['weight'];
$connectedTerminal=$_POST['connectedTerminal'];
$notes=$_POST['notes'];

//NEW PDO connection

$serialVal = "[a-zA-Z0-9-]+"; 
if ( preg_match( $serialVal, $serial ) ) {

try{   


$conn = new PDO("mysql:host=$sql_server;dbname=$sql_db", $sql_user, $sql_pass);

$sql = "INSERT INTO web01dev4s2.ingenicoInfo (serial, model, deviceCondition, sealCondition, location, deployDate, weight, connectedTerminal, notes) VALUES ('".$serial."', '".$model."', '".$deviceCondition."', '".$sealCondition."', '".$location."', '".$deployDate."', '".$weight."', '".$connectedTerminal."', '".$notes."')";


$q = $conn->prepare($sql);
$result_1=($sql); 

$q->execute(); 
}
catch (PDOException $pe) {
    die("Could not connect to the database" . $pe->getMessage());
}


$count = $q->rowCount();
print("Saved $count record(s).
");
header( "refresh:2;url=devicelist.php" );

}

else { 
 echo $serial . "Invalid serial number.";
} 

?>
</div>
  • 写回答

1条回答 默认 最新

  • dongye9071 2015-08-05 00:05
    关注

    You forgot the delimiters "/"

    if(preg_match("/[a-zA-Z0-9-]+/", $serial))
            echo 'oui';
        else
            echo 'no';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据