4E e 5E SIA: Inserisci, visualizza, modifica ed elimina record dal database utilizzando PHP e MySQLi con file zippato della procedura

 Inserisci, visualizza, modifica ed elimina record dal database utilizzando PHP e MySQLi e file zippato



Link originale: https://www.allphptricks.com/insert-view-edit-and-delete-record-from-database-using-php-and-mysqli/

In questo tutorial, spiegherò come inserire, visualizzare, modificare ed eliminare record dal database utilizzando PHP e MySQLi, in pratica questo tutorial è una seconda parte di  Simple User Registration & Login Script in PHP e MySQLi , in questa prima parte ho spiegato come per creare una semplice registrazione utente e accedere utilizzando PHP e MySQLi, se non sai come creare la registrazione utente e il modulo di accesso utente, quindi prima controlla questo tutorial Registrazione utente  semplice e script di accesso in PHP e MySQLi , dopodiché ora torna a questo tutorial.

Sto usando PHP 5.6 per questo tutorial, non ho controllato questo tutorial su PHP 7, assicurati gentilmente di utilizzare PHP 5.6 per evitare errori imprevisti.

Ti suggerirei di controllare PHP CRUD Operations Using PDO Prepared Statements , poiché è compatibile con PHP 7 e 8 entrambi.

Passaggi per creare un record di inserimento, visualizzazione, modifica ed eliminazione dal database utilizzando PHP e MySQLi

Ora presumo che tu abbia già creato la registrazione dell'utente e i moduli di accesso che ho creato in  Simple User Registration & Login Script in PHP e MySQLi  ora creerò un'altra tabella per conservare i record, aggiornerò il file dashboard.php e aggiungerò altri quattro nuovi pagine che sono insert.php, view.php, edit.php e delete.php , procedi nel seguente modo:

1.      Crea un'altra tabella per i record

2.      Aggiorna file dashboard

3.      Crea pagina di inserimento

4.      Crea Visualizza pagina

5.      Crea pagina Modifica/Aggiorna

6.      Crea Elimina pagina

1. Creare un'altra tabella per i record

Esegui la seguente query SQL:

CREATE TABLE IF NOT EXISTS `new_record` (

 `id` int(11) NOT NULL AUTO_INCREMENT,

 `trn_date` datetime NOT NULL,

 `name` varchar(50) NOT NULL,

 `age`int(11) NOT NULL,

 `submittedby` varchar(50) NOT NULL,

 PRIMARY KEY (`id`)

 );

2. Aggiorna la pagina del dashboard

Aggiorna  il file dashboard.php e incolla il seguente codice al suo interno.

<?php

require('db.php');

include("auth.php");

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Dashboard - Secured Page</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<div class="form">

<p>Welcome to Dashboard.</p>

<p><a href="index.php">Home</a><p>

<p><a href="insert.php">Insert New Record</a></p>

<p><a href="view.php">View Records</a><p>

<p><a href="logout.php">Logout</a></p>

</div>

</body>

</html>

3. Creare una pagina di inserimento

Crea una pagina con il nome insert.php e incolla il codice qui sotto.

<?php

require('db.php');

include("auth.php");

$status = "";

if(isset($_POST['new']) && $_POST['new']==1){

    $trn_date = date("Y-m-d H:i:s");

    $name =$_REQUEST['name'];

    $age = $_REQUEST['age'];

    $submittedby = $_SESSION["username"];

    $ins_query="insert into new_record

    (`trn_date`,`name`,`age`,`submittedby`)values

    ('$trn_date','$name','$age','$submittedby')";

    mysqli_query($con,$ins_query)

    or die(mysql_error());

    $status = "New Record Inserted Successfully.

    </br></br><a href='view.php'>View Inserted Record</a>";

}

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Insert New Record</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<div class="form">

<p><a href="dashboard.php">Dashboard</a>

| <a href="view.php">View Records</a>

| <a href="logout.php">Logout</a></p>

<div>

<h1>Insert New Record</h1>

<form name="form" method="post" action="">

<input type="hidden" name="new" value="1" />

<p><input type="text" name="name" placeholder="Enter Name" required /></p>

<p><input type="text" name="age" placeholder="Enter Age" required /></p>

<p><input name="submit" type="submit" value="Submit" /></p>

</form>

<p style="color:#FF0000;"><?php echo $status; ?></p>

</div>

</div>

</body>

</html>




4. Crea Visualizza pagina

Crea una pagina con il nome view.php e incolla il codice qui sotto.

<?php

require('db.php');

include("auth.php");

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>View Records</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<div class="form">

<p><a href="index.php">Home</a>

| <a href="insert.php">Insert New Record</a>

| <a href="logout.php">Logout</a></p>

<h2>View Records</h2>

<table width="100%" border="1" style="border-collapse:collapse;">

<thead>

<tr>

<th><strong>S.No</strong></th>

<th><strong>Name</strong></th>

<th><strong>Age</strong></th>

<th><strong>Edit</strong></th>

<th><strong>Delete</strong></th>

</tr>

</thead>

<tbody>

<?php

$count=1;

$sel_query="Select * from new_record ORDER BY id desc;";

$result = mysqli_query($con,$sel_query);

while($row = mysqli_fetch_assoc($result)) { ?>

<tr><td align="center"><?php echo $count; ?></td>

<td align="center"><?php echo $row["name"]; ?></td>

<td align="center"><?php echo $row["age"]; ?></td>

<td align="center">

<a href="edit.php?id=<?php echo $row["id"]; ?>">Edit</a>

</td>

<td align="center">

<a href="delete.php?id=<?php echo $row["id"]; ?>">Delete</a>

</td>

</tr>

<?php $count++; } ?>

</tbody>

</table>

</div>

</body>

</html>



5. Crea pagina Modifica/Aggiorna

Crea una pagina con il nome edit.php e incolla il codice qui sotto.

<?php

require('db.php');

include("auth.php");

$id=$_REQUEST['id'];

$query = "SELECT * from new_record where id='".$id."'";

$result = mysqli_query($con, $query) or die ( mysqli_error());

$row = mysqli_fetch_assoc($result);

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Update Record</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<div class="form">

<p><a href="dashboard.php">Dashboard</a>

| <a href="insert.php">Insert New Record</a>

| <a href="logout.php">Logout</a></p>

<h1>Update Record</h1>

<?php

$status = "";

if(isset($_POST['new']) && $_POST['new']==1)

{

$id=$_REQUEST['id'];

$trn_date = date("Y-m-d H:i:s");

$name =$_REQUEST['name'];

$age =$_REQUEST['age'];

$submittedby = $_SESSION["username"];

$update="update new_record set trn_date='".$trn_date."',

name='".$name."', age='".$age."',

submittedby='".$submittedby."' where id='".$id."'";

mysqli_query($con, $update) or die(mysqli_error());

$status = "Record Updated Successfully. </br></br>

<a href='view.php'>View Updated Record</a>";

echo '<p style="color:#FF0000;">'.$status.'</p>';

}else {

?>

<div>

<form name="form" method="post" action="">

<input type="hidden" name="new" value="1" />

<input name="id" type="hidden" value="<?php echo $row['id'];?>" />

<p><input type="text" name="name" placeholder="Enter Name"

required value="<?php echo $row['name'];?>" /></p>

<p><input type="text" name="age" placeholder="Enter Age"

required value="<?php echo $row['age'];?>" /></p>

<p><input name="submit" type="submit" value="Update" /></p>

</form>

<?php } ?>

</div>

</div>

</body>

</html>



6. Crea Elimina pagina

Crea una pagina con il nome delete.php e incolla il codice qui sotto.

<?php

require('db.php');

$id=$_REQUEST['id'];

$query = "DELETE FROM new_record WHERE id=$id";

$result = mysqli_query($con,$query) or die ( mysqli_error());

header("Location: view.php");

?>

SCARICA DEMO

 

Commenti

Post popolari in questo blog

Simulazioni di reti (con Cisco Packet Tracer)

Esercizi sulla rappresentazione della virgola mobile IEEE 754 (Floating Point)