我正在尝试为项目应用设置表单验证。我不断收到我正在调用未定义函数的错误,但我不是我做错了。根据我在网上学到的知识,这是正确的语法。基本上我只希望票号和点号值接受数字作为输入,并显示一条错误消息,提示用户输入数值。到目前为止,这是我的代码:
<html>
<?php
if(!is_number($_POST['tick_num'])) {
echo "please enter a number";
}
$searchBy = $_POST['field'];
$searchFor = $_POST['term'];
$database = 'jy6536';
$db = mysqli_connect('', 'jy6536', '1km4bjRi', $database);
$record = "SELECT * FROM keyfob WHERE $searchBy LIKE '%".$searchFor."%'";
$result = mysqli_query($db, $record);
while($myrow = mysqli_fetch_array($result))
{
echo "<br><br>First Name: ".$myrow['last_name'];
echo "<br> Last Name: ".$myrow['first_name'];
echo "<br> Ticket Number ".$myrow['tick_num'];
echo "<br> Make: ".$myrow['make'];
echo "<br> Spot Number: ".$myrow['spot_num'];
}
?>
<br><br><a href="viewdb.php">Return to Table </a>
<br><a href="search.html">Return to Search </a>
</html>
答案 0 :(得分:0)
http://php.net/manual/en/function.is-numeric.php
if(!is_numeric($_POST['tick_num'])) {
echo "please enter a number";
}
您是否允许使用&#34; + 0123.45e6&#34;等票号?和&#34; 9.1&#34;?也许http://php.net/manual/en/function.ctype-digit.php会更好?
您可以改用此代码:
$ticketNumber = $_POST['tick_num'];
if(!ctype_digit((string)$ticketNumber)) {
echo "please enter a number";
}
这只接受整数,9.1
或+0123.45e6
等ss值不会被接受。