Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dbconfig.php
- ------------------
- <?php
- class Database
- {
- private $host = "localhost";
- private $db_name = "dblogin";
- private $username = "root";
- private $password = "";
- public $conn;
- public function dbConnection()
- {
- $this->conn = null;
- try
- {
- $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
- $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $exception)
- {
- echo "Connection error: " . $exception->getMessage();
- }
- return $this->conn;
- }
- }
- ?>
- -------------------------------------
- addnew.php
- <?php
- error_reporting( ~E_NOTICE ); // avoid notice
- require_once 'dbconfig.php';
- if(isset($_POST['btnsave']))
- {
- $username = $_POST['user_name'];// Nome
- $userdescricao = $_POST['user_descricao'];// Descricao
- $usersexo= $_POST['user_sexo']; //Sexo M/F
- $useridade= $_POST['user_idade']; //Idade
- $userhobbie= $_POST['user_hobbie']; //Hobbie
- $imgFile = $_FILES['user_image']['name'];
- $tmp_dir = $_FILES['user_image']['tmp_name'];
- $imgSize = $_FILES['user_image']['size'];
- if(empty($username)){
- $errMSG = "Escreve o teu nome";
- }
- else if(empty($userdescricao)){
- $errMSG = "Escreve a tua descrição de perfil";
- }
- else if(empty($usersexo)){
- $errMSG = "Insere Sexo";
- }
- else if(empty($useridade)){
- $errMSG = "Insere Idade";
- }
- else if(empty($userhobbie)){
- $errMSG = "Insere Hobbie";
- }
- else if(empty($imgFile)){
- $errMSG = "Envia uma imagem";
- }
- else
- {
- $upload_dir = 'user_images/'; // upload directory
- $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
- // valid image extensions
- $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
- // rename uploading image
- $userpic = rand(1000,1000000).".".$imgExt;
- // allow valid image file formats
- if(in_array($imgExt, $valid_extensions)){
- // Check file size '5MB'
- if($imgSize < 5000000) {
- move_uploaded_file($tmp_dir,$upload_dir.$userpic);
- }
- else{
- $errMSG = "Imagem demasiado grande!";
- }
- }
- else{
- $errMSG = "Apenas formatos, JPG, JPEG, PNG & GIF files são permitidos";
- }
- }
- // if no error occured, continue ....
- if(!isset($errMSG))
- {
- *****error here***
- (Fatal error: Using $this when not in object context in)
- How do I execute the insert?
- $stmt = $this->conn('INSERT INTO users(user_name, descricao, sexo, foto_url, idade, hobbie) VALUES(:uname, :udescricao, :usexo, :upic, :uidade, :uhobbie');
- $stmt->bindParam(':uname',$username);
- $stmt->bindParam(':udescricao',$userdescricao);
- $stmt->bindParam(':usexo',$usersexo);
- $stmt->bindParam(':upic',$userpic);
- $stmt->bindParam(':uidade',$useridade);
- $stmt->bindParam(':uhobbie',$usehobbie);
- if($stmt->execute())
- {
- $successMSG = "novos dados foram inseridos com sucesso...";
- header("refresh:5;index.php"); //redireciona apos 5 segundos
- }
- else
- {
- $errMSG = "error durante inserção de dados";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement