r/PHPhelp • u/Big_Entertainer3156 • Feb 06 '26
Can someone help me with this data php code(i'm new to this)
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "mydatabase";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
if(isset($_POST['submit'])) {
$username=$_POST['username'];
$password=$_POST['password'];
$query = "INSERT INTO registration (username, password)
VALUES ('$username', '$password')";
if (!mysqli_query($dbconnect, $query)) {
die('An error occurred. Your review has not been submitted.');
} else {
echo "Thanks for your review.";
}
}
?>
9
u/Meek_braggart Feb 06 '26
help how?
6
u/Moceannl Feb 06 '26
This is not the way to learn. You are using a very old howto or blog if this is your current code. Learn about safe queries, escaping, sanitising etc. before you put something live on internet.
2
u/MateusAzevedo Feb 06 '26
Several things can be better.
Let's start with the error handling: just do nothing, PHP and MySQLi will report errors by themselves. This means you don't need
if ($dbconnect->connect_error)orif (!mysqli_query($dbconnect, $query)).(Note: if you're on PHP 8.0 or lower, you need to add
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT)just beforemysqli_connect()to configure the error mode to exceptions)Move
mysqli_connect()to afterif(isset($_POST['submit'])). You don't need to connect to the database if it won't be used anyway.Passwords MUST be hashed. In 2026 there's no excuse to not do it properly, specially when PHP makes this a no-brainer.
You MUST use prepared statements to add values to queries, otherwise you risk breaking SQL syntax and will be open to SQL injection attacks. This is also dead easy to do.
Validation is also important. At minimum, you want to check if values were actually provided (i.e. not null or empty).
u/colshrapnel has a great article about MySQLi you can read here. But the TL;DR code is like this:
Even better, learn PHP from a trusted and good source, like Laracasts or Program with Gio, both on YouTube. They will teach stuff the proper way.
1
u/Aggressive_Ad_5454 Feb 06 '26
Little Bobby Tables may be in your future …
1
u/Rosaras Feb 06 '26
Wat is your question / issue exactly?
1
u/equilni Feb 07 '26 edited Feb 07 '26
Answering OP's comment
Since this is PHPHelp, let's focus on PHP first. We want to validate the data before it reaches the database. I will simulate the database to remove that element for now so you can understand what's happening.
Some pseudo code - ie not real code & may have errors:
You want 2 pieces of data - a user and password. Let's set the variables:
(Now, this can easily be your $_POST['username'] or you can assign the variable to the input like
$usr = $_POST['username'] ?? ''.)Next, let's test it. But it's just empty you say? Yes, we can still test that.
User is emptywill be the first thing that will show. We should be returning the error back to the user to have them retry with the requirements we need.Let's add some data to the
$usrvariable and retestGreat, now
Password is emptynow shows. Same thing, send it back to the user to resend based on our requirementsWhat's next? username length? Let's check for that:
Great, return it back if this doesn't work for our application. You can keep going checking the data to make sure it fits and it can be for anything - phone numbers, zip codes, etc.
Next, we would likely check the database to see if the username exists. Let's simulate the database:
Next step, let's hash the password and add the records to the simulated database (This would be the database INSERT)
Var dump on it:
Sandbox link for this
1
u/equilni Feb 06 '26
Formatted for old reddit & mobile users:
It looks like tutorial code. What specifically are you having issues with? That would be helpful to note in the original post.
As usual, with tutorial code, this isn't ideal for learning current practices...
-1
u/OutdoorsNSmores Feb 06 '26
Paste it in a Gemini and ask it. It will not only give you the code, but explain why and if there is something you don't understand, ask it.
Just be sure to mention it has to be safe and secure.
0
u/xreddawgx Feb 06 '26
You should probably use pdo