3 回答
TA贡献1833条经验 获得超4个赞
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$firstname = $lastname = $email = $address = $status = $level = "";
$firstname_err = $lastname_err = $email_err = $address_err = $level_err = $status_err = "";
// Processing form data when form is submitted
if(isset($_POST["id"])){
// Get hidden input value
$id = $_POST["id"];
// Validate name-first
$input_firstname = trim($_POST["firstname"]);
if(empty($input_firstname)){
$firstname_err = "Please enter a first name.";
} elseif(!filter_var($input_firstname, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$firstname_err = "Please enter a valid first name.";
} else{
$firstname = $input_firstname;
}
//Validate lastname
$input_lastname = trim($_POST["lastname"]);
if(empty($input_lastname)){
$lastname_err = "Please enter a last name.";
} elseif(!filter_var($input_lastname, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$lastname_err = "Please enter a valid last name.";
} else{
$lastname = $input_firstname;
}
//Validate email address
$input_email = trim($_POST["email"]);
if(empty($input_email)){
$email_err = "Please enter an email address.";
} else{
$email = $input_email;
}
// Validate address
$input_address = trim($_POST["address"]);
if(empty($input_address)){
$address_err = "Please enter an address.";
} else{
$address = $input_address;
}
// Validate access level
$input_level = trim($_POST["level"]);
if(empty($input_level)){
$level_err = "Please enter an level.";
} elseif($input_level == 'Admin'){
$level = $input_level;
}elseif($input_level == 'Customer'){
$level = $input_level;
}else{
$level_err = 'Please choose from Admin or Customer';
}
// Validate status
$input_status = trim($_POST["status"]);
if(empty($input_status)){
$status_err = "Please enter a status";
}elseif($input_status == '1'){
$status = $input_status;
}elseif($input_status == '0'){
$status = $input_status;
}else{
$status_err = "Please enter a valid status";
}
echo "<script>alert('";
echo "Checking all inputs vales---";
echo $id; echo "=id--<br>";
echo $input_firstname; echo "=fname---<br>";
echo $input_lastname; echo "=lname---<br>";
echo $input_email; echo "=email---<br>";
echo $input_address; echo "=add---<br>";
echo $input_level; echo "=level<br>";
echo $input_status; echo "=stat<br>";
echo "')</script> ";
// Check input errors before inserting in database
if(empty($firstname_err) && empty($lastname_err) && empty($email_err) && empty($address_err) && empty($level_err) && empty($status_err)){
// Prepare an update statement
echo "<script>alert('";
echo "Im inside `Check Inputs before inserting in database` If Block... All Good so far"; echo "<br>";
echo "')</script> ";
$sql = "UPDATE users SET firstname=?, lastname=? address=?, email=?, access_level=?, status=? WHERE id=?";
if($stmt = mysqli_prepare($link, $sql)){
echo "<script>alert('";
echo "Im inside `mysqli_prepare` If Block... All Good so far"; echo "<br>";
echo "')</script> ";
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sssssii", $param_firstname, $param_lastname, $param_address, $param_email, $param_level, $param_status, $param_id);
// Set parameters
$param_firstname = $firstname;
$param_lastname = $lastname;
$param_address = $address;
$param_email = $email;
$param_level = $level;
$param_status = $status;
$param_id = $id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
echo "<script>alert('";
echo "Im inside `mysqli_stmt_execute` If Block... All Good so far"; echo "<br>";
echo "')</script> ";
header("location: index.php");
exit();
} else{
echo "<script>alert('";
echo "Im inside `mysqli_stmt_execute` If Block... Not Good"; echo "<br>";
echo "')</script> ";
echo "Something went wrong. Please try again later.";
}
// Close statement
mysqli_stmt_close($stmt);
}else{
echo "<script>alert('";
echo "Im directly into else block from `empty input check If` Block... Not Good"; echo "<br>";
echo "')</script> ";
echo mysqli_stmt_error($stmt);
}
}
// Close connection
mysqli_close($link);
} else{
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
// Get URL parameter
$id = trim($_GET["id"]);
// Prepare a select statement
$sql = "SELECT * FROM users WHERE id = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_id);
// Set parameters
$param_id = $id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 1){
/* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$firstname = $row["firstname"];
$lastname = $row["lastname"];
$email = $row["email"];
$address = $row["address"];
$level = $row["access_level"];
$status = $row["status"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: error.php");
exit();
}
}
?>
<?php
// core configuration
include_once "../../config/core.php";
// check if logged in as admin
include_once "../login_checker.php";
// set page title
$page_title = "Update Record";
// include page header HTML
include '../layout_head.php';
echo "<div class='col-md-12'>";
// get parameter values, and to prevent undefined index notice
$action = isset($_GET['action']) ? $_GET['action'] : "";
// tell the user he's already logged in
if ($action == 'already_logged_in') {
echo "<div class='alert alert-info'>";
echo "<strong>You</strong> are already logged in.";
echo "</div>";
} else if ($action == 'logged_in_as_admin') {
echo "<div class='alert alert-info'>";
echo "<strong>You</strong> are logged in as admin.";
echo "</div>";
}
echo "</div>";
//content when logged in
?>
<div class="wrapper" style="width: 500px; margin: 0 auto;">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Update Record</h2>
</div>
<p>Please edit the input values and submit to update the record.</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($firstname_err)) ? 'has-error' : ''; ?>">
<label>First Name</label>
<input type="text" name="firstname" class="form-control" value="<?php echo $firstname; ?>">
<span class="help-block"><?php echo $firstname_err;?></span>
</div>
<div class="form-group <?php echo (!empty($lastname_err)) ? 'has-error' : ''; ?>">
<label>Last Name</label>
<input type="text" name="lastname" class="form-control" value="<?php echo $lastname; ?>">
<span class="help-block"><?php echo $lastname_err;?></span>
</div>
<div class="form-group <?php echo (!empty($address_err)) ? 'has-error' : ''; ?>">
<label>Email</label>
<input type="email" name="email" class="form-control" value="<?php echo $email; ?>" />
<span class="help-block"><?php echo $email_err;?></span>
</div>
<div class="form-group <?php echo (!empty($address_err)) ? 'has-error' : ''; ?>">
<label>Address</label>
<textarea name="address" class="form-control"><?php echo $address; ?></textarea>
<span class="help-block"><?php echo $address_err;?></span>
</div>
<div class="form-group <?php echo (!empty($level_err)) ? 'has-error' : ''; ?>">
<label>Access Level</label>
<input type="text" name="level" class="form-control" value="<?php echo $level; ?>">
<span class="help-block"><?php echo $level_err;?></span>
</div>
<div class="form-group <?php echo (!empty($status_err)) ? 'has-error' : ''; ?>">
<label>Status</label>
<input type="text" name="status" class="form-control" value="<?php echo $status; ?>">
<span class="help-block"><?php echo $status_err;?></span>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
我更新了代码,请运行该代码。我无法猜测还有什么可能是错误的,但我已经在您的代码中添加了一些回显。请运行一下看看哪里出错了。仔细检查您的bind_parameter类型。它们必须与您的数据库表类型完全相同。我只是帮助你给你一个例子而不是整个解决方案。
现在您可以看到警报显示您在哪里,以便您可以追踪问题
希望它能帮助并解决您的问题。
TA贡献1854条经验 获得超8个赞
// pagination
public class PaginationList<T> : List<T>
{
public PaginationList(List<T> values,int count , int page , int pageSize)
{
AddRange(values);
TotalPage = (int)Math.Ceiling(count / (double)pageSize);
Page= page;
}
public int TotalPage { get; set; }
public int Page { get; set; }
public bool HasPreviuos { get => Page > 1; }
public bool HasNextiuos { get => Page < TotalPage; }
public static PaginationList<T> Craete(IQueryable<T> query , int page , int pageSize)
{
return new PaginationList<T>(query.Skip((page - 1) * pageSize).Take(pageSize).ToList(), query.Count(), page, pageSize);
}
}
// javascript
let itemdelete = document.querySelectorAll(".item-delete");
itemdelete.forEach(btn => btn.addEventListener("click", function (e) {
e.preventDefault();
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
let url = btn.getAttribute("href");
fetch(url)
.then(response => {
if (response.status == 200) {
window.location.reload(true);
} else {
alert("errorrr");
}
})
}
})
}))
//account
using Anyar.Models;
using Anyar.ViewModels;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace Anyar.Controllers
{
public class AccountController : Controller
{
private readonly UserManager<AppUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly SignInManager<AppUser> _signInManager;
public AccountController(UserManager<AppUser> userManager, RoleManager<IdentityRole> roleManager, SignInManager<AppUser> signInManager)
{
_userManager = userManager;
_roleManager = roleManager;
_signInManager = signInManager;
}
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> CreateAdmin()
{
AppUser user = new AppUser
{
Fullname = "Kamil Qurbanov",
UserName = "SuperAdminKamil"
};
var result = await _userManager.CreateAsync(user, "Kamil000");
return Ok(result);
}
public async Task<IActionResult> CreateRole()
{
IdentityRole role = new IdentityRole("SuperAdmin");
IdentityRole role2 = new IdentityRole("Admin");
IdentityRole role3 = new IdentityRole("Member");
await _roleManager.CreateAsync(role);
await _roleManager.CreateAsync(role2);
await _roleManager.CreateAsync(role3);
return Ok("added");
}
public async Task<IActionResult> AddRole()
{
AppUser user = await _userManager.FindByNameAsync("SuperAdminKamil");
await _userManager.AddToRoleAsync(user, "SuperAdmin");
return Ok("added");
}
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel adminLogin)
{
if (!ModelState.IsValid) { return View(); }
AppUser appUser = await _userManager.FindByNameAsync(adminLogin.Username);
if (appUser == null)
{
ModelState.AddModelError("", "name or password is invalid");
return View();
}
var result = await _signInManager.PasswordSignInAsync(appUser, adminLogin.Password, false, false);
if (!result.Succeeded)
{
ModelState.AddModelError("", "name or password is invalid");
return View();
}
return RedirectToAction("index","home");
}
public async Task<IActionResult> Logout()
{
if (User.Identity.IsAuthenticated)
{
await _signInManager.SignOutAsync();
}
return RedirectToAction("index", "home");
}
public IActionResult Register()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel register)
{
if (!ModelState.IsValid) { return View(); }
AppUser user = await _userManager.FindByNameAsync(register.Username);
if (user != null)
{
ModelState.AddModelError("username", "bele nir user name sitifade olunub");
return View();
}
user = await _userManager.FindByEmailAsync(register.Email);
if (user != null)
{
ModelState.AddModelError("email", "bele nir user email sitifade olunub");
return View();
}
user = new AppUser
{
UserName = register.Username,
Email = register.Email,
Fullname = register.Fullname,
};
var result = await _userManager.CreateAsync(user, register.Password);
if (!result.Succeeded)
{
foreach (var err in result.Errors)
{
ModelState.AddModelError("", err.Description);
return View();
}
}
var roleresult = await _userManager.AddToRoleAsync(user, "Member");
if (!roleresult.Succeeded)
{
foreach (var err in roleresult.Errors)
{
ModelState.AddModelError("", err.Description);
return View();
}
}
return RedirectToAction("login", "account");
}
}
}
TA贡献1884条经验 获得超4个赞
// javascript
let itemdelete = document.querySelectorAll(".item-delete");
itemdelete.forEach(btn => btn.addEventListener("click", function (e) {
e.preventDefault();
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
let url = btn.getAttribute("href");
fetch(url)
.then(response => {
if (response.status == 200) {
window.location.reload(true);
} else {
alert("errorrr");
}
})
}
})
}))
//account
using Anyar.Models;
using Anyar.ViewModels;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace Anyar.Controllers
{
public class AccountController : Controller
{
private readonly UserManager<AppUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly SignInManager<AppUser> _signInManager;
public AccountController(UserManager<AppUser> userManager, RoleManager<IdentityRole> roleManager, SignInManager<AppUser> signInManager)
{
_userManager = userManager;
_roleManager = roleManager;
_signInManager = signInManager;
}
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> CreateAdmin()
{
AppUser user = new AppUser
{
Fullname = "Kamil Qurbanov",
UserName = "SuperAdminKamil"
};
var result = await _userManager.CreateAsync(user, "Kamil000");
return Ok(result);
}
public async Task<IActionResult> CreateRole()
{
IdentityRole role = new IdentityRole("SuperAdmin");
IdentityRole role2 = new IdentityRole("Admin");
IdentityRole role3 = new IdentityRole("Member");
await _roleManager.CreateAsync(role);
await _roleManager.CreateAsync(role2);
await _roleManager.CreateAsync(role3);
return Ok("added");
}
public async Task<IActionResult> AddRole()
{
AppUser user = await _userManager.FindByNameAsync("SuperAdminKamil");
await _userManager.AddToRoleAsync(user, "SuperAdmin");
return Ok("added");
}
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel adminLogin)
{
if (!ModelState.IsValid) { return View(); }
AppUser appUser = await _userManager.FindByNameAsync(adminLogin.Username);
if (appUser == null)
{
ModelState.AddModelError("", "name or password is invalid");
return View();
}
var result = await _signInManager.PasswordSignInAsync(appUser, adminLogin.Password, false, false);
if (!result.Succeeded)
{
ModelState.AddModelError("", "name or password is invalid");
return View();
}
return RedirectToAction("index","home");
}
public async Task<IActionResult> Logout()
{
if (User.Identity.IsAuthenticated)
{
await _signInManager.SignOutAsync();
}
return RedirectToAction("index", "home");
}
public IActionResult Register()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel register)
{
if (!ModelState.IsValid) { return View(); }
AppUser user = await _userManager.FindByNameAsync(register.Username);
if (user != null)
{
ModelState.AddModelError("username", "bele nir user name sitifade olunub");
return View();
}
user = await _userManager.FindByEmailAsync(register.Email);
if (user != null)
{
ModelState.AddModelError("email", "bele nir user email sitifade olunub");
return View();
}
user = new AppUser
{
UserName = register.Username,
Email = register.Email,
Fullname = register.Fullname,
};
var result = await _userManager.CreateAsync(user, register.Password);
if (!result.Succeeded)
{
foreach (var err in result.Errors)
{
ModelState.AddModelError("", err.Description);
return View();
}
}
var roleresult = await _userManager.AddToRoleAsync(user, "Member");
if (!roleresult.Succeeded)
{
foreach (var err in roleresult.Errors)
{
ModelState.AddModelError("", err.Description);
return View();
}
}
return RedirectToAction("login", "account");
}
}
}
- 3 回答
- 0 关注
- 126 浏览
添加回答
举报