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');
}
?>

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

But is it possible that these issues causing the problem?
@John They will definitely be causing problems. Your redirect (header('Location: ../success.php')) will never work, and your date object will never be set.
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 <?php tags. I have no spaces before or after the tags <?php and ?>
@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.
@John maybe try calling sec_session_start(); at the end of your script. I think that might send headers too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.