我正在尝试使用 PHP 发布我的表单。但由于某种原因我的 PHP 脚本没有发布。当我打开错误报告时,出现以下错误:
Notice: Undefined index: date2
Notice: Undefined variable: newSingle_cal6
Warning: Cannot modify header information - headers already sent
我认为这些错误
都不会导致问题。脚本将我发送到 header('Location: ../error.php');
这意味着脚本未执行。另外,当我查看 phpmyadmin 时,我发现数据库未更新。
我尝试在mysql中执行sql语句,sql语句有效。
有人知道我的脚本出了什么问题以及如何解决该问题吗?
这是我的完整脚本:
<?php
include_once('../../../../includes/db_connect.php');
include_once('../../../../includes/functions.php');
sec_session_start();
$correct = true;
$_SESSION['user_id'];
$pers_id = $_POST['pers_id'] ;
$name = $_POST['name'] ;
$single_cal4 = $_POST['date1'] ;
$single_cal6 = $_POST['date2'] ;
try {
$myDateTime1 = DateTime::createFromFormat('d/m/Y', $single_cal4);
} catch (Exception $e) {
echo $e->getMessage();
}
$newSingle_cal4 = $myDateTime1->format('Y-m-d');
if(!empty($_POST['date2'])){
try {
$myDateTime3 = DateTime::createFromFormat('d/m/Y', $single_cal6);
} catch (Exception $e) {
echo $e->getMessage();
}
$newSingle_cal6 = $myDateTime3->format('Y-m-d');
}
if($correct){
$db = new PDO('mysql:host=localhost;dbname=db', 'root', '');
$query = "INSERT INTO cus(user_id, pers_id, name, date1, date2) VALUES (?, ?, ?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->execute(array($_SESSION['user_id'], $pers_id, $name, $newSingle_cal4, $newSingle_cal6));
header('Location: ../success.php');
}else{
header('Location: ../error.php');
}
?>
最佳答案
这里有几个问题:
注意:未定义索引:date2
这是因为索引 date2
您的帖子中不存在 ( $_POST['date2']
)
注意: undefined variable :newSingle_cal6
因为date2
您的帖子中不存在变量 newSingle_cal6
从未设置。
警告:无法修改 header 信息 - header 已发送
这是因为在您调用 header('Location: ../success.php');
之前,您页面上的 header 已经发送完毕。 。如果您调用 header
就会发生这种情况或echo
在那之前。如果您之前的文件中有任何原始的非 php 文本, header 也会发送。确保打开的 php 标签 ( <?php
) 之前没有空格或任何内容。
查找有关此错误的更多信息 here .
关于PHP 脚本没有执行我的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44937043/