· 6 years ago · May 31, 2019, 05:34 PM
1conexion.php
2-------------
3-------------
4-------------
5-------------
6<?php
7
8 $host_db= "localhost";
9 $user_db= "root";
10 $pass_db= "root";
11 $db_name= "tienda";
12?>
13-------------
14-------------
15-------------
16-------------
17registro.html
18-------------
19-------------
20-------------
21-------------
22<!DOCTYPE html>
23<html>
24
25<body>
26
27 <h2>The input Element</h2>
28
29 <form action="/df.php" method="POST">
30 Enter your username:
31 <input name="username" type="text">
32 <br><br> Enter your password:
33 <input name="password" type="password">
34 <br><br> Enter your email:
35 <input name="email" type="text">
36 <br><br> Enter your security answer:
37 <input name="respuesta_de_seguridad" type="text">
38 <input type="submit">
39 </form>
40
41</body>
42
43</html>
44-------------
45-------------
46-------------
47-------------
48df.php
49-------------
50-------------
51-------------
52-------------
53<?php
54include ("conexion.php");
55
56$username = $_POST['username'];
57$password = $_POST['password'];
58$email = $_POST['email'];
59$respuesta_de_seguridad = $_POST['respuesta_de_seguridad'];
60
61try {
62 $conn = new PDO("mysql:host=$host_db;dbname=$db_name", $user_db, $pass_db);
63 // set the PDO error mode to exception
64 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
65 $stmt = $conn->prepare("SELECT username FROM usuarios WHERE username = :username");
66 $stmt->bindParam(':username', $username);
67 $stmt->execute();
68 if ( $stmt->rowCount() > 0 ){
69 echo "
70 <html>
71 <head>
72 <meta http-equiv='REFRESH' content='0 ; url=/registro.html'>
73 <script>
74 alert('The user exist.');
75 </script>
76 </head>
77 </html>
78 ";
79 }
80 else {
81 $stmt = $conn->prepare('INSERT INTO usuarios (username, password, email, respuesta_de_seguridad) VALUES ( :username, :password, :email, :respuesta_de_seguridad)');
82 $stmt-> bindValue(":username",$username,PDO::PARAM_STR);
83 $stmt-> bindValue(":password",$password,PDO::PARAM_STR);
84 $stmt-> bindValue(":email",$email,PDO::PARAM_STR);
85 $stmt-> bindValue(":respuesta_de_seguridad",$respuesta_de_seguridad,PDO::PARAM_BOOL);
86 $stmt->execute();
87 echo"
88 <html>
89 <head>
90 <meta http-equiv='REFRESH' content='0 ; url=/registro.html'>
91 <script>
92 alert('The user has been created.');
93 </script>
94 </head>
95 </html>";
96 }
97 }
98catch(PDOException $e)
99 {
100 echo "You cannot use this query again." . "<br>" . "If you used already this config file, please remove it urgently from the public folder, if not check your database connection data.";
101 }
102?>
103-------------
104-------------
105-------------
106-------------
107CREATE DATABASE tienda;
108CREATE TABLE IF NOT EXISTS usuarios (
109 id INT NOT NULL AUTO_INCREMENT,
110 username VARCHAR(255) NOT NULL,
111 password VARCHAR(255) NOT NULL,
112 email VARCHAR(255) NOT NULL,
113 respuesta_de_seguridad VARCHAR(255),
114 PRIMARY KEY id(id)
115) ENGINE=INNODB;