将文本从表单转换为int
我试图从表单中获取一些数据,但我不能出于某种原因。 这里是我的代码的一些行。
cellphone: <input type = "text" name = "cellphone"><br />
username : <input type = "text" name = "username"><br />
    $cellphone = $_GET["cellphone"];
    //$cellphone = int() $cellphone;
    $username= $_GET["username"];
$link = mysql_connect('myhost', 'myuser', 'mypass') or die("could not connect to database");
    mysql_select_db ('hunter',$link) or die ("could not find database");
    echo "fetced database";
    //injecting user info into database
    mysql_query("INSERT INTO player values ('','$firstname','$lastname','$location','$cellphone','$username','$email','$password')")or die("could not inject into database.");
但由于某些原因,我无法获取单元号码进入我的数据库。 请帮助我:D
  第一: 
  你应该清理所有传入的变量! 
那么,要转换为整数,您可以使用:
$cellphone = (int) $_GET["cellphone"];
  更好的解决方案是使用filter_var(); 
$cellphone = filter_var( $_GET["cellphone"], FILTER_VALIDATE_INT);
在编写任何进一步的代码之前,你应该阅读关于输入验证/ sanitazion和filter_var。
  现在,在人们开始建议mysql_real_escape_string() (做这项工作,但不是最好的解决方案)之前,请通过PDO或MySQLi 
顺便说一下,如果您将电话号码存储为整数,则无法很好地处理国际前缀(例如意大利语为+39或0039),因为“+”不是数字,“00”将在转换为整数....
链接地址: http://www.djcxy.com/p/21881.html上一篇: converting text to int from a form
下一篇: Are there any security vulnerabilities in this PHP code?
