How to display each submitted form data in another page - php

I want to display form data on another page so that when I click "More View" button and the inputed data of each user will display on another page.
This is the code of the HTML form:
registerbooks.php
<!DOCTYPE Html>
<<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Register books</title>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<body>
<div id='main-content'>
<form action="connect.php" method="POST">
<input type="text" name="tittle" placeholder="Book Tittle"></br>
<input type="text" name="author" placeholder="Author"></br>
<input type="text" name="copies" placeholder="Copies Available"></br>
<button type="submit" name="submit">submit</button>
<button type="reset" value="Reset">Reset</button>
</form>
</div>
</body>
</html>
This is the connection.php
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'list';
// Create connection
$connect = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['submit'])){
$Tittle = $_POST['tittle'];
$Author = $_POST['author'];
$Copies = $_POST['copies'];
$query = "INSERT INTO books(Tittle,Author,Copies) VALUES('$Tittle' , '$Author' , '$Copies')";
$result=mysqli_query($connect, $query);
if($result){
echo 'Available Books updated';
}
else{
echo "Failed to update";
}
}
?>
The fom is displayed in this page:
Availablebooks.php
<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Available books</title>
</head>
<body>
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'list';
$connect = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM books";
$result=mysqli_query($connect, $sql);
?>
<table align="centre" border="1px" width="600px" line-height="30px" >
<tr>
<th colspan="4">Available books</th>
</tr>
<tr>
<th>ID</th>
<th>Book tittle</th>
<th>Author</th>
<th>More Details</th>
</tr>;
<?php
while ($rows = $result->fetch_assoc())
{
?>
<tr>
<td><?php echo $rows['ID']; ?> </td>
<td> <?php echo $rows['Tittle']; ?> </td>
<td> <?php echo $rows['Author']; ?> </td>
<td> <button type="submit">More Views</button> </td>
</tr>
<?php
}
?>
</table>
</body>
</html>
When I click "more views" button, I want each user data to be displayed in "orders.php"
<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Available books</title>
</head>
<body>
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'list';
$connect = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM books WHERE id=(Tittle,Author,Copies )";
$result=mysqli_query($connect, $sql);
?>
<table align="left" border="1px" width="600px" line-height="30px" >
<tr>
<th colspan="4">Ordered books</th>
</tr>
<tr>
<th>ID</th>
<th>Book tittle</th>
<th>Author</th>
<th>Number of copies</th>
</tr>
<?php
while ($rows = $result->fetch_assoc())
{
?>
<tr>
<td><?php echo $rows['ID']; ?> </td>
<td><?php echo $rows['id']; ?> </td>
<td> <?php echo $rows['id']; ?> </td>
<td> <?php echo $rows['id']; ?> </td>
</tr>
<?php
}
?>
</table>
</body>
</html>
But the code is not working. Please help me to resolve this issue.
Thanks

Related

Delete hyperlink and function not working on web page

I tried to add a delete "button"(link to file with function) it should delete a row from the database, but it didn't work. I looked for tutorials and answers on forums but found nothing how solve for my problem.
<td>Delete</td>
link from code:
The link works correctly, but when I tried to delete it just doesn't want to take 'commentId' variable and go back to test.php page
Table on website:
dbh.inc.php
<?php
$serverName = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "php-login";
$conn = mysqli_connect($serverName, $dBUsername, $dBPassword, $dBName);
if (!$conn){
die("connection failed: " . mysqli_connect_error());
}
test.php
<?php
include_once 'header.php';
include "includes/dbh.inc.php";
include 'includes/test.inc.php';
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="box">
<h4 class="display-4 text-center">Comments</h4><br>
<?php if (isset($_GET['success'])) { ?>
<div class="alert alert-success" role="alert">
<?php echo $_GET['success']; ?>
</div>
<?php } ?>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Username</th>
<th scope="col">Comment</th>
<th scope="col">Action</th>
</tr>
</thead>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<td><?php echo $row["commentId"]; ?></td>
<td><?php echo $row["usersUid"]; ?></td>
<td><?php echo $row["comment"]; ?></td>
<td>Delete</td>
</tr>
<?php
$i++;
}
?>
</table>
</div>
</div>
</body>
</html>
test.inc.php
<?php
include "dbh.inc.php";
$sql = "SELECT * FROM commenttb ORDER BY commentId DESC";
$result = mysqli_query($conn, $sql);
delete.inc.php
<?php
include "dbh.inc.php";
if(isset($_GET['commentId'])) {
$id = $_GET['commentId'];
$delete = "DELETE FROM `commenttb` WHERE `commentId` ='$id'";
$result = mysqli_query($conn, $delete);
if ($result) {
header("Location: ../test.php?success=successfully deleted");
} else {
header("Location: ../test.php?error=unknown error occurred");
}
}else {
header("Location: ../test.php?error=smth gone wrong");
}
If I press on the link "delete" it should take 'commentId' variable from row e.g. 5 and by SQL query from delete.inc.php file delete row with this id from my database
I tried change $_Get to $_POST and add method="POST" to link on delete.inc.php file, but it didn't work

Why is the record I wanted to delete still there even though I had the function up and running?

I am working on an assignment in PHP and MySQL, where I am needed to create a function to delete records from a table via ID with a push of a button with JavaScript. Even though I followed by teacher's videos, it still is not able to delete the record away.
My teacher and I suspected that it has got to do with the bind_param part, but still it is not solved.
Here are the files:
db-connect.php
<?php
$servername = "localhost";
$username = "root";
$password = ""; // this should be empty
$dbName = "newDB"; // add your database name here
$conn = new mysqli($servername, $username, $password, $dbName);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
/**
* $conn->connect_error - contains an error message from the database server (if any)
*/
}
?>
index.php
<?php
require "db-connect.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Code-Along 2</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>List of Records</h1>
<div>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Email</th>
<th>Actions</th>
</tr>
<?php
$sql = "SELECT * FROM `Employee`;";
$sql_run = $conn->query($sql);
if($sql_run) { // if it is not false, then proceed
if($sql_run->num_rows > 0) { // num_rows will check if there are row(s) of results
while($row = $sql_run->fetch_assoc()) {
?>
<tr>
<td><?= $row['id']; ?></td>
<td><?= $row['name']; ?></td>
<td><?= $row['age']; ?></td>
<td><?= $row['email']; ?></td>
<td>
<button onclick="document.location.href = 'form.php?id=<?= $row['id']; ?>'">Edit</button>
<button onclick="deleteConfirm(<?= $row['id']; ?>);">Delete</button>
</td>
</tr>
<?php
}
} else {
// echo "No table rows found.";
?>
<tr>
<td colspan="5">No records found.</td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="5">Error retrieving table rows: <?= $conn->error; ?></td>
</tr>
<?php
}
?>
</table>
</div>
<script src="main.js"></script>
</body>
</html>
main.js
function deleteConfirm(id) {
const response = confirm(`Are you sure you want to delete record #${id}?`);
if(response) {
document.location.href = "db-deleterecord.php?=id" + id;
}
}
db-deleterecord.php
<?php
if(isset($_GET['id'])) { // check if "?id=..." exists, i.e. if a GET value id is obtained.
require "db-connect.php";
$sql = "DELETE FROM `Employee` WHERE `id` = ?;";
$stmt = $conn->prepare($sql);
if($stmt) {
$stmt->bind_param("i", $_GET['id']);
if($stmt->execute()) {
echo "Deleted record with ID: " .$_GET['id'];
} else echo "Unable to delete record #" . $_GET['id'] . ": " .$stmt->error;
} else echo "Unable to prepare statement: " . $conn->error;
}
header("refresh:5; url=index.php");
?>
There's an error in your string concatenation for the url -
document.location.href = "db-deleterecord.php?=id" + id;
should be -
document.location.href = "db-deleterecord.php?id=" + id;

Read out data and edit

i would like to read out all data records from a table and give individual data records a new value in "status" according to their "id".
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Table</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$conn = new mysqli ("localhost", "root", "Z&r*%/Nm#X4x", "cms");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, position, status FROM data";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach($data as $row) {
?>
<form method="post">
<div class="table-responsive d-table">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>POSITION</th>
<th>STATUS</th>
<th><button type="submit" name="change">CHANGE STATUS</button></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td>
<?php
if (isset($_POST['change'])) {
$update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
mysqli_query($conn, $update);
echo "Successful";
} elseif (!isset($_POST['change'])) {
echo "Not clicked";
} else {
echo "Failed";
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<?php
}
} else {
echo "No data found!";
$conn->close();
}
?>
</html>
My problem is, when I click the button, all records are changed and not just one. I hope someone can help to solve the problem.
.........................................................................................................................................................................................................
Try using {} to embed array elements into a string and enclose 'id' value
$update = "UPDATE data Set status = '0' WHERE id = {$row['id']}"
Otherwise, your query will interpret $row and [id] as separated items:
UPDATE data Set status = '0' WHERE id = [id]
and as a result, all your rows updated
Also consider to apply the status update button to each single row, there is no way to update only one row with a general "update status" button without sending a parameter of which row id will update. For example:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Table</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$conn = new mysqli ("localhost", "root", "Z&r*%/Nm#X4x", "cms");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, position, status FROM data";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach($data as $row) {
?>
<form method="post">
<div class="table-responsive d-table">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>POSITION</th>
<th>STATUS</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td>
<button type="submit" name="change" value="<?php echo $row['id']; ?>">CHANGE STATUS</button>
<?php
if (isset($_POST['change']) && $_POST["change"]==$row['id']) {
$update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
mysqli_query($conn, $update);
echo "Successful";
} elseif (!isset($_POST['change'])) {
echo "Not clicked";
} else {
echo "Failed";
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<?php
}
} else {
echo "No data found!";
$conn->close();
}
?>
</html>

index of fetch array

i want to get the index of td that the user clicked , i have an html table fill from database using php ...
this is my index.php :
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
</thead>
<tbody>
<tr>
<?php
session_start();
$connect = mysqli_connect("localhost","root","","test");
if (!$connect) {
die(mysql_error());
}
$results = mysqli_query($connect,"SELECT * FROM family where parent_id = 0");
while($row = mysqli_fetch_assoc($results)) {
?>
<td onclick="window.location='index2.php'"
<?php $id = $row['id'];
$_SESSION['varname'] = $id;?>>
<?php echo $row['name']?> <br/>
<?php echo $row['description']?> <br/>
<?php echo $row['parent_id']?> <br/>
</td>
<?php
}
?>
</tr>
</tbody>
</table>
</body>
</html>
this is my index2.php :
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
</thead>
<tbody>
<tr>
<?php
session_start();
$gg = $_SESSION['varname'];
echo $gg;
$connect = mysqli_connect("localhost","root", "","test");
if (!$connect) {
die(mysql_error());
}
$results = mysqli_query($connect,"SELECT * FROM family where parent_id = '$gg' ");
while($row = mysqli_fetch_array($results)) {
?>
<td>
<?php echo $row['id']?> <br/>
<?php echo $row['name']?> <br/>
<?php echo $row['description']?> <br/>
<?php echo $row['parent_id']?> <br/>
</td>
<?php
}
?>
</tr>
</tbody>
</table>
</body>
</html>
now i want to take the "id" of the td that the user click on,, but this code always give me the last id in my database ...
what can i do ?
Replace in Index.php:
<td onclick="window.location='index2.php'"
With:
<td onclick="window.location='index2.php?parent_id=<?php echo $row['id']; ?>'"
And in
Index2.php:
$gg = $_SESSION['varname'];
With:
$gg = (int)$_GET['parent_id'];
It's better to use $_GET variable for this than $_session (urls are search engine friendly)

Get specific data from database with PHP code when a user login

I want to get data from my database with PHP code. In brief when a user log in with id & password i want to show his data only in user panel, but i got some error: Here is my user admin panel code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>User Panel</title>
<link rel="stylesheet" href="assets/demo.css">
<link rel="stylesheet" href="assets/form-labels-on-top.css">
</head>
<body>
<header>
<h1 align="center"><b>User Information Details</b></h1></br>
</header>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "xyz";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
include('user_login_check.php');
$result= mysql_query("SELECT * FROM `user_information` WHERE `user_id` = '".$_SESSION['id']."' ")or die(mysql_error());
// $result = mysql_query("SELECT * FROM user_information WHERE user_name='" . $_POST["user_name"] . "' and user_password = '". $_POST["user_password"]."'");
?>
<form>
<table border="5" style= "background-color: #333333; color: #FFF; margin: 0 auto; padding:100px" >
<thead>
<tr>
<th>User ID</th>
<th>User Type</th>
<th>User Name</th>
<th>User Country</th>
<th>User Email</th>
<th>User Phone</th>
<td>User Address</td>
<td>User Password</td>
<td>User Status</td>
<td>Edit</td>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_assoc( $result ) ){
echo
"<tr>
<td>{$row['user_id']}</td>
<td>{$row['user_type']}</td>
<td>{$row['user_name']}</td>
<td>{$row['user_country']}</td>
<td>{$row['user_email']}</td>
<td>{$row['user_phone']}</td>
<td>{$row['user_address']}</td>
<td>{$row['user_password']}</td>
<td>{$row['user_status']}</td>
<td><img src='image/editicon.jpeg'/></td>;
</tr>\n";
}
?>
</tbody>
<div align="left" style="background:#333333">
<h1><input type="button" value="Log Out" onClick="window.location.href='logout_user.php'" /> </h1>
</div>
</table>
</form>
</body>
</html>
problem is in specially this section
$result= mysql_query("SELECT * FROM `user_information` WHERE `user_id` = '".$_SESSION['id']."' ")or die(mysql_error());
here is my login check code:
<?php
//connect the database
$hostName="localhost";
$dbUsername="root";
$dbPassword="";
$dbName="xyz";
mysql_connect($hostName,$dbUsername,$dbPassword) or die("Connection failed");
mysql_select_db($dbName) or die("Database name doesn't exist");
//start session
#session_start();
if(isset($_POST["user_name"] , $_POST["user_password"]))
{
$User_name = $_POST["user_name"];
// echo "$User_name";
$User_password = $_POST["user_password"];
// echo "$User_password";
$sql = "SELECT * FROM `user_information` WHERE `user_name`='".$User_name."' AND `user_password`='".$User_password."' ";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
// $result = mysql_query("SELECT * FROM `user_info` WHERE `User_name`='".$username."' AND `User_password``='".$password."'");
$my_arary = array();
while($row = mysql_fetch_assoc($result))
{
$_SESSION['id']= $row["Id"];
// echo $_SESSION['login_user_id'];
$_SESSION['user_password']= $row["user_password"];
//echo $_SESSION['login_user_password'];
$_SESSION['user_name']= $row["user_name"];
//echo $_SESSION['login_user_name'];
$my_arary[] = $row;
print_r($row);
if($User_name == $_SESSION['user_name'] && $User_password == $_SESSION['user_password']){
header("Location: userpanel.php");
//echo "successfully logged in";
}
else{
echo "no matches";
header("Location: user_login_basic.php");
}
}
}
?>
You set your $_SESSION['id']= $row["Id"] . Are you sure there column Id in your table? Because you using user_id instead of Id in your query in user admin panel code

Categories