插入数据库时​​更改数据

时间:2013-10-01 14:13:58

标签: php

我有一个表格,包含全名,电子邮件,电话,当数值插入数据库时​​,电话号码更改为2147483647(全名和电子邮件都可以)。这是我的PHP文件 register.php

<h1>register form</h1>
<?php
$labels=array("full_name"=>"Full Name",
                "email"=>"Email",
                "phone"=>"Phone");
echo "<form action='check_register.php' method='POST'>";
foreach($labels as $key =>$value)
{
    echo "$value <input type='text' name='$key'/><br/>";
}
echo "<input type='submit' value='submit'/>";
echo "</form>";
?>

check_register.php

<?php
$labels=array("full_name"=>"Full Name",
                "email"=>"Email",
                "phone"=>"Phone");
foreach($_POST as $key =>$value)
{
    if(empty($value))
    {
        $empty_value[]=$key;
    }
    elseif($key=="full_name")
    {
        if(!preg_match("/^[A-Za-z '-]{2,50}$/",$value))
        {
            $invalid_value[]=$key;
        }
    }
    elseif($key=="email")
    {
        if(!preg_match("/^[A-Za-z0-9]{5,20}+(@)[A-Za-z0-9]{5,20}(\.com)$/",$value))
        {
            $invalid_value[]=$key;
        }
    }
    elseif($key=="phone")
    {   
        if(!preg_match("/^(\(\d+\)|\d+\-)?\d{10,20}$/",$value))
        {
            $invalid_value[]=$key;
        }
    }
}

if(@sizeof($empty_value)>0 or @sizeof($invalid_value)>0)
{
    if(@sizeof($empty_value)>0)
    {
        echo "input ";
        foreach($empty_value as $key) //loop empty value
        {
            echo " $labels[$key] ";
        }
    }
    if(@sizeof($invalid_value)>0)
    {
        echo "<br/>invalid data ";
        foreach($invalid_value as $key) //loop invalid_value
        {
            echo " $labels[$key]    ";
        }
    }
    echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
    foreach($labels as $key =>$value)
    {
        echo "$value <input type='text' name='$key'/><br/>";
    }
    echo "<input type='submit' value='submit'/>";
    echo "</form>";
    }
else
    {
    $user='root';
    $host='localhost';
    $password='root';
    $dbname='pet';
    $connect=mysqli_connect($host,$user,$password,$dbname) or die("can't connect to server");
        foreach($labels as $key =>$value)
        {
            $good_data[$key]=strip_tags(trim($_POST[$key]));
                if($key=="phone")
                {
                    $good_data[$key]=preg_replace("/[)( .-]/","",$good_data[$key]);
                }
            $good_data[$key]=mysqli_real_escape_string($connect,$good_data[$key]);
        }
        $query="INSERT INTO register ("; 
        foreach($good_data as $key =>$value)
        {
            $query.="$key,"; 
        }
        $query.= ") VALUES (";      
        $query=preg_replace("/,\)/",")",$query);
        foreach($good_data as $key =>$value)   
        {
            $query.="'$value',";
        }
        $query.=")";
        $query=preg_replace("/,\)/",")",$query);
        $query=preg_replace("/(\(\d+\)|\d+\-)/",")",$query);

        $result=mysqli_query($connect,$query) or die ("can't execute query.".mysqli_error($connect));
        echo "$query";
        echo "<h4>member inserted $query </h4>";
    }



?>

1 个答案:

答案 0 :(得分:1)

您的电话号码存储为integer,并且您存储的号码大于integer字段。

您可以选择alter表格,将此字段设为bigintvarchar