doq70020
2015-05-03 16:27
浏览 34
已采纳

需要检查才能在php中输入浮点数?

I want to validate and that the user only enters float number in the text field such as 3.4. I need to check only the $value1 and $value2 in this case using php.

libraryForm.php

<form action="savelibscores.php" method="POST">
S3: <input class="inputfield" type="text" name="s3" size="5"> <br /><br/>
S4: <input class="inputfield" type="text" name="s4" size="5"> <br /><br/>
<b>Year:<b/>
<select name="year"> 
<option value="Choose">Please select..</option>
<option value="2005">2005</option> 
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option></select><br/><br/>

<br/>
<input type="submit" value="Save" name="submit">
<input type="reset" name="reset" value="Clear"><br /><br/>

</form>

savelibscores.php

<?php

 define('DB_NAME','');
 define('DB_USER','');
 define('DB_PASSWORD','');
 define('DB_HOST','localhost');

 $connect = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

 if(!$connect){
      die('Could not connect:'.mysql_error());
 }

 $db_selected=mysql_select_db(DB_NAME,$connect);

 if(!$db_selected){
    die('Can\'t use'.DB_NAME.':'.mysql_error());
 }

 if(isset($_POST['submit'])){
    $value1=$_POST['s3'];
    $value2=$_POST['s4'];
   $value3=$_POST['year'];
 if(!empty($value1) && !empty($value2) && !empty($value3)){
       $sql=mysql_query("INSERT INTO `library`(s3,s4,year) VALUES ('".$value1."','".$value2."','".$value3."')")or die(mysql_error());
}
   else{
    echo "Please fill all the fields.";
   }
}
?>

图片转代码服务由CSDN问答提供 功能建议

我想验证并且用户只在文本字段中输入浮点数,例如 3.4 。 在这种情况下,我需要使用 php 检查 $ value1 $ value2

libraryForm .php

 &lt; form action =“savelibscores.php”method =“POST”&gt; 
S3:&lt; input  class =“inputfield”type =“text”name =“s3”size =“5”&gt;  &lt; br /&gt;&lt; br /&gt; 
S4:&lt; input class =“inputfield”type =“text”name =“s4”size =“5”&gt;  &lt; br /&gt;&lt; br /&gt; 
&lt; b&gt;年:&lt; b /&gt; 
&lt; select name =“year”&gt;  
&lt; option value =“选择”&gt;请选择..&lt; / option&gt; 
&lt; option value =“2005”&gt; 2005&lt; / option&gt;  
&lt; option value =“2006”&gt; 2006&lt; / option&gt; 
&lt; option value =“2007”&gt; 2007&lt; / option&gt; 
&lt; option value =“2008”&gt; 2008&lt; / option&gt; 
&lt;  ; option value =“2010”&gt; 2010&lt; / option&gt; 
&lt; option value =“2011”&gt; 2011&lt; / option&gt; 
&lt; option value =“2012”&gt; 2012&lt; / option&gt;&lt; / select&gt  ;&lt; br /&gt;&lt; br /&gt; 
 
&lt; br /&gt; 
&lt; input type =“submit”value =“保存”name =“submit”&gt; 
&lt; input type =“ 重置“name =”reset“value =”清除“&gt;&lt; br /&gt;&lt; br /&gt; 
 
&lt; / form&gt; 
   
 
 

savelibscores.php

 &lt;?php 
 
define('DB_NAME',''); 
 define('DB_USER',''); 
  define('DB_PASSWORD',''); 
 define('DB_HOST','localhost'); 
 
 $ connect = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
 
 if(!$ connect){  
 die('无法连接:'。mysql_error()); 
} 
 
 $ db_selected = mysql_select_db(DB_NAME,$ connect); 
 
 if if(!$ db_selected){
 die(' 不能使用'.DB_NAME。':'。mysql_error()); 
} 
 
 if(isset($ _ POST ['submit'])){
 $ value1 = $ _ POST ['s3'  ]; 
 $ value2  = $ _ POST ['s4']; 
 $ value3 = $ _ POST ['year']; 
 if(!empty($ value1)&amp;&amp;  !empty($ value2)&amp;&amp;  !empty($ value3)){
 $ sql = mysql_query(“INSERT INTO` library`(s3,s4,year)VALUES('”。$ value1。“','”。$ value2。“','”  。$ value3。“')”)或死(mysql_error()); 
} 
其他{
 echo“请填写所有字段。”; 
} 
} 
?&gt; 
 <  / code>  
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtdsbakn210537 2015-05-03 17:11
    已采纳

    You have a couple of options ..

    is_float() function

    filter_var() function, with the FILTER_VALIDATE_FLOAT filter as second argument.

    floatval() or the (float) cast before your variable name.

    The first two will check wether or not it's a float, and the third one will convert whatever you give it to a float. I like to use the filter_var() function as it makes the code clear, and it,s also very useful to verify all sort of things (emails adress, IP adress, URL, etc).

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题