数据输入不是通过表单在PHP中发生的

时间:2013-07-09 10:41:51

标签: php mysql forms post

当用户通过表单输入时,不会发生数据插入。我试图通过3个字段并排检查用户的姓名,姓氏和电话号码,但数据未插入数据库。 代码为 - >

主页,即allinfo.php:

<html>
<head></head>
<body>
    <?php 
    $labels=array("firstname"=>"FirstName" , "lastname"=>"LastName" ,"phone"=>"PhoneNumber");
    ?>
    <form action='blank.php' method='POST'>
        <?php
    foreach ($labels as $field => $value) 
    {
        echo "<label for='$field'>$value</label>";
        echo "<input type='text'  name='$field'>";
    }
        echo "<input type='submit' value='Submit'>";

       ?>
    </form>
</body>

其他页面:

<html>
<head></head>
<body>
    <?php 
    /*foreach($_POST as $field=>$value)
    {
        echo "$field = $value<br />";
    }*/
    /*foreach($_POST as $field)
    {
        echo "$field <br />";
    }*/
    $labels=array("firstname"=>"FirstName" , "lastname"=>"LastName" ,"phone"=>"PhoneNumber");
    foreach ($_POST as $field => $name) 
    {
        if($field!='lastname')
        {
            if(empty($name))
            {
                $blank_array[]=$field;
            }
        }   
            elseif($field=="phone")
            {
                if(!preg_match("/^ [0-9 ) ( -] {7-20} $/" , $name))
                {
                    $bad_array[]=$field;
                }

            }
    }
    if(@sizeof($blank_array) > 0 or @sizeof($bad_array) > 0)
{
    if(@sizeof($blank_array) > 0)
    {
        echo "<p>You have missed some of the values</p>";
        foreach($blank_array as $name)
        {
            echo "{$labels[$name]}<br />";
        }
    }
    if(@sizeof($bad_array) > 0) 
    {

        echo "Please enter in correct format";
        foreach($bad_array as $name)
        {
            echo "{$labels[$name]}";
        }
    }
        echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";                   /* re-display the form*/
        foreach($labels as $field=>$name)
        {
            $good_data[$field]=strip_tags(trim($_POST[$field]));
            echo "<label for='$field'>$name</label>";
            echo "<input type='text'  name='$field' value='{$good_data[$field]}'>";
        }
        echo "<input type='submit' value='Submit'>";
        echo "</form>";
}

else
{
    $host="localhost";
    $acc="root";
    $password="*******";
    $database="member";
    $cxn=mysqli_connect($host,$acc,$password,$database) or die("can not found");
    foreach($labels as $field=> $name)
    {
        $good_data[$field]=strip_tags(trim($_POST[$field]));
        if($field=="phone")
        {
            $good_data[$field]=preg_replace("/ [) ( . -] / " , " " , $good_data[$field]);
        }   
            $good_data[$field]=mysqli_real_escape_string($cxn,$good_data[$field]);
    }
        $query="insert into user (";
            foreach($good_data as $field =>$name)
            {
                $query.="$field";
            }
        $query.=") VALUES (";
        $query=preg_replace("/,\)/",")",$query) ;
        foreach($good_data as $field =>$name)
            {
                $query.="'$name',";
            }
            $query.=")";
            $query=preg_replace("/,\)/",")",$query) ;
            $result=mysqli_query($cxn,$query);
            echo "new entry";

}
    echo "<p>All the information is available &hearts; </p>"    ;

    ?>
</body>

请帮忙。

1 个答案:

答案 0 :(得分:0)

更改构建查询字符串的循环

foreach($good_data as $field =>$name)
        {
            $query.="$field";
}

foreach($good_data as $field =>$name)
        {
            $query.="$field,";
}

注意你错过了昏迷。