duanbiao4025 2017-02-23 20:33
浏览 49
已采纳

使用复选框选择无法正确删除图像

I am having difficulty removing an image URL from a MySQL table with the use of a checkbox, if a user decides they no longer want an image (avatar) to be used on their profile.

The table (it has more columns) looks like:

user_id | img                                    | user_name
 1      |  http://www.domain.com/path/to/image   |  User A
 2      |                                        |  User B
 3      |                                        |  User C

On the user profiles, if a URL exists in img, then the image properly shows.

My HTML form looks like:

<form method="post" action="user/update_info.php">
  <div class="form-group">
    <label for="user_name">Username:</label>
    <input type="text" id="user_name" name="user_name" value="user_name" required>
  </div>
  <div class="form-group">
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" value="email" required>
  </div>
  <div class="form-group">
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" value="password" required>
  </div>
  <div class="form-group col-sm-4">
    <input type="file" name="img">
    <br>
    <label for="noav">Use no avatar.</label>
    <input type="checkbox" id="noav" name="noav" value="Yes">
  </div>
  <input type="submit" class="btn btn-warning" value="Update">
</form>

My PHP file looks like:

<?php
$user_id = $_SESSION["user"]['user_id'];
$user_name = $_POST['user_name'];
$email = $_POST['email'];
$password = $_POST['password'];
$noav = $_POST['noav'];
$session_password = $_SESSION["user"]['password'];

if ($password != $session_password) {
  $password = password_hash($password, PASSWORD_DEFAULT);
}
$img = $_FILES["img"];
$query = "UPDATE users 
          SET user_name='$user_name', char_name='$char_name',
              user_email='$email', password='$password'
          WHERE user_id=$user_id";
mysqli_query($this->connection, $query)
  or die("Update error: " . mysqli_error($this->connection));
if($img["error"] == 0) {
  $name = "public/gallery/" . time() . $img["name"];
  $name_in_db = URL . $name;
  $tmp_name = $img["tmp_name"];
  move_uploaded_file($tmp_name,  $name);
  if ($noav = 'Yes') {
    $query = "UPDATE users SET img='' WHERE user_id=$user_id";
  } else {
    $query = "UPDATE users SET img='$name_in_db' WHERE user_id=$user_id";
  }
mysqli_query($this->connection, $query)
  or die("Update error: " . mysqli_error($this->connection));
}
?>

Before I added in the "noav" checkbox, variable, and if check (only the bottom query existed), everything functioned properly.

Adding in the "noav" stuff essentially broke it. Now, no matter what, if the checkbox is checked or not, and if an image is selected or not, it clears the img column for that user, making no img whatsoever.

  • 写回答

1条回答 默认 最新

  • douhao3562 2017-02-23 20:41
    关注

    The problem is with this if ($noav = 'Yes')

    You're assigning instead of comparing:

    if ($noav == 'Yes')
    

    References:

    Plus, it's best to check if the checkbox is set using isset().

    Also make sure the session was started; that is unknown.

    If it wasn't, add session_start(); inside all your files using them.

    You're also open to a serious SQL injection. Use a prepared statement:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。