<?php
session_start();
require_once "connect.php";
if (isset($_POST['update'])) {
$id = $_POST['id'];
$user_name = $_POST['user_name'];
$email = $_POST['email'];
$img = $_FILES['img'];
$img2 = $_POST['img2'];
$upload = $_FILES['img']['name'];
if ($upload != '') {
$allow = array('jpg', 'jpeg', 'png');
$extension = explode('.', $img['name']);
$fileActExt = strtolower(end($extension));
$fileNew = rand() . "." . $fileActExt; // rand function create the rand number
$filePath = 'uploads/'.$fileNew;
if (in_array($fileActExt, $allow)) {
if ($img['size'] > 0 && $img['error'] == 0) {
move_uploaded_file($img['tmp_name'], $filePath);
}
}
} else {
$fileNew = $img2;
}
$sql = $pdo->prepare("UPDATE users SET user_name = :user_name, email = :email, img = :img WHERE id = :id");
$sql->bindParam(":id", $id);
$sql->bindParam(":user_name", $user_name);
$sql->bindParam(":email", $email);
$sql->bindParam(":img", $fileNew);
$sql->execute();
if ($sql) {
$_SESSION['success'] = "Data has been updated successfully";
header("location: E.php");
} else {
$_SESSION['error'] = "Data has not been updated successfully";
header("location: E.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Data</title>
<link href="
https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
.container {
max-width: 550px;
}
</style>
</head>
<body>
<div class="container mt-5">
<h1>Edit Data</h1>
<hr>
<form action="E.php" method="post" enctype="multipart/form-data">
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = '$id'");
$stmt->execute();
$data = $stmt->fetch();
}
?>
<div class="mb-3">
<label for="id" class="col-form-label">ID:</label>
<input type="text" readonly value="<?php echo $data['id']; ?>" required class="form-control" name="id" >
<label for="user_name" class="col-form-label">Name:</label>
<input type="text" value="<?php echo $data['user_name']; ?>" required class="form-control" name="user_name" >
<input type="hidden" value="<?php echo $data['img']; ?>" required class="form-control" name="img2" >
</div>
<div class="mb-3">
<label for="user_name" class="col-form-label">Email</label>
<input type="text" value="<?php echo $data['email']; ?>" required class="form-control" name="email">
</div>
<div class="mb-3">
<label for="user_name" class="col-form-label">Position:</label>
<input type="text" value="<?php echo $data['position']; ?>" required class="form-control" name="position">
</div>
<div class="mb-3">
<label for="img" class="col-form-label">Image:</label>
<input type="file" class="form-control" id="imgInput" name="img">
<img width="100%" src="uploads/<?php echo $data['img']; ?>" id="previewImg" alt="">
</div>
<hr>
<a href="E.php" class="btn btn-secondary">Go Back</a>
<button type="submit" name="update" class="btn btn-primary">Update</button>
</form>
</div>
<script>
let imgInput = document.getElementById('imgInput');
let previewImg = document.getElementById('previewImg');
imgInput.onchange = evt => {
const [file] = imgInput.files;
if (file) {
previewImg.src = URL.createObjectURL(file)
}
}
</script>
</body>
</html>
ผมเขียนcode ขึ้น error Trying to access array offset on value of type bool in ของphp ต้องแก้ไงหรอครับ
session_start();
require_once "connect.php";
if (isset($_POST['update'])) {
$id = $_POST['id'];
$user_name = $_POST['user_name'];
$email = $_POST['email'];
$img = $_FILES['img'];
$img2 = $_POST['img2'];
$upload = $_FILES['img']['name'];
if ($upload != '') {
$allow = array('jpg', 'jpeg', 'png');
$extension = explode('.', $img['name']);
$fileActExt = strtolower(end($extension));
$fileNew = rand() . "." . $fileActExt; // rand function create the rand number
$filePath = 'uploads/'.$fileNew;
if (in_array($fileActExt, $allow)) {
if ($img['size'] > 0 && $img['error'] == 0) {
move_uploaded_file($img['tmp_name'], $filePath);
}
}
} else {
$fileNew = $img2;
}
$sql = $pdo->prepare("UPDATE users SET user_name = :user_name, email = :email, img = :img WHERE id = :id");
$sql->bindParam(":id", $id);
$sql->bindParam(":user_name", $user_name);
$sql->bindParam(":email", $email);
$sql->bindParam(":img", $fileNew);
$sql->execute();
if ($sql) {
$_SESSION['success'] = "Data has been updated successfully";
header("location: E.php");
} else {
$_SESSION['error'] = "Data has not been updated successfully";
header("location: E.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Data</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
.container {
max-width: 550px;
}
</style>
</head>
<body>
<div class="container mt-5">
<h1>Edit Data</h1>
<hr>
<form action="E.php" method="post" enctype="multipart/form-data">
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = '$id'");
$stmt->execute();
$data = $stmt->fetch();
}
?>
<div class="mb-3">
<label for="id" class="col-form-label">ID:</label>
<input type="text" readonly value="<?php echo $data['id']; ?>" required class="form-control" name="id" >
<label for="user_name" class="col-form-label">Name:</label>
<input type="text" value="<?php echo $data['user_name']; ?>" required class="form-control" name="user_name" >
<input type="hidden" value="<?php echo $data['img']; ?>" required class="form-control" name="img2" >
</div>
<div class="mb-3">
<label for="user_name" class="col-form-label">Email</label>
<input type="text" value="<?php echo $data['email']; ?>" required class="form-control" name="email">
</div>
<div class="mb-3">
<label for="user_name" class="col-form-label">Position:</label>
<input type="text" value="<?php echo $data['position']; ?>" required class="form-control" name="position">
</div>
<div class="mb-3">
<label for="img" class="col-form-label">Image:</label>
<input type="file" class="form-control" id="imgInput" name="img">
<img width="100%" src="uploads/<?php echo $data['img']; ?>" id="previewImg" alt="">
</div>
<hr>
<a href="E.php" class="btn btn-secondary">Go Back</a>
<button type="submit" name="update" class="btn btn-primary">Update</button>
</form>
</div>
<script>
let imgInput = document.getElementById('imgInput');
let previewImg = document.getElementById('previewImg');
imgInput.onchange = evt => {
const [file] = imgInput.files;
if (file) {
previewImg.src = URL.createObjectURL(file)
}
}
</script>
</body>
</html>