0

I have created simple form and table insert code. I am getting the 'This page isn’t working localhost redirected you too many times. Try clearing your cookies.' error. I am not sure how to fix this? After the insert the user is redirected back to same page (with hopefully empty form). New developer here - thanks in advance for any help!

<?php
  $errors = [];
  $f['firstname'] = trim($_POST['firstname'] ?? '');
  $f['lastname'] = trim($_POST['lastname'] ?? '');

  if(isset($_POST['submit'])) {
  echo 'doing the validation' . '<br>';

       //Validate form entries  
    if (!$f['firstname']) {
      $errors[] = 'First name is required.';
    }
    if (!$f['lastname']) {
      $errors[] = 'Last name is required.';
    }
 }

 if (!$errors) {
            // Insert request
          echo 'Doing insert' . '<br>';
     $qInsert = "INSERT INTO test
     (ID, FirstName, LastName)
      VALUES (NULL, :firstname, :lastname);";
    try {
        $stmtInsert = $db->prepare($qInsert);
        $stmtInsert->bindParam(':firstname',$f['firstname']);
        $stmtInsert->bindParam(':lastname',$f['lastname']);
        $stmtInsert->execute();
        echo 'after the insert' . '<br>';
        header("Location: testform.php");


    } catch (PDOException $e) {
        logError($e);
        echo 'there were errors on insert' . '<br>';
        $errors[] = 'Sorry we had a problem and could not submit your request.  Please try again.';
    }
  }
  ?>

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?php echo $pageTitle; ?></title>
</head>

<body>
<main id="test-request">
  <h1>Test Request</h1>
  <?php
    if ($errors) {
      //Show form errors
      echo '<h3>Please correct the following errors:</h3>
      <ol class="error">';
      foreach ($errors as $error) {
        echo "<li>$error</li>";
      }
      echo '</ol>';
    }
  ?>

  <div class='addform'>
  <form action='testform.php' method='post' novalidate>
    <label><span>
        First Name: </span><input type='text' id='firstname'name='firstname' size='20' 
        placeholder="Enter First Name" value ="<?= $f['firstname'] ?>" required>
    </label>
    <label><span>
        Last Name: </span><input type='text' id='lastname' name='lastname' size='25' 
        placeholder="Enter Last Name" value ="<?= $f['lastname'] ?>" required>
    </label>
    <button name="submit">Add Request</button>
</form>
</div>

</main>
</body>
Leesa
  • 1
  • 1
  • you need to move the `if (!$errors) {` block inside the `if(isset($_POST['submit'])) {` block. Otherwise, the error checking happens for every request, including GET requests (which occur when doing a normal load of the page, such as after a redirect)...and because there are no errors, therefore you just keep getting redirected. (And it'll also keep inserting blank rows into your database, just for extra fun) – ADyson Dec 03 '19 at 20:35
  • P.S. FYI you'll never ever see `echo 'after the insert' . '
    ';` on the screen, because the browser will ignore the response content returned by your script and instead proceed immediately to doing the instructed redirect. Another non-browser client (such as PostMan, for instance) which doesn't automatically follow redirect headers would enable you to see it, though.
    – ADyson Dec 03 '19 at 20:36
  • (Voted to re-open because the nominated duplicate doesn't cover this error) – ADyson Dec 03 '19 at 20:50
  • I moved the if (!$errors) { line to within as you suggested (and that makes sense). However it is still not doing the insert. I just get back a blank form. When I query the db there is no row added. The if(isset($_POST['submit'])) { is not true I suppose. But why - that is what I cannot figure out. – Leesa Dec 03 '19 at 21:44
  • if it's doing the redirect then it must be doing the insert. That's assuming of course that the query is not failing for some reason. Have you definitely got PDO set up to throw exceptions when queries fail (see https://www.php.net/manual/en/pdo.error-handling.php) ? P.S. a good way to check if code is entering a specific block is to add some simple "echo" commands in specific places (but you'll have to comment out your redirect command temporarily while you're testing, just so you can see the output) so you know exactly which code has been executed. – ADyson Dec 04 '19 at 08:34
  • I see it is trying the insert but getting an error 42S02. It seems to be putting a prefix in front of my table name (prefix.test). Not sure where that is coming from but will investigate. Thanks for the guidance. – Leesa Dec 04 '19 at 13:36
  • Did you specify a specific database to connect to when you created your PDO connection object? That error can sometimes occur if you don't specify a database, and then it tries to use the one configured as the default for the user account, and maybe that isn't the database you actually wanted to run the query against. – ADyson Dec 04 '19 at 14:00
  • Yes it is working now. I specified the table schema in the insert. Thank you so much for your help @ADyson! I don't see how to mark your answers as accepted. Sorry. – Leesa Dec 04 '19 at 14:21

0 Answers0