Show number of new records on page - php

I want to display the exact number of new entries (records) on my page of my phpmyadmin database after reloading the page. I looked online but couldn't find anything usefull. Maybe someone can help me. This is the code for the page:
<?php
header("Refresh: 120");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="verzoekstyle.css">
<title>Verzoekpagina DJ</title>
</head>
<body id="body">
<div id="tabel">
<?php
$con = mysqli_connect('database','username','password');
if (!$con)
{
echo 'Geen verbinding met server';
}
if (!mysqli_select_db($con, 'eventqy179_verzoeken'))
{
echo 'Database niet geselecteerd';
}
$sql = "SELECT * FROM info";
$query = mysqli_query($con,$sql);
if(!$query)
{
echo 'Onbekende fout gevonden';
}
echo '
<table class="table">
<tr>
<th><h3><b>Artiest</b></h3></th>
<th><h3><b>Titel</b></h3></th>
<th><h3><b>Verwijderen</b></h3></th>
</tr>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td><h3>'.$row['artiest'].'</h3></td>';
echo '<td><h3>'.$row['titel'].'</h3></td>';
echo '<td><h3><a href=delete.php?id='.$row['id'].'>Verwijderen</a></h3></td>';
echo '</tr>';
}
echo '</table>';
?>
</div>
</body>
</html>

Already found another way, just ORDER BY id DESC. Question is therefor answered

Related

Deleting data from database using PHP

I am trying to delete data from MySQL using PHP
<?php
if (isset($_POST['delete'])) {
$queryDelete = "Delete FROM info WHERE userID={$_POST['delete']}";
if (!($database = mysqli_connect("localhost", "root", ""))) {
die("Could not connect to database. </body></html>");
}
if (!mysqli_select_db($database, "project2")) {
die("Could not open books database. </body></html>");
}
if (!(mysqli_query($database, $queryDelete))) {
echo "<p>Could not execute query!</p>";
die(mysqli_error($database) . "</body></html>");
}
mysqli_close($database);
}
this is my delete.php using it on this page
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Style.css">
</head>
<header>
<div>
<p id="page">Users List</p>
<img id="title_pic" src="images/title_pic.jpg" alt="#">
</div>
</header>
<body>
<?php include 'bar.php' ?>
<?php include 'delete.php' ?>
<br><br><br><br>
<h1 style="color:yellow;"> List of all Users: </h1>
<br>
<?php
$query = "SELECT userID, fName, email FROM info";
if (!($database = mysqli_connect("localhost", "root", ""))) {
die("Could not connect to database. </body></html>");
}
if (!mysqli_select_db($database, "project2")) {
die("Could not open project database. </body></html>");
}
if (!($result = mysqli_query($database, $query))) {
echo "<p>Could not execute query!</p>";
die(mysqli_error($database) . "</body></html>");
}
mysqli_close($database);
while ($row = mysqli_fetch_row($result)) {
foreach ($row as $value) {
echo "<span style='color:white;'> $value </span>";
}
echo ' <form action = "delete.php" method = "POST">';
echo '<input type="submit" name= "delete" value="delete" class="btn">';
echo '</form>';
echo "<br>";
}
?>
</html>
It's redirecting me to delete.php page but when I go back to the second one (Displayuser.php) all info are there and nothing is deleted
I used the same technique to add info but I am having trouble to delete them from the table.
Here is how your code should look like. First in your form, provide the ID of the user you want to delete. Make sure to enable mysqli error reporting and select the right database when connecting.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$database = mysqli_connect("localhost", "root", "", 'project2');
$database->set_charset('utf8mb4'); // always set the charset
$users = $database->query("SELECT userID, fName, email FROM info");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Style.css">
</head>
<body>
<header>
<div>
<p id="page">Users List</p>
<img id="title_pic" src="images/title_pic.jpg" alt="#">
</div>
</header>
<?php include 'bar.php' ?>
<?php include 'delete.php' ?>
<br><br><br><br>
<h1 style="color:yellow;"> List of all Users: </h1>
<br>
<?php
foreach ($users as $user) {
foreach ($user as $value) {
echo "<span style='color:white;'>'.htmlspecialchars($value).'</span>";
}
echo ' <form action = "delete.php" method = "POST">';
echo '<button type="submit" name="delete" value="'.htmlspecialchars($user['userID']).'" class="btn">Delete</button>';
echo '</form>';
echo "<br>";
}
?>
</html>
Then in your delete.php, read the POST value and delete the row with that ID using prepared statement.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$database = mysqli_connect("localhost", "root", "", 'project2');
$database->set_charset('utf8mb4'); // always set the charset
if (isset($_POST['delete'])) {
$stmt = $database->prepare("DELETE FROM info WHERE userID=?");
$stmt->bind_param('s', $_POST['delete']);
$stmt->execute();
}

product detail from DB unique

I'm trying to show the detail of my product from a database, but i only want to show the product I clicked using the button "afficher le detail" right now it shows all app at once.
Any tips would be appreciated because I'm really lost on this one.
i don't know if i need to modify the index, class.app or product page.
this is what i got on my index.php
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Applications</title>
</head>
<body>
<?php
include ("class.app.php");
$obj_app = new app();
include("class.client.php");
$obj_client = new client();
$btnajouterpanier=false;
if(isset($_SESSION['utilisateur'])){
if($obj_client->validate_login($_SESSION['utilisateur'],$_SESSION['motdepasse'])){
echo $obj_client->get_welcome_message($_SESSION['utilisateur']);
$btnajouterpanier=true;
}
}
if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER["CONTENT_TYPE"] === 'application/json'){
$data = json_decode(file_get_contents('php://input'),true);
$resultat = array('ajout' => FALSE);
$_SESSION['panier'][$_POST($data['id'])]+= 1;
$resultat['ajout'] = TRUE;
}
echo '<form name="frm_cat" action="index.php" method="POST">';
echo '<select name="lst_cat">';
$obj_app->get_cat_options();
echo '</select>';
echo '<input type="submit" name="btn_cat" value="Filtrer" />';
echo '</form>';
if(isset($_POST['lst_cat'])){
$obj_app->get_app_list($_POST['lst_cat'],$btnajouterpanier);
}else{
$obj_app->get_app_list('',$btnajouterpanier);
}
?>
<script src="jquery-3.2.1.min.js"></script>
<script src="panier.js"></script>
</body>
</html>
this is my class.app.php
<?php
class app{
private $dbh;
public function __construct(){
$this->dbh = new PDO('mysql:host=localhost;dbname=appstore','root','',array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ));
}
public function get_cat_options(){
$req = $this->dbh->prepare('SELECT * FROM cat ORDER BY titre');
$req->execute();
while($cat = $req->fetch()){
echo '<option value="'.$cat->id.'">'.$cat->titre.'</option>';
}
}
public function get_app_list($idcat="",$btnajouterpanier=false){
if ($idcat!=""){
$reqapp = $this->dbh->prepare("SELECT app.* FROM app JOIN appcat ON app.id=appcat.idapp WHERE appcat.idcat='".$idcat."'");
}else{
$reqapp = $this->dbh->prepare("SELECT app.* FROM app");
}
$reqapp->execute();
while($app = $reqapp->fetch()){
echo '<h3>'.$app->nom.'</h3><br/>';
echo '<img src="image/'.$app->image.'" /><br/>';
echo '<p>'.$app->description.'</p><br/>';
echo $app->prix.' $<br/>';
echo "<form name='btn_det' method='POST' action='produit.php'><input type='hidden' name='app_detail' value='".$app->id."' /><input type='submit' name='iddetail' value='Afficher le detail' data-id='".$app->id."'/></form>";
if($btnajouterpanier){
echo '<button type="button" class="btnajouterpanier" data-id="$app->id">Ajouter au panier</button>';
}
echo '<hr/>';
}
}
public function detail($iddetail){
$reqdet = $this->dbh->prepare('SELECT * FROM app where id = ?');
$reqdet->execute(array($iddetail));
while($app = $reqdet->fetch()){
echo '<h3>'.$app->nom.'</h3><br/>';
echo '<img src="image/'.$app->image.'" /><br/>';
echo '<p>'.$app->description.'</p><br/>';
echo $app->prix.' $<br/>';
}
}
}
?>
<script src="jquery-3.2.1.min.js"></script>
<script src="panier.js"></script>
and this is my produit.php page
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Document sans nom</title>
</head>
<body>
<?php
include ("class.app.php");
$obj_app = new app();
$obj_app->detail($_POST['iddetail'])
?>
</body>
</html>

How to fetch from MySQL database “Hindi” हिन्दी text (Indian local language)

Database which stores my data is this:
Now I want to fetch that data and display on my php page, but when I'm trying to fetch data in my php code I'm getting text into the following formate
UID= ????/??????????/????/?????/?????/Test upgrade/1
UID= ????/??????????/??????/??????/??????????/159/1
UID= ????/??????????/??????/??????/??????????/190/1
UID= ????/??????????/??????/??????/??????????/194/1
UID= ????/??????????/??????/???????/?????? (??.)/730/1
UID= ????/??????????/??????/???????/?????? (??.)/742/1/1
UID= ????/??????????/??????/???????/?????? (??.)/732/1
UID= ????/??????????/??????/??????/??????/98/8/1
UID= ????/??????????/??????/??????/??????/48/10/1
Referring to this question I have changed my database charset to "utf8_unicode_ci", but Still not working. I have written following code to fetch the data
datebase connection page
<?php
// Database configuration
$dbHost = "localhost";
$dbUsername = "user";
$dbPassword = "xxxxxxxxxxxxx";
$dbName = "tutorialsssxxxxx";
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
and index page
<?php
include $_SERVER['DOCUMENT_ROOT']."/header.php";
?><br>
<!DOCTYPE HTML>
<html lang="hi">
<head>
<title><?php echo $_GET['dta']; ?> Tutorials Mrtutorials.net</title>
<link href='style.css' rel='stylesheet' type='text/css'>
<script src="jquery.min.js"></script>
<script type="text/javascript">
// Show loading overlay when ajax request starts
$( document ).ajaxStart(function() {
$('.loading-overlay').show();
});
// Hide loading overlay when ajax request completes
$( document ).ajaxStop(function() {
$('.loading-overlay').hide();
});
</script>
</head>
<body>
<div class="content">
<div class="dta"> <div class="list_item"><h2><?php echo $_GET['dta']; ?> Tutorials</h2></div>
<div class="post-wrapper">
<div class="loading-overlay"><div class="overlay-content">Loading.....</div></div>
<div id="posts_content">
<?php
//Include pagination class file
include('Pagination.php');
//Include database configuration file
include('dbConfig.php');
$limit = 10;
//get number of rows
$queryNum = $db->query("SELECT COUNT(*) as postNum FROM posts");
$resultNum = $queryNum->fetch_assoc();
$rowCount = $resultNum['postNum'];
//initialize pagination class
$pagConfig = array('baseURL'=>'getData.php', 'totalRows'=>$rowCount, 'perPage'=>$limit, 'contentDiv'=>'posts_content');
$pagination = new Pagination($pagConfig);
//get rows
$query = $db->query("SELECT * FROM posts Where type=$yyy ORDER BY id DESC LIMIT $limit");
if($query->num_rows > 0){ ?>
<div class="posts_list">
<?php
while($row = $query->fetch_assoc()){
$postID = $row['id'];
?>
<table width="" border="0" cellspacing="5" cellpadding="0">
<tr class="up">
<td style="font-size: 45px; padding-left:5px; padding-right:5px"><?php echo $row["id"]; ?></td>
<td valign="left" width="100%"><?php echo $row["title"]; ?> <br> <?=$value['type']?></td>
</tr>
</table>
<?php } ?>
</div>
<?php echo $pagination->createLinks(); ?>
<?php } ?>
</div>
</div></div>
</div>
</body>
</html><?php
include $_SERVER['DOCUMENT_ROOT']."/footer.php";
?>
You need to use "set_charset"
Try this: (in your index.php)
//initialize pagination class
$pagConfig = array('baseURL'=>'getData.php', 'totalRows'=>$rowCount, 'perPage'=>$limit, 'contentDiv'=>'posts_content');
$pagination = new Pagination($pagConfig);
mysqli_set_charset( $db, 'utf8');
//get rows
$query = $db->query("SELECT * FROM posts Where type=$yyy ORDER BY id DESC LIMIT $limit");
To be precise, In your case you need to add this in code where you fetching the db:
mysqli_set_charset( $db, 'utf8');
mysqli_set_charset( $db, 'utf8'); will help you set unicode on the db connection. Now to show it in the page, you will still have to set the character encoding in html.
Remember to do this. Otherwise your page will still not show you the unicode characters.
<head>
<meta charset="UTF-8">
</head>
Nothing worked for me as per reply on stackoverflow.com, to display on web page, the Hindi Text from MySql through PHP.
The following code worked for me. Please write your comments
enter code here
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM audio WHERE AudioFor= 'Aarti' ORDER BY Name";
mysqli_set_charset( $conn, 'utf8'); **// to get the hindi font/text displayed**
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) { ?>
<table cellpadding="10">
<thead>
<tr align= "centre"><h3> Other Aartis </h3></tr>
<tr>
<th>Aarti Of</th>
<th>Wordings</th>
<th>Click to Listen</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td >" . $row["Name"]."</td><td >" . $row["Wording"]. "</td><td >". ' Click To Listen '. '</td></tr>';
}
} //else {
// echo "0 results";
//}
?>
After connecting to SQL Server and Database run the following query
msql_query("SET CHARACTER SET utf8")

Why i can see login form if im already login

Hi guys can i ask what is the problem on my code.
all i want is if there is someone already login then if i click back i don`t want to see the login form what is the problem in my code thank you for you help
//php for check if i login
<link rel="stylesheet" href="bootstrap.min.css" />
<?php
if (!isset($_SESSION["member_id"])) {
header("Location:index.php");
}
?>
//html
<?php
session_start();
require_once("check_login.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>All Members</title>
<link rel="stylesheet" href="bootstrap.min.css" />
<script>
function confirmDelete() {
if (!confirm("Are you sure you want to delete this member?")) {
return false;
}
}
</script>
</head>
<body>
<?php require_once("top_nav.php"); ?>
<div class="container">
<h1>All Members</h1>
<table class="table table-striped">
<th>ID</th>
<th>Email</th>
<th>Name</th>
<th></th>
<?php
require_once("db_open.php");
$sql = "SELECT * FROM members";
$result = $conn->query($sql) or die($conn->error);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$delete_link = "delete_member_db.php?member_id=".$row["member_id"];
echo "<tr>";
echo "<td>".$row["member_id"]."</td>";
echo "<td>".$row["member_email"]."</td>";
echo "<td>".$row["member_full_name"]."</td>";
echo "<td><a href='".$delete_link."' onClick='return confirmDelete();' class='btn btn-danger btn-xs'>Delete</a></td>";
echo "</tr>";
}
} else {
echo "<p>No members to show...</p>";
}
require_once("db_close.php");
?>
</table>
</div>
</body>
</html>
My apologies, I didn't get your question exactly but as per my understanding, in order to avoid login page view if user is logged in, you have to check with another condition on login page that,
if (isset($_SESSION["member_id"])) {
header("Location:some_page_name.php");
}
I think, You have checked condition
if (!isset($_SESSION["member_id"])) {
header("Location:index.php");
}
which is useful for pages visible after logging in...

PHP Ajax drop down box

<?php
require ("init.php");
?>
<head>
<script src="ajax.js"></script>
<script src="common.js"></script>
</head>
<body>
<?php
$query = "SELECT * FROM User1";
$result = mysqli_query($connection, $query);
echo '<form> ';
echo "Select a Users:";
echo '<select name="users" onchange="showUser(this.value)">';
while ($row=mysqli_fetch_assoc($result)){
echo $row=['Username'];
echo '<option value="'.$row=['Username'].'">'.$row=['Username'].'</option>';
}
echo '</select></form>';
?>
<div id="txtHint"><b>User info will be listed here.</b></div>
</body>
the problem i am having is that the drop down box only shows array multiple times and does not show usernames from my database however the connection to my database is working as when tryed debugging it got it to echo out just one username
IT should be like this:
while ($row=mysqli_fetch_assoc($result)){
//echo $row=['Username']; //Invalied here
echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';
}
No need that =sign.
Correct:
echo $row=['Username'];// This is unnecessary.
echo '<option value="'.$row=['Username'].'">'.$row=['Username'].'</option>';// what is = doing here?
To:
echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';

Categories