|
 |
|
รบกวนดูให้ทีครับ ผมต้องการอัพเดตข้อมูลที่มีไฟล์รูปภาพ ในกรณีที่เราไม่ได้inputรูปไปด้วย พอกดอัพเดตเเล้วรูปหาย ต้องทำยังไงครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
if(isset($_POST['update']))
{
$id = mysqli_real_escape_string($con, $_POST['id']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$email = mysqli_real_escape_string($con, $_POST['email']);
if(isset($_FILES['image'])){
$temp = $_FILES['image']['tmp_name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];
$profile = $_FILES['image']['name'];
$ext = pathinfo($profile, PATHINFO_EXTENSION);
$image_file ="$id.$ext";
unlink("profile/$image_file");
move_uploaded_file($temp, "profile/$image_file");
$sql = "UPDATE applyjob SET image='$image_file',phone='$phone',email='$email' WHERE id=$id";
}else{
$sql = "UPDATE applyjob SET phone='$phone',email='$email' WHERE id=$id";
}
$result = mysqli_query($con, $sql);
header("Location: index.php?id=$id");
}
|
 |
 |
 |
 |
Date :
2021-02-12 15:14:29 |
By :
lakornworld |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เปลี่ยนเงื่อนไขใหม่
Code (PHP)
if(!empty($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
// Handle uploaded image
}
หรือ เช็ค size
|
 |
 |
 |
 |
Date :
2021-02-12 18:36:46 |
By :
lakornworld |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
if ($_POST) {
// ถ้ามีการ submit form -> method POST
if ($formValidaation === true) {
// ถ้าตรวจฟอร์มผ่านหมด เช่น ช่องที่ต้องกรอก, รูปแบบที่ควรกรอก..
if (isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name'])) {
// ถ้ามีการอัพโหลด
if ($uploadedFileValidated === true) {
// ถ้าตรวจไฟล์อัพโหลด เช่น ประเภท, นามสกุล, ขนาด ผ่านหมดแล้ว
// ถ้าต้องมีการลบไฟล์เก่า ก็ลบตรงนี้ที่นี่
// move_uploaded_file();
// กำหนดค่าไฟล์ที่จะบันทึกลง DB ลงตัวแปร เช่น $updateUploaded
$updateUploaded = [];
} else {
// ถ้าการอัพโหลดมีการตรวจแล้วไม่ผ่าน
$formValidaation = false;// กำหนดว่าไม่ผ่าน
}
}
if ($formValidaation === true) {
// ตรวจอีกทีว่าผ่านแล้ว"ทั้งหมด" รวมถึงไฟล์อัพโหลดด้วย
// เตรียมข้อมูลสำหรับการ UPDATE
$updateData = [];
if (isset($updateUploaded)) {
// ถ้ามีการอัพโหลดด้วย มันจะมีตัวแปรนี้
// ให้สร้างคำสั่ง UPDATE ที่มีการกำหนดค่าไฟล์ใหม่ลงไป
}
// execute คำสั่ง UPDATE.
} else {
// echo form error ตรงนี้รวมถึงการอัพโหลด ตรวจสอบไม่ผ่าน
}
} else {
// แสดง error ฟอร์มไม่ผ่าน
// จบ
}
}
|
ประวัติการแก้ไข 2021-02-12 19:58:42
 |
 |
 |
 |
Date :
2021-02-12 19:56:54 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|