skip to Main Content

Im Not Sure What the issue could be I am able to store the data from the form in variables display them the input does not throw an error but nothing is in the table. I am using Mamp.

<?php 
$server = 'localhost';
$username = 'root';
$password = 'root';
$database = 'dikweb_contact';


try {
    $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e) {
    die("Connection failed ". $e->getMessage());
    exit();
}

$name=$_POST['name'];
echo $name;
echo "<br>";
$email=$_POST['email'];
echo $email;
echo "<br>";
$phone=$_POST['phone'];
echo $phone;
echo "<br>";
$message=$_POST['message'];
echo $message;
echo "<br>";
$created_at=date("D, d M Y");
echo $created_at;

$sql = 'INSERT INTO contacts(name, email, phone, message, created_at) VALUES(:name, :email, :phone, :message, :created_at)';

$statement = $conn->prepare($sql);
$statement->execute([
    ':name' => $name,
    ':email' => $email,
    ':phone' => $phone,
    ':message' => $message,
    ':created_at' => $created_at
]);

$publisher_id = $conn->lastInsertId();

echo 'The publisher id ' . $publisher_id . ' was inserted';

 ?>

3

Answers


  1. Chosen as BEST ANSWER

    The Problem seems to have been with the database itself once I dropped it. Once I recreated a new database with the same name and table it worked. Still have no idea what could have caused this.


  2. Friend You can use this: all php code available here

    <?php Class Database{
    public $host   = DB_HOST;
    public $user   = DB_USER;
    public $pass   = DB_PASS;
    public $dbname = DB_NAME;
    
    public $link;
    public $error;
    
    public function __construct(){
        $this->connectDB();
    }
    
    private function connectDB(){
    $this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
        if(!$this->link){
            $this->error ="Connection fail".$this->link->connect_error;
            return false;
        }
     }}?>
    
    Login or Signup to reply.
  3. Your query related code is fine no problem in this but as I am guessing that that in contacts table you have created a column "created_at" and it’s data type is either DATE or DATETIME.

    if it is correct then this field will save only this date format
    Y-m-d H:i:s

    and according to your php code, your date format is D, d M Y. so mysql query is giving you an error.

    Please change this line of code

    $created_at=date("D, d M Y");

    to

    $created_at=date("Y-m-d H:i:s");

    Otherwise, you want to save only "D, d M Y" format then please change the datatype of created_at field to VARCHAR

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search