【问题标题】:Update image with information in php用php中的信息更新图像
【发布时间】:2019-04-21 13:55:50
【问题描述】:

我一直在寻找代码来从 youtube 和 stackoverflow 的数据库中更新我的图像。但我似乎无法让它发挥作用。图像可以更新,但不能编辑标题和正文。请帮忙。

我用 php 和 phpmyadmin 来使用它

这是我一直在尝试进行更新的代码:

if (isset($_POST['update'])) {

 $id = $_POST['editid'];
 $edtitle = $_POST['edittitle'];
 $edbody = $_POST['editmyTextarea'];
 $file = $_FILES['editpgupload'];

 $filename = $file['name'];
 $fileTmp = $file['tmp_name'];
 $filesize = $file['size'];
 $fileerror = $file['error'];
 $filetype = $file['type'];

$fileExt = explode('.', $filename);
$fileActExt = strtolower(end($fileExt));

$allowed = array('jpg', 'jpeg', 'png');

if (in_array($fileActExt, $allowed)) {
  if ($fileerror === 0) {
    if ($filesize < 1000000) {
      $filenamenew = uniqid('', true).".".$fileActExt;
      $fileds = '../../../image/upload/'.$filenamenew;
      move_uploaded_file($fileTmp, $fileds);

      $sql = "UPDATE patients_guide SET pg_title = '$edtitle', pg_body = '$edbody', pg_image = '$filenamenew' WHERE pg_id = '$id'";
      mysqli_query($conn, $sql);

      header("Location: ../index.php?update=success");
    }else{
      // echo "your image is too large";
      header("Location: ../index.php?error=imagetoolarge");
    }

  }else{
    // echo "There was an error uploading your file";
    header("Location: ../index.php?error=errorupload");
  }

}else{
  // echo "You can not upload this file";
  header("Location: ../index.php?error=cannotupload");
}
}

它只更新图像,但标题和正文保持不变。它不能被编辑。这应该能够更新标题、正文和图像。

患者指南结构

   CREATE TABLE `patients_guide` (
 `pg_id` int(11) NOT NULL AUTO_INCREMENT,
 `pg_title` varchar(100) NOT NULL,
 `pg_body` text NOT NULL,
 `pg_image` varchar(100) NOT NULL,
 PRIMARY KEY (`pg_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf

【问题讨论】:

  • 首先我强烈建议使用准备好的语句。我有一个问题:您为什么决定其他字段没有更新?
  • @Alex 我希望他们更新到。但我的代码只更新图像。我也想更新标题和正文
  • 尝试检查$edtitle$edbody 是否不为空。你可以echo他们看看他们包含什么。也许你在他们各自的$_POSTs 中有错误的键?
  • @Yuko 谢谢 :) 我已经知道了
  • 警告:您对SQL Injections 持开放态度,应该真正使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何类型的输入,尤其是来自客户端的输入。即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data

标签: php sql database mysqli


【解决方案1】:

我明白了。

对于和我有同样问题的人,这里是代码:

if (isset($_POST['update'])) {

$id = $_POST['editid'];
$edtitle = $_POST['edittitle'];
$edbody = $_POST['editmyTextarea'];
$file = $_FILES['editpgupload'];

$filename = $file['name'];
$fileTmp = $file['tmp_name'];
$filesize = $file['size'];
$fileerror = $file['error'];
$filetype = $file['type'];

//remove old image
unlink('../../../image/upload/'.$row['pg_image']);

$fileExt = explode('.', $filename);
$fileActExt = strtolower(end($fileExt));

$allowed = array('jpg', 'jpeg', 'png');

if (in_array($fileActExt, $allowed)) {
  if ($fileerror === 0) {
    if ($filesize < 1000000) {
      $filenamenew = uniqid('', true).".".$fileActExt;
      $fileds = '../../../image/upload/'.$filenamenew;
      move_uploaded_file($fileTmp, $fileds);

      $sql = "UPDATE patients_guide SET pg_title = '$edtitle', pg_body = '$edbody', pg_image = '$filenamenew' WHERE pg_id = '$id'";
      mysqli_query($conn, $sql);


      header("Location: ../index.php?update=success");
    }else{
      // echo "your image is too large";
      header("Location: ../index.php?error=imagetoolarge");
    }

  }else{
    // echo "There was an error uploading your file";
    header("Location: ../index.php?error=errorupload");
  }

}else{
  $sql = "UPDATE patients_guide SET pg_title = '$edtitle', pg_body = '$edbody' WHERE pg_id = '$id'";
      mysqli_query($conn, $sql);


      header("Location: ../index.php?update=success");
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-17
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2011-04-03
    • 1970-01-01
    相关资源
    最近更新 更多