简体   繁体   中英

Edit form creates new data instead php

im new to php, tried to 'google' the problem and it gave me nothing. I have index.php that has a form of creating new data and shows table data from sql with link on edit.php to edit data. In edit.php i have the form only. So the problem is that edit.php creates new data in database instead of changing that one that i get by id. Tried to put request directly in phpmyadmin and everything worked fine.

edit.php

include '../connect.php';
include '../errors.php';
include 'view_edit.php';

var_dump($id = $_GET['edit']);

if (isset($_POST['submit'])){
  if (empty($_POST['username'])){
    $errors = "Впишите ваше имя";
  }elseif (empty($_POST['email'])){
    $errors = "Впишите ваш email";
  }elseif (empty($_POST['task'])){
    $errors = "Впишите задание";
  }elseif (empty($_FILES['image']['name'])){
    $errors = "Вставьте картинку";
  }else{
    $id = $_GET['edit'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $task = $_POST['task'];
    $image = $_FILES['image']['name'];
    $target = "../uploads/".basename($_FILES['image']['name']);

    $sql = "UPDATE `tasks` SET `username`='$username', `email`='$email',
        `task`='$task', `image`='$image' WHERE `id`='$id'";
    mysql_query($db. $sql);
    move_uploaded_file($_FILES['image']['tmp_name'], $target);
    header('location: index.php');
  }
}

in var_dump($id = $_GET['edit']); i get the correct id of data.

Form from view_edit.php

  <form method="post" action="index.php" class="input_form" enctype="multipart/form-
  data">
  <input type="text" name="username" placeholder="Введите Имя" 
  class="username_input">
  <input type="email" name="email" placeholder="Введите email" 
   class="email_input">
    <br>
    <br>
  <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
  <input type="text" name="task" placeholder="Введите задание" 
  class="task_input">
  <p>Сменить изображение</p>
  <input type="file" name="image" multiple accept="image/png, image/jpeg, 
  image/gif">
    <br>
  <button type="submit" name="submit" id="add_btn">Изменить 
  запись</button>
  </form>

Table from index.php

while ($row = mysqli_fetch_assoc($tasks)) { ?>
  <tr>
  <td class="username"> <?php echo $row['username']; ?> </td>
  <td class="email"> <?php echo $row['email']; ?> </td>
  <td class="task"> <?php echo $row['task']; ?> </td>
  <td> <?php echo "<img src='../uploads/".$row['image']."'>"; ?> </td>
    <td>
  <a class="delete" href="index.php?del_task=<?php echo $row['id'] ?>">x</a>
    <br><br>
  <a class="edit" href="edit.php?edit=<?php echo $row['id']; ?
     >">Редактировать</a>
    </td>
  </tr>
 <?php  } ?>

And form from view_index.php

<form method="post" action="index.php" class="input_form" 
 enctype="multipart/form-data">
<input type="text" name="username" placeholder="Введите ваше имя" 
 class="username_input" id="username">
<input type="email" name="email" placeholder="Введите ваш email" 
  class="email_input" id="email">
  <br><br>
<input type="text" name="task" placeholder="Введите задание" 
 class="task_input"  id="task">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
 <p>Добавить изображение</p>
<input type="file" name="image" multiple accept="image/png, image/jpeg, 
  image/gif">
 <br>
<button type="submit" name="submit" id="add_btn">Добавить задачу</button>
<button type="submit" name="preview" id="preview">Предварительный 
 просмотр</button>
</form>

Try adding some code in form action in view_edit.php and always use input submit button of form not

<form method="post" action="edit.php?edit=<?php echo $id; ?>" class="input_form" enctype="multipart/form-
      data">
        <input type="text" name="username" placeholder="Введите Имя"
               class="username_input">
        <input type="email" name="email" placeholder="Введите email"
               class="email_input">
        <br>
        <br>
        <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
        <input type="text" name="task" placeholder="Введите задание"
               class="task_input">
        <p>Сменить изображение</p>
        <input type="file" name="image" multiple accept="image/png, image/jpeg, 
      image/gif">
        <br>
        <input type="submit" name="edit_task" id="add_btn"
value="Изменить запись"/>

Remove include 'view_edit.php'; from and use this code and tell me what happens 并使用此代码,告诉我会发生什么

The action-attribute of your view_edit.php form is set to index.php, so unless edit.php gets included in the index.php, the submitted form won't be handled by edit.php but by index.php.

Anyway, an UPDATE query in MySQL can't create a new record, so the insertion must happen somewhere else in some code that is not posted here.

On a sidenote: Your code in very insecure. Google SQL-injection. People could use that form to drop your database or cause some other kind of harm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM