Database not saving data

this is my code below and error

<?php

session_start();

include('../config/dbconnect.php');

$a = $_POST['name'];

$b = $_POST['office'];

$c = $_POST['zone'];

$d = $_POST['phone'];

$e = $_POST['email'];

// query

$file_name  = strtolower($_FILES['file']['name']);

$file_ext = substr($file_name, strrpos($file_name, '.'));

$prefix = 'vtu_'.md5(time()*rand(1, 9999));

$file_name_new = $prefix.$file_ext;

$path = '../uploads/'.$file_name_new;

    /* check if the file uploaded successfully */

    if(@move_uploaded_file($_FILES['file']['tmp_name'], $path)) {

  //do your write to the database filename and other details   

        $sql = "INSERT INTO excosssss (name,office,zone,phone,email,file) VALUES (:a,:b,:c,:d,:e,:f)";

$q = $db->prepare($sql);

$q->execute(array(':a'=>$a,':b'=>$b,':c'=>$c,':d'=>$d,':e'=>$e,':f'=>$file_name_new));

if($q){

      header("location:profile.php?success=true");

        }else{

            header("location:profile.php?failed=true");

        } 

    }

    ?>

/** ERROR

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘email’

cannot be null in C:\Program Files\Ampps\www\newChurch\admin\save-excosssss.php:32 Stack trace:

#0 C:\Program Files\Ampps\www\newChurch\admin\save-excosssss.php(32): PDOStatement->execute(Array)

#1 {main} thrown in C:\Program Files\Ampps\www\newChurch\admin\save-excosssss.php on line 32

*/

I TRIED ANOTHER MEANS, BUT IT IS NOT WORKING

<?php

include '../config/dbconnect.php';

$name = $_POST['name'];

$office = $_POST['office'];

$zone = $_POST['zone'];

$phone = $_POST['phone'];

$email = $_POST['email'];

// This code will save file into the database

$query = ORM ::for_table('excos')->create();

$query->name=$name;

$query->office=$office;

$query->zone=$zone;

$query->phone=$phone;

$query->email = $email;

$query->save();

  if($query){

      header("location:profile.php?success=true");

        }else{

            header("location:profile.php?failed=true");

        }

        

?>

/** ERROR

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘email’

cannot be null in C:\Program Files\Ampps\www\newChurch\admin\save-excosssss.php:25 Stack trace:

#0 C:\Program Files\Ampps\www\newChurch\admin\save-excosssss.php(25): PDOStatement->execute(Array)

#1 {main} thrown in C:\Program Files\Ampps\www\newChurch\admin\save-excosssss.php on line 25

*/

Well, first of all, please place all your code inside < code > … < /code > tags so we can actually read your code.

But, as you did not tell us which line 1048 is, I will guess. Not really sure of your OBJECT’s especially QRM since that is not shown anywhere. But, the error clearly says the EMAIL data can not be NULL.
This means that your code is not getting the email correctly from your form. Look at where you enter your email address to see what is missing.

Also, normally you get all the inputs from the posted form, then validate every one of them to insure they are within validation rules before you attempt to create a query using them. I do not see that. You do not even check if any of the fields are empty. Good luck.

Sponsor our Newsletter | Privacy Policy | Terms of Service