I cant insert data in my database, where I'm going wrong?

Here is my CODE. I’m using php 5.4 and the PDO connection! I included my database connection and some other files i need. And now I have a registration form wich i add some values from filling them. The ridirection is made and not a problem is shown. But when I check the DB is not added. Where I’m going wrong?

<?php
     include'db.php';
     include'header.php';
     require 'vendndodhje.php';

     $VendndodhjeInput = new Vendndodhje();

     if ( !empty($_POST)) {
     // keep track validation errors
     $emerError = null;
     $mbiemerlError = null;
     $dtlError = null;
     $telError = null;
     $emailError = null;
     $vendndodhjeError=null;

     // keep track post values
     $emer = $_POST['emer'];
     $mbiemer = $_POST['mbiemer'];
     $datelindje = $_POST['datelindje'];
     $tel = $_POST['tel'];
     $email = $_POST['email'];
     $Vendndodhje=$_POST['Vendndodhje'];

    // validate input
        $valid = true;
        if (empty($emer)) {
            $emerError = 'Ju lutemi plotesoni emrin!';
            $valid = false;
        }
        
        if (empty($mbiemer)) {
            $mbiemerError = 'Ju lutemi plotesoni mbiemrin!';
            $valid = false;
        }
             if (empty($datelindje)) {
            $dtlError = 'Ju lutemi plotesoni datelindjen tuaj!';
            $valid = false;
        }


         if (empty($tel)) {
            $telError = 'Ju lutemi plotesoni numrin tuaj te telefonit!';
            $valid = false;
         }


          if (!$VendndodhjeInput ->isValid($Vendndodhje)) {
            $vendndodhjeError = 'Ju lutemi plotesoni piken e marketit me te afert per te terhequr karten!';
            $valid = false;
         }


        if (empty($email)) {
            $emailError = 'Ju lutemi plotesoni email-in tuaj!';
            $valid = false;
         } else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
            $emailError = 'Ju lutemi vendosni nje adrese email-i te rregullt!';
            $valid = false;
        }

     //insert values

     if ($valid) {
     $pdo = Database::connect();
     $sql = "INSERT INTO klienti(emer,mbiemer,datelindje,tel,email,Vendndodhje,date_aplikimi) VALUES(?, ?, ?, ?, ?, ?,     NOW())";
    $q = $pdo->prepare($sql);
    $q->execute(array($emer, $mbiemer, $datelindje, $tel,$email, $Vendndodhje));
    }
    header("Location: form'.php");
   }
  ?>

Where is the value for $valid set? Unless there is code missing, it would never be set to true…

1 Like

i adedd the missing code

There are two options:

  1. The $POST is not set, which would skip all of that code entirely
  2. There is an error ($valid = false),

I don’t know if you’re not showing all of the code, or if you just don’t have the code to handle all of the possibilities but one of the two is happening.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.