I've been searching for a couple of hours trying to figure this out:
person types his username in a text field and hits submit, then php checks if the username is in the database(in the table username). What method do i use? or what statement. Because this is really confusing me.
my database is named arcforum table is named afusers the rows are filled with usernames and what not.
Visual reference:
I tried using:
SELECT username FROM afusers WHERE username LIKE 'DarkEyeDragon';
But this returns nothing.
PS: I'm not asking for the completely filled in code(although always welcome). Just a method I can use to do this. Or a simular question.
Thanks in advance.
EDIT: http://i.imgur.com/4V0Cay1.png Proof table/database is not empty
Code:
include_once 'psl-config.php';
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if ($mysqli->connect_error) {
header("Location: ../error.php?err=Unable to connect to MySQL");
exit();
}
The next part just prints out every row in the table. Just to make sure everything is working as intended.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "| Name: " . $row["username"]. "<br>email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
exit();
after that i'm not entirly sure how I would utilise the code to check if the words are the same.
$mysqli->query('select * from afusers where username = someusername')
You can verify the row quantity with $mysqli->num_rows
And then you can fetch your data like this:
$user = mysqli_fetch_object($mysqli)
That will give you the desired user if the username is unique.
Of course you could use a variable for the username like:
$mysqli->query('select * from afusers where username ="' . $username . '"')
Related
i'm trying to learn PHP/MySql and create a simple "website".
I have a problem while i try to print a row from a Select. I created a Database with the table "Utenti" and i want to select an username from it.
$username='Prova';
// In my database there is a record with username "Prova"
require 'connessione_db.php';
// I connect Php to the Database in another file
mysqli_select_db ($conn, $dbname);
$checkusername= "SELECT username FROM utenti WHERE username= '$username' ";
// when i write "Where username='Prova' " there aren't problem.
$username_result=mysqli_query($conn,$checkusername);
if (mysqli_num_rows($username_result) > 0) {
while($rows=mysqli_fetch_array($username_result)) {
echo $rows['username'];
}
} else {
echo "no";
}
With actual code the result is "no", but if i write:
... where username='Prova'";
the result is "Prova".
I tried to change the code reading user's solutions without success. I tried:
" ... Where username='" .$username. "'";
or
mysqli_real_escape_string($conn,$username);
and other useless advice.
When you're adressing to your sql server you're using string so you jsut need to merge two strings so username='" .$username. "'" should work just fine. What might work is something like "...username='{$username}". You should debug this string by using error.log($checkusername) what's wrong with your string. Also what version of php you currently using? Best stable version are 5.6 or 7.3.4 although 7+ versions isn't popular.
This system is based on invitation codes, if u have a code that is present in the database you can submit the input therefore change a value in a row. There are 2 inputs, 1) Invitation Code (key), if exist in the database the user can submit the value 2)Name (user). I done the following code but it doesn't work, any suggestions?
<?php
//get value pass from form in login.php
$username = $POST['user'];
$password = $POST['key'];
//connect to the server and select database
mysql_connect("localhost", "...","...");
mysql_select_db("...");
// Query the database for user
$result = mysql_query("UPDATE invitation_keys SET name ='$username' WHERE key = '$password'";)
or die("Failed to query database".mysql_error());
$row = mysql_fetch_array($result);
if ($row['key'] == $password) {
echo "Login success!!!".$row['key'];
} else {
echo "Failed to login";
}
?>
When you are coding in PHP, var_dump($var) is your best friend.
So the first thing to do here, is to print the query.
You will see, that your $username and $password vars are NULL, because you missed the syntax of $_POST[].
After, you can put in var_dump what you want, and that's why its interesting, because you will debug faster with this.
Ok, I'm confused. I have some code that searches a database table for a username, and then uses an if else statement to run some code depending on if the user is found or not. My code is below. The problem is that the code isn't even seeing the if else statement, and I have no idea why. Any help is appreciated.
$sqluser = "select * from users where username='" . $user ."'"; //Searching to see if the user is in the database
echo $sqluser . "<br><br>"; //writes out the select statement to make sure it is correct
$query = mssql_query($sqluser); //returns the results
$num_rows = mssql_num_rows($query); //gets the number of rows returned
echo $num_rows; //writes out the number of rows
if ($num_rows==0) //determines what happens next if the user exists or not
{
//displays an error box if the user doesn't exist
echo "<script type=text/javascript>";
echo "alert('That user doesn't exist. Please try again.')";
echo "</script>";
}
else
{
//will be code to run if the user does exist
echo "<script type=text/javascript>alert('Testing.')</script>";
}
I couldn't add a comment. So I will write this as an answer instead.
Since you state that the alert JavaScript is showing in the page source, this mean that the IF/ELSE statement in PHP is working fine. The problem is with the single quote. You have a single quote inside a single quoted alert function. Hence the JavaScript alert function cannot be executed.
echo "alert('That user doesn't exist. Please try again.')";
Try using this instead
echo "alert('That user doesn\'t exist. Please try again.');";
So I have the code below (and at the top the database connector and session start). However, he doesn't echo the amount of cubes (a valuta on my site).
$cubes = mysql_query("SELECT cubes FROM leden WHERE id='" . $_SESSION['id'] . "'");
echo "You currently have " . $cubes;
What is going wrong here? The database is there, the tables and everything exist.
The mysql_query function returns you a result set. So if you want to get the information you must also do :
$cubesRow = mysql_fetch_object($cubes);
echo "You currently have " . $cubesRow->cubes;
Help with user deletion:
Hello I am creating a user creation system for a project of mine, I am still very new to PHP, my issue is getting the user from the MySQL database and then deleting it, I will show you my code below:
<?php
require_once("config/db.php");
if ($login->isUserLoggedIn() == true) {
if ($_SESSION['user_perm'] == 1) {
//Create Connection
$db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Check connection
if ($db_connection->connect_error) {
die("Connection failed: " . $db_connection->connect_error);
}
$sql = "SELECT user_name FROM users";
$result = $db_connection->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["user_name"];
$user_name_delete = $row["user_name"];
$_SESSION['user_name_delete'] = $user_name_delete;
echo 'Delete User<br>';
}
}
}
$db_connection->close(); ?>
On the deleteuser.php page my code is, note this was just a test:
<?php
echo $_SESSION['user_name_delete'];
?>
My issue with this is grabbing the user who you selected to delete as at the moment it only outputs the last user grabbed from the database.
Any help is much appreciated.
This value:
$_SESSION['user_name_delete']
Is going to contain only the last user in your data. Because you keep overwriting it in your loop:
$_SESSION['user_name_delete'] = $user_name_delete;
The short answer is... Don't use session state for this. (Really, you shouldn't use session state for much of anything unless you absolutely have to.) The identity of the user to be deleted should be included in the request to delete the user. In this case, you can add it to the link. Something like this:
echo 'Delete User<br>';
(Or whatever you use to identify the user in the data row.)
Then in deleteuser.php you can get that value from:
$_GET['id']
Validate the inputs, validate that the user is authorized to perform the delete, and then use that value in the WHERE clause of your DELETE query.
Get the users id and insert it into a delete query.
$id = $db->real_escape_string($_GET['id']);
$sql = "delete from users where id = " . $id; and then run the query to delete the user from the database.