Answer the question
In order to leave comments, you need to log in
How to implement multi-line data editing in PHP?
There is an editable list:
When editing, you need to implement an arbitrary number of positions, not just one:
How does this change the structure of the database and the connections of elements in it?
Edit line edit file code edit.php :
<?php session_start();?>
<?php
if (!isset($_SESSION['valid'])) {
header('Location: login.php');
}
?>
<?php
// including the database connection file
include_once "connection.php";
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$qty = $_POST['qty'];
$price = $_POST['price'];
// checking empty fields
if (empty($name) || empty($qty) || empty($price)) {
if (empty($name)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}
if (empty($qty)) {
echo "<font color='red'>Quantity field is empty.</font><br/>";
}
if (empty($price)) {
echo "<font color='red'>Price field is empty.</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($mysqli, "UPDATE products SET name='$name', qty='$qty', price='$price' WHERE id=$id");
//redirectig to the display page. In our case, it is view.php
header("Location: view.php");
}
}
?>
<?php
//getting id from url
$id = $_GET['id'];
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM products WHERE id=$id");
while ($res = mysqli_fetch_array($result)) {
$name = $res['name'];
$qty = $res['qty'];
$price = $res['price'];
}
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
<a href="index.php">Home</a> | <a href="view.php">View Products</a> | <a href="logout.php">Logout</a>
<br/><br/>
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $name; ?>"></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="qty" value="<?php echo $qty; ?>"></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price" value="<?php echo $price; ?>"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo $_GET['id']; ?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
create database test2;
use test2;
CREATE TABLE login
(
id int(9) NOT NULL
auto_increment,
name varchar
(100) NOT NULL,
email varchar
(100) NOT NULL,
username varchar
(100) NOT NULL,
password varchar
(100) NOT NULL,
PRIMARY KEY
(id)
) ENGINE=InnoDB;
CREATE TABLE products
(
id int(11) NOT NULL
auto_increment,
name varchar
(100) NOT NULL,
qty int
(5) NOT NULL,
price decimal
(10,2) NOT NULL,
login_id int
(11) NOT NULL,
PRIMARY KEY
(id),
CONSTRAINT FK_products_1
FOREIGN KEY
(login_id) REFERENCES login
(id)
ON
UPDATE CASCADE ON
DELETE CASCADE
) ENGINE=InnoDB;
Answer the question
In order to leave comments, you need to log in
The question is very broad. In short, here are a few points for you to think about:
1) if you need access to each individual line interactively, then look towards ajax requests.
2) the structure, be it ajax requests, let it be just a whole POST, the input will have name="[{id_elemnt}] price" , and on the post, you can process both a separate element and several enumerations.
3) add the attribute - data-id to tr to make it easier to navigate in the lines.
4) The maximum size of the POST request. study the question. since I definitely came across the task of solving such a flaw from the developers about 5 times.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question