隐藏的表单输入导致PHP注意:未定义的变量:

时间:2014-04-30 13:51:11

标签: php html forms variables error-handling

当用户提交表单并将其中某些字段留空时,要使用预定义数据填充数据库的关联空字段,以下代码首先检查用户是否将表单的字段留空,然后通过隐藏的表单输入将预定义数据插入数据库的相应字段issuing_date reference_detailsname

if ((isset($_POST["submit_form"])) && ($_POST["submit_form] == "Submit")) { 

$issue_date = $_POST['DEFAULT CURRENT_TIMESTAMP'];

$reference = "Not Available";
if(isset($_POST['reference_details']) && !empty($_POST['reference_details'])){  
$reference = $_POST['reference_details'];
}

$drawer_name = "Not Available";
if(isset($_POST['name']) && !empty($_POST['name'])){  
$drawer_name = $_POST['name'];
}

$insertSQL = sprintf("INSERT INTO table (issuing_date, reference_details, name) VALUES (%s, %s, %s)",

GetSQLValueString(trim($issue_date), "date"),
GetSQLValueString(trim($reference), "text"),
GetSQLValueString(trim($drawer_name), "text"));

and do more.......

<input type="submit" name="submit_form" id="submit" value="Submit" />
<input type="hidden" name="issuing_date" value="<?php echo "$issue_date"; ?>" />---->Line110
<input type="hidden" name="reference_details" value="<?php echo "$reference"; ?>" />---->Line111
<input type="hidden" name="name" value="<?php echo "$drawer_name"; ?>" />---->Line112

error_log处于有效状态时,只要form page在网络浏览器中启动,error_log就会在PHP Notice: Undefined variable: issuing_date in /home/user/public_html/dir/subdir/test.php on line 110 PHP Notice: Undefined variable: reference_details in /home/user/public_html/dir/subdir/test.php on line 111 PHP Notice: Undefined variable: name in /home/user/public_html/dir/subdir/test.php on line 112 中生成以下通知示例。

{{1}}

这里出了什么问题?表单的隐藏输入是否定义不正确?

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在初次加载表单时,您尚未向其发布任何数据,因此它永远不会进入此代码块:

首次加载 - 访问该网页

if ((isset($_POST["submit_form"])) && ($_POST["submit_form"] == "Submit")) { // Data received from your submit button is not available because the form was not submitted

    // We never make it here so $issue_date is not available when you need it

    $issue_date = $_POST['DEFAULT CURRENT_TIMESTAMP'];

    // everything else

}

第二次加载 - 将表单提交给自己

if ((isset($_POST["submit_form"])) && ($_POST["submit_form"] == "Submit")) { // Data received from your submit button is available so we enter this block of code

    // We made it here so $issue_date is available later on

    $issue_date = $_POST['DEFAULT CURRENT_TIMESTAMP'];

    // everything else

}

您有三种选择:

1 - 关闭错误报告

^这是最简单的解决方案,应始终在生产环境或面向公众的网站中完成

在文件的最开头,请执行以下操作:

error_reporting(0);

2 - 使用isset()来判断变量是否已被声明

<input type="submit" name="submit_form" id="submit" value="Submit" />
<input type="hidden" name="issuing_date" value="<?php echo (isset($issue_date) ? $issue_date : ''); ?>" />
<input type="hidden" name="reference_details" value="<?php echo (isset($reference) ? $reference: ''); ?>" />
<input type="hidden" name="name" value="<?php echo (isset($drawer_name) ? $drawer_name: ''); ?>" />

3 - 在if(){}块

之前声明变量
$issue_date = NULL;
$reference = NULL;
$drawer_name = NULL;

if ((isset($_POST["submit_form"])) && ($_POST["submit_form"] == "Submit")) {

    $issue_date = $_POST['DEFAULT CURRENT_TIMESTAMP'];

    // everything else
}

答案 1 :(得分:0)

你之前设置了变量吗?

$variable = $_POST['NAME VALUE'];

例如,如果数据库中的列是测试

你必须设置

$variablename = $_POST['test'];

当然你甚至必须运行查询