gpt4 book ai didi

php - 隐藏的表单输入导致 PHP 通知 : Undefined variable:

转载 作者:行者123 更新时间:2023-12-03 07:50:23 24 4
gpt4 key购买 nike

当用户提交表单时,其中一些字段为空,要使用预定义的数据填充数据库的相关空字段,以下代码首先检查用户是否将表单的字段留空,然后将预定义的数据插入相应的字段 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 处于事件状态时,每当在 Web 浏览器中启动 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

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

任何的想法?

最佳答案

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

第一次加载 - 访问页面

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(){} block 之前声明变量
$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
}

关于php - 隐藏的表单输入导致 PHP 通知 : Undefined variable:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23390003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com