Wednesday, April 23, 2025

MySQLi Finishing Pure PHP CMS

update_all.html


<form action="update_all_script.php" method="post">
<label>ID</label><br>
<input type="text" name="id">
<br><br>

<label>Name</label><br>
<input type="text" name="name">
<br><br>

<label>Last Name</label><br>
<input type="text" name="lastname">
<br><br>

<label>Telephon</label><br>
<input type="text" name="telephon">
<br><br>

<label>email</label><br>
<input type="text" name="email">
<br><br>

<label>Address</label><br>
<input type="text" name="address">
<br><br>

<input type="submit" value="Submit">
</form>

This is an HTML form that allows a user to input data for updating a record in a MySQL database table. The form has fields for ID, name, last name, telephone, email, and address. When the form is submitted, the data is sent to a PHP script for processing.

The PHP script will receive the data submitted by the form via the $_POST superglobal variable. It will then establish a connection to the MySQL server using the given credentials and database name. The script will then construct an SQL command to update the record in the table using the ID specified in the form. The SQL command will update all columns in the table for the specified ID.

If the SQL command executes successfully, the script will display a message indicating success. If an error occurs during the execution of the SQL command, the script will display an error message containing the error description returned by MySQL.

update_all_script.php


<?php

$id = $_POST["id"];
$name = $_POST["name"];
$lastname = $_POST["lastname"];
$telephon = $_POST["telephon"];
$email = $_POST["email"];
$address = $_POST["address"];

//Config

$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";

//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);

//Check Connection
if (!$connection) {
	die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
	echo "Connection Successfull <br>";
}

//SQL Command
$sql_command = "UPDATE people SET 
name = '$name',
lastname = '$lastname',
telephon = '$telephon',
email = '$email',
address = '$address' WHERE id = '$id'";

if (mysqli_query($connection, $sql_command)) {
	echo "SQL Command OK";	
} else {
	echo "SQL Error " . mysqli_error($connection) ;
}

?>

This PHP script updates the information of a person in an address book stored in a MySQL database. It receives the new information through a form submitted with the POST method, and uses the values provided to update the fields of the corresponding person in the database.

The script connects to the MySQL server and checks the connection. If the connection is successful, it builds an SQL command to update the values of the person with the specified ID. If the SQL command is executed successfully, the script outputs "SQL Command OK". Otherwise, it outputs an error message with the reason for the failure.

When you run it with new data:


Connection Successfull
SQL Command OK

Always check entries with html_table_report.php

 

Navigation - general structure for php include or html hardcoding

<a href="html_table_report.php">Home - Report</a> |
<a href="html_form_to_table.html">New Post</a> |
<a href="delete_by_id.html">Delete by ID</a> |
<a href="update_all.html">Update by ID</a><br><br>

This is a navigation menu in HTML that includes links to different pages or actions related to an address book application. The links are:

  • Home - Report: this link goes to the "html_table_report.php" page, which likely displays a table with data from the address book.
  • New Post: this link goes to the "html_form_to_table.html" page, which likely displays a form to add a new entry to the address book.
  • Delete by ID: this link goes to the "delete_by_id.html" page, which likely displays a form to delete a specific entry from the address book by ID.
  • Update by ID: this link goes to the "update_all.html" page, which likely displays a form to update a specific entry from the address book by ID.

Report/Listing on every php or html page (remove part for connection if connection is already established) - it's same code as in html_table_report.php


<?php
//Config

$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";

//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);

//Check Connection
if (!$connection) {
	die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
	echo "Connection Successfull <br>";
}

//SQL Command
$sql_command = "SELECT * FROM people";
$action = mysqli_query($connection, $sql_command);

echo "<h1>Results: </h1>";
echo "<table width='80%' cellspan='2' border='2'><tr bgcolor='yellow'>";
echo "<td>ID</td>";
echo "<td>Name</td>";
echo "<td>Lastname</td>";
echo "<td>Telephon</td>";
echo "<td>email</td>";
echo "<td>Address</td>";
echo "<td>Data/Time</td></tr>";

while ($line = mysqli_fetch_assoc($action)) {
	echo "<tr><td>" . $line["id"] . "</td>
	<td>" . $line["name"] . "</td>
	<td>" . $line["lastname"] . "</td>
	<td>" . $line["telephon"] . "</td>
	<td>" . $line["email"] . "</td>
	<td>" . $line["address"] . "</td>
	<td>" . $line["meta"] . "</td>";
	
}
echo "</table>";

?>

This PHP script is used to display the results of a MySQL query on a webpage in the form of an HTML table.

It first sets up the connection to a MySQL server using the given configuration parameters. It then constructs a SELECT query that fetches all rows from the "people" table in the specified database.

After executing the query, it prints an HTML table with headers for each column in the table. It then loops through each row returned by the query and prints out the values for each column in that row. Each row is represented as an HTML table row, and each column is represented as an HTML table data cell.

Finally, it closes the HTML table and outputs it to the webpage.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...