PHP表单不会向MySQL发送数据

时间:2012-03-09 11:43:19

标签: php mysql

这是两种不会将数据发送到数据库的表单中最短的,但我有一个可以正常工作的表单。当我按提交时,表单刷新,清除页面,不会将数据发送到我的表。我已经包含了我的HTML包,这是一个问题。     

include 'dbc.php';

$err = array();

if(@$_POST['doPersonal'] == 'Personal') 
{

此代码过滤有害的脚本代码并转义所有POST数据的数据 来自用户提交的表格。

foreach($_POST as $key => $value) {
    $data[$key] = filter($value);
}

自动收集主机名或域名,例如example.com)

$host  = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$path   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

if(empty($err)) {

SQL Insert

$sql_insert = "INSERT into `personal`
    (`sex`,`aux_citizen`,`birth_place`,`birth_country`,`children`)
    VALUES
    ('$data[sex]','$data[aux_citizen]','$data[birth_place]','$data[birth_country]',
    '$data[children]')";

mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());

header("Location: thankyou.php");  
exit();
} 
}       
?>

表单的HTML代码:

<html>
<head>
    <title>title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/javascript"
            src="file:///js/jquery.validate.js"></script>
    <tr>
    </tr>
    <script>
        $("#regForm").validate();
        })
        ;
    </script>
    <link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
    <tr>
        <td colspan="3">&nbsp;</td>
    </tr>
    <tr>
        <td width="160" valign="top"><p>&nbsp;</p>

            <p>&nbsp; </p>

            <p>&nbsp;</p>

            <p>&nbsp;</p>

            <p>&nbsp;</p></td>
        <td width="732" valign="top"><p>
            <?php
            if (!empty($err)) {
                echo "<div class=\"msg\">";
                foreach ($err as $e) {
                    echo "* $e <br>";
                }
                echo "</div>";
            }
            ?>
            <br>

            <form action="personal.php" method="post" name="regForm" id="regForm">
                <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">
                    <tr>
                        <td>Sex <font color="#CC0000">*</font></span></td>
                        <td><select name="sex" class="required" id="sex">
                            <option value="" selected></option>
                            <option value="M">Male</option>
                            <option value="F">Female</option>
                        </td>
                    </tr>
                    </select>

我不会把剩下的HTML代码放在其中,因为它会永远存在,但这是提交函数

<input name="doPersonal" type="submit" id="doPersonal" value="Submit">

有人能看到问题吗?它曾经工作过一次,但现在却没有 提前致谢

1 个答案:

答案 0 :(得分:1)

看似提交按钮的name属性和value属性与您在处理器表单中的检查不一致:

// Submit's value should be 'Personal'
if(@$_POST['doPersonal'] == 'Personal') 


// But your value on the button is 'Submit'
<input name="doPersonal" type="submit" id="doPersonal" value="Submit">

// You can change this to:
if($_POST['doPersonal'] == 'Submit') 
// But see the improved version below...

在尝试使用数组密钥之前,请检查是否存在数组密钥,而不是使用@来抑制错误:

if (!empty($_POST['doPersonal']) && $_POST['doPersonal'] == 'Submit') {
   // Handle your form
}