0

I am trying to post my form with PHP. But by some reason my PHP script is not posting. When I turn error reporting on I get the following errors:

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

I think none of these errors are causing the problem. The scripts sends me to header('Location: ../error.php'); which means the script is not executed. Also when I look in phpmyadmin, I see that the database is not updated.

I have tried to execute the SQL statement in mysql and the sql statement is working.

Does someone know what is wrong in my script and how I can fix the problem?

Here is my full script:

<?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');
}
?>
Abax
  • 54
  • 6
John
  • 904
  • 8
  • 22
  • 56

1 Answers1

1

There's a few issues here:

Notice: Undefined index: date2

This is because the index date2 does not exist in your post ($_POST['date2'])

Notice: Undefined variable: newSingle_cal6

Because date2 does not exist in your post the variable newSingle_cal6 is never set.

Warning: Cannot modify header information - headers already sent

This is because the headers on your page have already been sent before you call header('Location: ../success.php');. This will happen if you call header or echo before that. The headers will also send if you have any raw non-php text in your file before. Make sure there is no space or anything before your opening php tag (<?php).

Find more information on this error here.

Community
  • 1
  • 1
Tony
  • 2,890
  • 1
  • 24
  • 35
  • But is it possible that these issues causing the problem? – John Jul 05 '17 at 22:45
  • @John They will definitely be causing problems. Your redirect (`header('Location: ../success.php')`) will never work, and your date object will never be set. – Tony Jul 05 '17 at 22:48
  • And how can I solve it? I know that the post for `date2` is empty. Because it is empty I am not changing the format to `('Y-m-d');`. I checked also my ` – John Jul 05 '17 at 23:00
  • @John if you are not worried about `date2` being empty ignore those warnings. As for the header issue make sure that you are not sending any headers in those `include_once`. – Tony Jul 05 '17 at 23:03
  • @John maybe try calling `sec_session_start();` at the end of your script. I think that might send headers too. – Tony Jul 05 '17 at 23:04
  • You mean here? `} sec_session_start(); ?>` – John Jul 05 '17 at 23:07
  • I am sending headers in `includes/functions.php`; but the headers are only for errors – John Jul 05 '17 at 23:08
  • I also tried to replace `$stmt->execute(array($_SESSION['user_id']` with `$stmt->execute(array('1'`but this didnt change anything – John Jul 05 '17 at 23:15
  • Use `if(isset($_POST['name1']))` to test if the variable is set before trying to use it. – Barmar Jul 05 '17 at 23:19
  • The warning messages about the undefined variables are causing the "Headers already sent", because they're printing output before you call `header()`. – Barmar Jul 05 '17 at 23:19