<!doctype html>
<html lang="EN">
the <head>
<title>Admin panel</title>
</head>
the <body>
<?php
$link = mysqli_connect("localhost", "root", "", "practice"); // Connect to the database
// I swear, if the connection could not be established
if (!$link) {
echo 'cannot connect to the database. Error code: '. mysqli_connect_errno() . 'error:' . mysqli_connect_error();
exit;
}
//If the variable Name passed
if (isset($_POST["Username"])) {
//If it is update request, updating
if (isset($_GET['red_id'])) {
$sql = mysqli_query($link, "UPDATE `users` SET `username` = '{$_POST['username']}',`email` = '{$_POST['email']}',`password` = '{$_POST['password']}',`role` = '{$_POST['role']}' WHERE `id`={$_GET['red_id']}");
} else {
//Otherwise, insert the data, putting them in the query
$sql = mysqli_query($link, "INSERT INTO `users` (`username`, `email`, `password`, `role`) VALUES ('{$_POST['username']}', '{$_POST['email']}', '{$_POST['password']}', '{$_POST['role']}')");
}
//If the insert was successful
if ($sql) {
echo '<p>Successfully!</p>';
} else {
echo '<p>an error Occurred: '. mysqli_error($link) . '</p>';
}
}
if (isset($_GET['del_id'])) { //check whether the variable
//delete the row from the table
$sql = mysqli_query($link, "DELETE FROM `users` WHERE `ID` = {$_GET['del_id']}");
if ($sql) {
echo "<p>Item deleted.</p>";
} else {
echo '<p>an error Occurred: '. mysqli_error($link) . '</p>';
}
}
//If the passed variable red_id, it is necessary to update the data. To start we'll get them from the database
if (isset($_GET['red_id'])) {
$sql = mysqli_query($link, "SELECT `ID`, `username`, `email`, `password`, `role` FROM `users` WHERE `ID`={$_GET['red_id']}");
$user = mysqli_fetch_array($sql);
}
?>
<form action="" method="post">
<table>
the <tr>
<td>user Name:</td>
<td><input type="text" name="Username" value="<?= isset($_GET['red_id']) ? $user['Username'] : "; ?>"></td>
</tr>
the <tr>
<td>E-mail:</td>
<td><input type="text" name="Email" size="3" value="<?= isset($_GET['red_id']) ? $user['Email'] : "; ?>"></td>
</tr>
the <tr>
<td>Password:</td>
<td><input type="text" name="Password" size="3" value="<?= isset($_GET['red_id']) ? $user['Password'] : "; ?>"></td>
</tr>
the <tr>
<td>Role:</td>
<td><input type="text" name="Role" size="3" value="<?= isset($_GET['red_id']) ? $user['Role'] : "; ?>"></td>
</tr>
the <tr>
<td colspan="2"><input type="submit" value="OK"></td>
</tr>
</table>
</form>
<table border='1'>
the <tr>
<td>ID</td>
<td>user Name</td>
<td>E-mail</td>
<td>Password</td>
<td>Role</td>
<td>Delete</td>
<td>Change</td>
</tr>
<?php
$sql = mysqli_query($link, 'SELECT `ID`, `Username`, `Email`, `Password`, `Role` FROM `users`);
while ($result = mysqli_fetch_array($sql)) {
echo '<tr>' .
"<td>{$result['ID']}</td>" .
"<td>{$result['Username']}</td>" .
"<td>{$result['Email']}</td>" .
"<td>{$result['Password']}</td>" .
"<td>{$result['Role']}</td>" .
"<td><a href='?del_id={$result['ID']}'>Delete</a></td>" .
"<td><a href='?red_id={$result['ID']}'>Edit</a></td>" .
'</tr>';
}
?>
</table>
<p><a href="?add=new">Add new product</a></p>
</body>
</html>
<code>
Find more questions by tags PHP
where this will take? - Lucile8 commented on April 19th 20 at 12:21
`Role` FROM `users`
- karli.Bruen commented on April 19th 20 at 12:27- karli.Bruen commented on April 19th 20 at 12:36
Usually it appeal to more Soobrazitelny classmates who are not averse to earn extra money - Lucile8 commented on April 19th 20 at 13:06
What specific log you mean? - Lucile8 commented on April 19th 20 at 13:09
I never wrote that you have this code did not work.
I wrote you word it does not understand.
And in this case it is necessary to seek paid help to fellow students - Lucile8 commented on April 19th 20 at 13:15
can beg more - maybe someone will take pity and poke your nose - Lucile8 commented on April 19th 20 at 13:24
Or the second option, prepared queries:
And now about the second part of the question: what isn't working? What "here is not what"? Are you talking about? - Merl commented on April 19th 20 at 13:27
Here is an example. Changed the user under the ID 5 in the end, all fields are cleared except the ID:
if (isset($_POST["Username"]))
..
SET `username` = '{$_POST['username']}' - Keaton commented on April 19th 20 at 13:36
- ova_Connel commented on April 19th 20 at 13:45
- Merl commented on April 19th 20 at 13:48