I'm having a bit of trouble with register information.
It's supposed to go to a database called "mon" and to a table called "user".This table contains the information "'id','name',and 'pass'".The 'id' is based off of an auto increment system,so doesn't need to be defined in the register page.
No errors are reported,just a reload of the page.But,when visiting the table using phpMyAdmin,it shows that a new row wasn't created.I need help on this because I am fairly new to PHP and don't know much of the kinks of it.
Code:
Pastebin because stackoverflow is acting weird with my code even though I spaced it.
register.php:
<?php
require_once 'connect.php';
if(!empty($_POST)) {
if(isset($_POST['username'], $_POST['password'], $_POST['desc'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if(!empty($username) && !empty($password)) {
$insert = $db->prepare("INSERT INTO 'user' ('name', 'pass') VALUES (?, ?)");
$insert->bind_param('ss', $username, $password);
}
}
}
?>
<html>
<head>
<?php
require_once 'style.php';
?>
<title>si | Registering an Account</title>
</head>
<body>
<?php
require_once 'header.php';
?>
<div id="page">
<h2>Register an Account on Si</h2>
<form action="" method="post">
<input type="text" name="username" placeholder="Username" autocomplete="off">
<input type="password" name="password" placeholder="Password" autocomplete="off">
<button>Register</button>
</form>
</div>
</body>
</html>
connect.php:
<?php
$db = new mysqli('127.0.0.1', 'root', '');
if($db->connect_errno) {
echo "<p id='gotem'>Something has gone wrong.Tell J to fix it.</p>";
}
?>
Any help on this,the creation of a session that marks login,and the redirecting to the index page with the session on is very appreciated.
First of all you forgot to add your database name and password in your mysqli instance
Then in your register.php file you have added a post index which will not be set(
$_POST['desc']
), that's why your code inside your if statement will never be execute since your form does not contain an input name desc.I think its better remove it and please do not forget to execute your prepared statement. The code might be like this.UPDATED CHANGE ' TO `
Adding an attribute type of your button might also be helpful
Line no. 6
I think you have added extra parameter 'desc' to isset statement which is not used anywhere.
Change this line
To following
Add following code.
You have bind the parameter to to sql query but not yet executed.
Add the following code just after $insert->bind_param('ss', $username, $password);
Creating Session
To create session you need to store session value in session variable. You can do this by storing newly registred user id in session variable. Add following code just after $insert->execute();
Redirect to Index Page
Redirection of page done using header function in php
Add following code.
On Index page
To get access to session and session variable. Add following code at very first line.
In conjunction with the other answers given, you are using regular quotes around your table and columns, which are invalid identifier qualifiers.
Which should either be removed
or using ticks which resemble a quote, but is in fact NOT a quote; those are two different animals altogether.
Reference on identifier qualifiers for MySQL:
Passwords
I also noticed that you may be storing passwords in plain text. This is not recommended.
Use one of the following:
crypt()
bcrypt()
scrypt()
password_hash()
function.Other links:
Important sidenote about column length:
If and when you do decide to use
password_hash()
or crypt, it is important to note that if your present password column's length is anything lower than 60, it will need to be changed to that (or higher). The manual suggests a length of 255.You will need to ALTER your column's length and start over with a new hash in order for it to take effect. Otherwise, MySQL will fail silently.
in your file , line 6
if(isset($_POST['username'], $_POST['password'], $_POST['desc'])) {
you dont post
$_POST['desc']
check this.NOTE: Better way is , have else for all of your if , to see what happend
EDIT:
use
$insert->execute();
after
$insert->bind_param('ss', $username, $password);