Advertisement
Guest User

Untitled

a guest
Dec 13th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. dbconfig.php
  2.  
  3. ------------------
  4.  
  5. <?php
  6. class Database
  7. {
  8. private $host = "localhost";
  9. private $db_name = "dblogin";
  10. private $username = "root";
  11. private $password = "";
  12. public $conn;
  13.  
  14. public function dbConnection()
  15. {
  16.  
  17. $this->conn = null;
  18. try
  19. {
  20. $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
  21. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  22. }
  23. catch(PDOException $exception)
  24. {
  25. echo "Connection error: " . $exception->getMessage();
  26. }
  27.  
  28. return $this->conn;
  29. }
  30. }
  31. ?>
  32.  
  33.  
  34. -------------------------------------
  35.  
  36. addnew.php
  37.  
  38. <?php
  39. error_reporting( ~E_NOTICE ); // avoid notice
  40. require_once 'dbconfig.php';
  41.  
  42. if(isset($_POST['btnsave']))
  43. {
  44. $username = $_POST['user_name'];// Nome
  45. $userdescricao = $_POST['user_descricao'];// Descricao
  46. $usersexo= $_POST['user_sexo']; //Sexo M/F
  47. $useridade= $_POST['user_idade']; //Idade
  48. $userhobbie= $_POST['user_hobbie']; //Hobbie
  49.  
  50. $imgFile = $_FILES['user_image']['name'];
  51. $tmp_dir = $_FILES['user_image']['tmp_name'];
  52. $imgSize = $_FILES['user_image']['size'];
  53.  
  54.  
  55. if(empty($username)){
  56. $errMSG = "Escreve o teu nome";
  57. }
  58. else if(empty($userdescricao)){
  59. $errMSG = "Escreve a tua descrição de perfil";
  60. }
  61.  
  62. else if(empty($usersexo)){
  63. $errMSG = "Insere Sexo";
  64. }
  65.  
  66. else if(empty($useridade)){
  67. $errMSG = "Insere Idade";
  68. }
  69.  
  70. else if(empty($userhobbie)){
  71. $errMSG = "Insere Hobbie";
  72. }
  73.  
  74. else if(empty($imgFile)){
  75. $errMSG = "Envia uma imagem";
  76. }
  77. else
  78. {
  79. $upload_dir = 'user_images/'; // upload directory
  80.  
  81. $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
  82.  
  83. // valid image extensions
  84. $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
  85.  
  86. // rename uploading image
  87. $userpic = rand(1000,1000000).".".$imgExt;
  88.  
  89. // allow valid image file formats
  90. if(in_array($imgExt, $valid_extensions)){
  91. // Check file size '5MB'
  92. if($imgSize < 5000000) {
  93. move_uploaded_file($tmp_dir,$upload_dir.$userpic);
  94. }
  95. else{
  96. $errMSG = "Imagem demasiado grande!";
  97. }
  98. }
  99. else{
  100. $errMSG = "Apenas formatos, JPG, JPEG, PNG & GIF files são permitidos";
  101. }
  102. }
  103.  
  104.  
  105. // if no error occured, continue ....
  106. if(!isset($errMSG))
  107. {
  108.  
  109. *****error here***
  110. (Fatal error: Using $this when not in object context in)
  111.  
  112. How do I execute the insert?
  113.  
  114. $stmt = $this->conn('INSERT INTO users(user_name, descricao, sexo, foto_url, idade, hobbie) VALUES(:uname, :udescricao, :usexo, :upic, :uidade, :uhobbie');
  115.  
  116. $stmt->bindParam(':uname',$username);
  117. $stmt->bindParam(':udescricao',$userdescricao);
  118. $stmt->bindParam(':usexo',$usersexo);
  119. $stmt->bindParam(':upic',$userpic);
  120. $stmt->bindParam(':uidade',$useridade);
  121. $stmt->bindParam(':uhobbie',$usehobbie);
  122.  
  123. if($stmt->execute())
  124. {
  125. $successMSG = "novos dados foram inseridos com sucesso...";
  126. header("refresh:5;index.php"); //redireciona apos 5 segundos
  127. }
  128. else
  129. {
  130. $errMSG = "error durante inserção de dados";
  131. }
  132. }
  133. }
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement