PHP脚本没有执行我的语句

时间:2017-07-05 22:33:58

标签: php mysql

我正在尝试使用PHP发布我的表单。但由于某种原因,我的PHP脚本没有发布。当我打开错误报告时,我收到以下错误:

Notice: Undefined index: date2
Notice: Undefined variable: newSingle_cal6
Warning: Cannot modify header information - headers already sent 

我认为这些errors都不会导致问题。脚本将我发送到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');
}
?>

1 个答案:

答案 0 :(得分:1)

这里有一些问题:

注意:未定义的索引:date2

这是因为您的帖子中不存在索引date2$_POST['date2']

注意:未定义的变量:newSingle_cal6

由于您的帖子中不存在date2,因此永远不会设置变量newSingle_cal6

警告:无法修改标头信息 - 已发送的标头

这是因为在您致电header('Location: ../success.php');之前,页面上的标题已经发送过了。如果您在此之前致电headerecho,就会发生这种情况。如果您之前在文件中有任何原始的非php文本,标题也会发送。在打开php标记(<?php)之前确保没有空格或任何内容。

查找有关此错误的更多信息here

相关问题