How can I connect one row to tables in database? - php

I'm currently working on a system for our school, and I'm having a signup/login/attendance system.
The problem is that I want to make a user which is in a row of my table to connect to another table. It is for him only the one who can access that table because it seems that whenever we login another user it redirects on the same webpage.
Is there a way I can fix that?
<?php
$mymail = $_POST["mymail"];
$mypass = $_POST["mypass"];
echo "$mymail";
//database connection to check inside table and query email and password
$servername = "localhost";
$username = "root";
$password = "admin";
$dbname = "sistema";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM userlogin where mymail ='" . $mymail . "' and mypass = '" . $mypass . "'";
$result = $conn->query($sql);
// echo $sql;
if ($result->num_rows > 0) {
// output data of each row
// while($row = $result->fetch_assoc()) {
// echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["idnumber"]. "<br>";
// }
header("Location: http://localhost/sistema/attendance/index.php"); /* Redirect browser */
} else {
header("Location: http://localhost/sistema/signup/index2.html"); /* Redirect browser */
// echo "0 results";
}
$conn->close();
?>
//THIS IS JUST THE CONNECTION TO THE DATABASE AS WELL AS THE QUERY, I separated the HTML form

you can store him user id in a session
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
//after login that user
$_SESSION["id"] = $_Post['id'];
$_SESSION["id"] = $_Post['username'];
?>
you have to start session every page and you can use that user id anyway on your website
to call $_SESSION["id"]
if he click logout
unset($_SESSION["id"]); & unset($_SESSION["id"]);

<?php
//Start session
session_start();
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn){
die('not conected :'.mysqli_error());
}
#$conn =new mysqli("localhost","root","","mystudy");
$email = $_POST['email'];
$password = $_POST['psw'];
$qry="SELECT * FROM users_table WHERE user_email='$email' AND user_password='$password'";
$result=mysqli_query($conn,$qry);
if($result) {
if(mysqli_num_rows($result) > 0) {
//Login Successful
$users = mysqli_fetch_assoc($result);
session_regenerate_id();
$_SESSION['SESS_user_ID'] = $users['id'];
$_SESSION['SESS_user'] = $users['user_name'];
$_SESSION['SESS_user_EMAIL'] = $users['user_email'];
session_write_close();
header("Location: /any your path url/page.php");
}else{
header("Location: /any your path url/error_page.php");
}
?>
index.php
<?php
//Start session
session_start();
$userid=$_SESSION['SESS_user_ID'];
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM users_table WHERE id='$userid'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
?>
<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>EMAIL</th>
</tr>
<tr>
<?php
while($row = mysqli_fetch_assoc($result)) {
<td><?php echo $row["id"];?></td>
<td><?php echo $row["username"];?></td>
<td><?php echo $row["email"];?></td>
<?php } }?>
</tr>
</table>

Related

Echo only current user details

please can someone help me with this code? It shows all the users’ info but i need it to show only the info of the logged user.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "username";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT AEG FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 3) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["AEG"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Upon login, store the email/username/id in a $_SESSION variable;
$_SESSION['email'] = $email; // in this example I used email
Then on your file, you can access session variables using $_SESSION['variable'] and use it on your sql statement;
My modifications are the ones with comments.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "username";
/*Store session data in a variable*/
$email = $_SESSION['email'];
/**********************************/
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/*Add where clause to your sql statement*/
$sql = "SELECT AEG FROM users WHERE email ='".$email."'";
/****************************************/
$result = $conn->query($sql);
if ($result->num_rows > 3) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["AEG"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Try this one if you are having 4 user then it is showing only one
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "id: " . $row["AEG"]. "<br>";
}
}
else
{
echo "0 results";
}

What is the best way to simply show the first name, on html page, retrieved from database?

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'blood_db';
$connection = new mysqli($dbhost,$dbuser,$dbpass,$db);
if($connection->connect_error){
die("Server Error ".$connection->connect_error);
}
else{
$query = ("SELECT first_name FROM accounts WHERE email_address = '".$_SESSION['username']."'");
$obj = $connection->query($query);
echo "Welcome ".$obj->first_name;
here it shows a notice, which is "
Notice: Undefined property: mysqli_result::$first_name "
It is returning the object, So how would i extract the first name from it?
// printf("Welcome %s",$result->);
echo '<br>Logout';
use below code
$result = $connection->query($query);
if($result){
$row = $result->fetch_assoc();
echo "Welcome ".$row['first_name'];
}else{
// check error
}
you can try this which select data and add condition if you want to check that data empty or not
$obj = $connection->query($query);
if ($obj->num_rows > 0) {
$row = $obj->fetch_assoc();
echo "Welcome ".$row['first_name'];
}
if you do not want to check data have then use this
$row = $obj->fetch_assoc();
echo "Welcome ".$row['first_name'];
for more information
https://www.w3schools.com/php/php_mysql_select.asp
A very basic example would be like this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$firstname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM tbl_test WHERE email_address = '". $_SESSION['username']. "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Retrieve Firstname
while($row = $result->fetch_assoc()) {
$firstname = $row["first_name"];
}
} else {
echo "No results found!";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<header>
<h3>Welcome <?php echo $firstname; ?></h3>
</header>
</body>
</html>
<?php
$conn->close();
?>

Dynamically generate buttons with loop php

What I wan't to do is create buttons that are automatically generated from the database. So when I add a new record in the database the button is created Is this possible with a loop? So yes how do I create the button.
This is what I have so far:
<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM theme";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row["theme_name"]. "<br>";
}
} else {
echo "no results";
}
$conn->close();
?>
Yes it is possible. you need to echo html
<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM theme";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$your_url ="https://www.google.com";
echo "". $row["theme_name"]. "<br>";
echo '<input type="button" name="' . $row["theme_name"]. '" value="'. $row["theme_name"].'">';
}
} else {
echo "no results";
}
$conn->close();
?>

PHP Loop through results

I am trying to loop through my database and check to see if the user already exists in another table. If they do then I want to increment a value, if they don't then I want to add the user.
When I run the code below it happily loops through all the results:
<?php
$servername = "p:10*********";
$username = "*******";
$password = "*******";
$dbname = "******";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM payroll WHERE user != ' ' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo $result->num_rows;
while($row = $result->fetch_assoc()) {
$user = $row['user'];
$time = $row['time'];
$id = $row['id'];
echo $id;
echo $user;
}
} else {
echo "0 results";
}
$conn->close();
?>
However when I add in the SQL to check to see if they exist in the other table the loop no longer functions correctly and echos the same user each time.
<?php
$servername = "*******";
$username = "******";
$password = "********";
$dbname = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM payroll WHERE user != ' ' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo $result->num_rows;
while($row = $result->fetch_assoc()) {
$user = $row['user'];
$time = $row['time'];
$id = $row['id'];
echo $id;
echo $user;
// Added existing user check:
$sql = "SELECT * FROM smsreport WHERE user = '$user'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "found";
} else {
echo "USER NOT FOUND";
}
}
} else {
echo "0 results";
}
$conn->close();
?>
In the open eye:
Rename the inside $result variable. It is over writting the first $result.
It could be the problem. Not tested though.

PHP Log in to show details

The website has a login system, however when a user logs into the website I simply want their details to appear on the next page. This is my code I so far. Problem is, I only want to display the logged in users details, not all the databases details.
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "loginsystem";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM members";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
LOG IN SYSTEM
<?php
session_start();
if (isset($_POST['username'])) {
include_once("dbConnect.php");
// Set the posted data from the form into local variables
$usname = strip_tags($_POST['username']);
$paswd = strip_tags($_POST['password']);
$usname = mysqli_real_escape_string($dbCon, $usname);
$paswd = mysqli_real_escape_string($dbCon, $paswd);
$sql = "SELECT id, username, password FROM members WHERE username = '$usname' AND activated = '1' LIMIT 1";
$query = mysqli_query($dbCon, $sql);
$row = mysqli_fetch_row($query);
$uid = $row[0];
$dbUsname = $row[1];
$dbPassword = $row[2];
// Check if the username and the password they entered was correct
if ($usname == $dbUsname && password_verify($paswd,$dbPassword)) {
// Set session
$_SESSION['username'] = $usname;
$_SESSION['id'] = $uid;
// Now direct to users feed
header("Location: MemberDetails.php");
} else {
echo "Oops that username or password combination was incorrect.
<br /> Please try again.";
}
}
?>
Add
session_start();
to the top of the page and then on the next page as well and then you will be able to carry over those variables once they are set.
For example:
$_SESSION['user'] = $_POST['user'];
Then on the next page call:
echo $_SESSION['user'];
You first have to implement the user login part. and after that, get the specified user id or login credentials and use that in your query.
In your LOG IN SYSTEM file, put session_start(); before including the db connection.
Then in the member details page do this:
session_start(); //put this on the first line.
Then your query will now look like below:
<?php
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "loginsystem";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_id = $_SESSION['id'];
$sql = "SELECT user_id, firstname, lastname FROM members WHERE user_id = ".$user_id;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Database structure

Categories