简体   繁体   中英

Insert PHP form in PostgresSQL database

So i'm building a website where you can buy tickets etc. So I want to have a login system, I started building the website and started with the PHP code to sign in but I always get the error Array?? It does work when I only want to insert a variable email and the rest plain text.

I've spend a whole week trying different methods etc. But I don't get why it doesn't work.

I even get the same error when I use constants instead of POST variables...

CREATE TABLE Users(
  userId SERIAL,
  email VARCHAR(40) NOT NULL,
  password VARCHAR(30) NOT NULL,
  firstName VARCHAR(20) NOT NULL,
  lastName VARCHAR(20) NOT NULL,
  age INT NOT NULL,
  organizer BOOLEAN NOT NULL,
  region VARCHAR(30),
  favouriteGenre VARCHAR(15),
  description VARCHAR(200),

  PRIMARY KEY(userId)
);
<?php

  require 'globals.php';

  try {
    $db_conn = new PDO("pgsql:host=$db_host;dbname=$db_name", $db_user, $db_password);
  } catch (PDOException $e) {
    die("Error: ".$e->getMessage()."\n");
  }

  $email = $_POST['email'];
  $password = $_POST['password'];
  $pwdConfirm = $_POST['confirm'];
  $firsName = $_POST['firstName'];
  $lastName = $_POST['lastName'];
  $age = $_POST['age'];
  $rol = $_POST['rol'];
  $region = $_POST['region'];
  $favGenre = $_POST['favGenre'];
  $description = $_POST['description'];

  //TODO inputChecks

  $query = $db_conn->prepare('INSERT INTO users (email, password, firstName, lastName, age, organizer)
                              VALUES (:email, :password, :firstName, :lastName, :age, :organizer)');
  $query->bindParam(':email', $email, PDO::PARAM_STR, 40);
  $query->bindParam(':password', $password, PDO::PARAM_STR, 30);
  $query->bindParam(':firstName', $firstName, PDO::PARAM_STR, 20);
  $query->bindParam(':lastName', $lastName, PDO::PARAM_STR, 20);
  $query->bindParam(':age', $age, PDO::PARAM_INT);
  $query->bindParam(':organizer', $firstName, PDO::PARAM_BOOL);

  if ($query->execute()) {
    echo "success!";
  } else {
    die("Execute query error: ".$db_conn->errorInfo());
  }

  $db_conn = NULL;

I expect it to insert it into the database and don't give an error anymore.

Try this

$query = $db_conn->prepare('INSERT INTO users (email, password, firstName, lastName, age, organizer,region, favouriteGenre, description)
                              VALUES (:email, :password, :firstName, :lastName, :age, :organizer, :region, :favouriteGenre, :description)');
  $query->bindParam(':email', $email, PDO::PARAM_STR, 40);
  $query->bindParam(':password', $pwd, PDO::PARAM_STR, 30);
  $query->bindParam(':firstName', $firstName, PDO::PARAM_STR, 20);
  $query->bindParam(':lastName', $lastName, PDO::PARAM_STR, 20);
  $query->bindParam(':age', $age, PDO::PARAM_INT);
  $query->bindParam(':organizer', $firstName, PDO::PARAM_BOOL);
  $query->bindParam(':region', $region, PDO::PARAM_STR);
  $query->bindParam(':favouriteGenre', $favGenre, PDO::PARAM_STR);
  $query->bindParam(':description',  $description, PDO::PARAM_STR);

One of the possible causes of the error you are getting is that you are trying to insert 6 values into a table with 9 fields.Another possible cause of the bug is that you have defined the variable for password as $pwd but use $password variable when binding parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM