Check out part one of creating sign up form.
In the previous class we created a signup form and stored the data in variable and displayed it using echo. Now instead of displaying the data directly, we will store the data in a Mysql database so that we can use the data whenever we need it. Even if you don’t have understanding of sql queries, you can easily get along as i will show you just basic operation and if you have any doubts, just comment ’em up.
MySQL + PHP |
To store the sign up data of users in a database, first of all you need a database. You can see how I create local database using XAMPP here.
After creating your database you need to connect to the database just created and then simply modify the signup function that we created in the user class.
Connet to your database like this-
function __construct() {
$this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->db);
if ($this->mysqli->error) {
echo $this->mysqli->error;
} else
echo”connection successful<br>”;
}
private $host = “localhost”; // default localhost
private $db = “kathford_db”; //name of the database
private $user = “root”; // default root
private $pass = “”; // default password
private $mysqli;
And then modify the signup function of part 1 like this-
function signup() {
//echo “hi i am test<br>”;
$user = $_POST[‘username’];
$email = $_POST[’email’];
$phone = $_POST[‘phone’];
$address = $_POST[‘address’];
$pass = $_POST[‘password’];
echo ‘<br>’ . $user;
echo ‘<br>’ . $pass;
echo ‘<bt>’ . $phone;
echo ‘<br>’ . $address;
echo ‘<br>’ . $email;
$sql = “insert into tbl_user(name,email,address,phone,password) values(‘$user’,’$email’,’$address’,’$phone’,’$pass’)”;
$this->mysqli->query($sql);
if ($this->mysqli->error) {
echo $this->mysqli->error;
} else {
echo “insertion successful”;
}
}
We are done, very simple, wasn’t it? Next we will learn how to sign in a user by checking his email and password, till then, happy coding and if any problems, just comment or connect to me.