How to connect our Website to Database Using PHP?
<?php
$url='localhost';
$username='root';
$password='';
$conn=mysqli_connect($url,$username,$password,"webprogrammingdb");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
?>
<?php
if(isset($_POST['delete'])) {
//Open connection
$conn= mysqli_connect("localhost","root","","webprogrammingdb") or die ("could not connect to mysql");
$id = $_POST['id'];
//Attempt delete query execution
$sql = "DELETE FROM login_user WHERE id = $id" ;
//Execute query
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
// Close connection
mysqli_close($conn);
}else {
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border = "0" cellspacing = "1" cellpadding = "2">
<tr>
<td width = "100">ID</td>
<td><input name = "id" type = "text" id = "id"></td>
<td><input name = "delete" type = "submit" id = "delete" value = "Delete"></td>
</tr>
</table>
</form>
<?php
}
?>
<?php
//Open connection
$con= mysqli_connect("localhost","root","","webprogrammingdb") or die ("could not connect to mysql");
//Escape user inputs for security
$name = mysqli_real_escape_string($con, $_REQUEST['name']);
$user_name = mysqli_real_escape_string($con, $_REQUEST['user_name']);
$password = mysqli_real_escape_string($con, $_REQUEST['password']);
//Attempt insert query execution
$sql = "INSERT INTO login_user (name, user_name, password) VALUES ('$name', '$user_name', '$password')";
//Execute query
if(mysqli_query($con, $sql)){
echo "Records added successfully. Sign in <a href='login.php'>here</a>";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}
// Close connection
mysqli_close($con);
?>
How to create functioning registration form and the information stored in database?
<html>
<head>
<title>User Login</title>
</head>
<body>
<form name="frmUser" method="post" action="insert.php" align="center">
<h3 align="center">Enter Sigup Details Details</h3>
<br>
Name:<br>
<input type="name" name="name">
<br>
Username:<br>
<input type="text" name="user_name">
<br>
Password:<br>
<input type="password" name="password">
<br><br>
<input type="submit" name="submit" value="Submit">
<input type="reset">
<br>
</form>
</body>
</html>
How to view our database from our own website?
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<?php
//Open connection
$conn= mysqli_connect("localhost","root","","webprogrammingdb") or die ("could not connect to mysql");
//Attempt select query execution
$sql = "SELECT * FROM login_user";
$result = $conn->query($sql);
//Execute query
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Usernama</th><th>Password</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["user_name"]. "</td>
<td>" . $row["password"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
// Close connection
$conn->close();
?>
<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM login_user");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<title>Delete data</title>
</head>
<body>
<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Username</td>
<td>Password</td>
<td>Action</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
$classname="even";
else
$classname="odd";
?>
<tr class="<?php if(isset($classname)) echo $classname;?>">
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["user_name"]; ?></td>
<td><?php echo $row["password"]; ?></td>
<td><a href="update-process.php?id=<?php echo $row["id"]; ?>">Update</a></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>
//login
<?php
//Create session
session_start();
$message="";
//Open connection
if(count($_POST)>0) {
$con = mysqli_connect('localhost','root','','bjbs') or die('Unable To connect');
//Attempt select query execution
$result = mysqli_query($con,"SELECT * FROM registeracc WHERE user_name='" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");
$row = mysqli_fetch_array($result);
//Execute query
if(is_array($row)) {
$_SESSION["id"] = $row['id'];
$_SESSION["name"] = $row['name'];
} else {
$message = "Invalid Username or Password!";
}
}
if(isset($_SESSION["id"])) {
header("Location:index.php");
}
?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<form name="frmUser" method="post" action="" align="center">
<div class="message"><?php if($message!="") { echo $message; } ?></div>
<h3 align="center">Enter Login Details</h3>
Username:<br>
<input type="text" name="user_name">
<br>
Password:<br>
<input type="password" name="password">
<br><br>
<input type="submit" name="submit" value="Submit">
<input type="reset">
<br>
Not a member yet? Sign up <a href="registration_form.php">here</a>
</form>
</body>
</html>
//logout
<?php
session_start();
unset($_SESSION["id"]);
unset($_SESSION["name"]);
header("Location:login.php");
?>
0 Comments