This question already has an answer here:
I've been trying to learn php but I got stuck at this basic thing. I really don't know what's wrong. Can u guys look at the code and tell me what's wrong? Thanks.
<?php
session_start();
$connection = mysqli_connect('127.0.0.1', 'root', 'root', 'blogdb');
if(!$connection)
die('error');
$call = '';
$username = $_POST['nickname'];
$password = $_POST['password'];
$password_again = $_POST['password_again'];
if($_POST)
{
if($password != $password_again)
$call = 'Hesla nesouhlasí!';
else if($_POST['year'] != date('Y'))
$call = 'Špatně vyplněný antispam!';
else
{
$exists = mysqli_query('SELECT COUNT(*)
FROM uzivatel
WHERE jmeno=?
LIMIT 1', $username);
if($exists)
$call = 'Uživatelské jméno je obsazeno!';
else
{
mysqli_query("INSERT INTO uzivatel (jmeno , heslo)
VALUES ($username , $password)");
$call = 'Registrace proběhla úspěšně!';
}
}
}
?>
Oh my god, I realized it now. I didn't tell you what was the problem. So the problem was that I couldn't connect to my database. I created new file.php and new DB just to test if I'm doin it right.
$test = mysqli_query($connection ,"INSERT INTO test (jmeno , prijmeni)
VALUES ('test' , 'test')");
This was working so I modified the code but it still doesn't work.
mysqli_query($connection, "INSERT INTO uzivatel (jmeno , heslo)
VALUES ('admin' , 'admin')");
It wont't appear in the DB.
</div>
I changed something and now it works.
<?php
$connection = mysqli_connect('127.0.0.1', 'root', '', 'blogdb');
if(!$connection)
die('error');
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$antispam = $_POST['year'];
$error = '';
if($_POST)
{
if(empty($username) || empty($password) || empty($password2) || empty($antispam))
{
$error = 'Všechny pole musí být vyplněný!';
}
else if($password != $password2)
{
$error = 'Hesla se neshodují!';
}
else if($antispam != date('Y'))
{
$error = 'Špatně vyplněný antispam!';
}
else
{
$exists = mysqli_query($connection, "SELECT jmeno FROM uzivatel WHERE jmeno = '$username'");
if(mysqli_num_rows($exists) > 0)
{
$error = 'Uživatelské jméno je již obsazeno!';
}
else
{
mysqli_query($connection, "INSERT INTO uzivatel (jmeno, heslo) VALUES ('$username', '$password')");
$error = 'Registrace proběhla úspěšně.';
}
}
}
?>
I recommend to please check mysql server username and password and also is mysql server running or not.
Generally on localhost username is root and password is "" (blank).
Also please do check that you have created a database blogdb which you are trying to connect to.
mysqli_connect('127.0.0.1', 'root', 'root', 'blogdb') the third parameter inside the bracket should be your database password, by default, its empty so you can do ('127.0.0.1', 'root', '', 'blogdb'). and make sure your local mysql server is running
Change the password to something other root
and see if it helps.