简体   繁体   English

编辑表单创建新数据,而不是php

[英]Edit form creates new data instead php

im new to php, tried to 'google' the problem and it gave me nothing. 我是php的新手,试图'google'这个问题,但没有给我任何帮助。 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. 我有index.php,它具有创建新数据的形式,并显示来自sql的表数据以及edit.php上的链接以编辑数据。 In edit.php i have the form only. 在edit.php中,我只有表格。 So the problem is that edit.php creates new data in database instead of changing that one that i get by id. 因此,问题在于edit.php在数据库中创建了新数据,而不是更改我通过id获得的数据。 Tried to put request directly in phpmyadmin and everything worked fine. 试图直接在phpmyadmin中放置请求,并且一切正常。

edit.php 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']); var_dump($id = $_GET['edit']); i get the correct id of data. 我得到正确的数据ID。

Form from view_edit.php 来自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 来自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 并从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 尝试在view_edit.php中的表单action中添加一些代码,并始终使用表单的输入提交按钮

<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'; 删除include 'view_edit.php'; from edit.php and use this code and tell me what happens edit.php并使用此代码,告诉我会发生什么

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. view_edit.php表单的动作属性设置为index.php,因此,除非edit.php包含在index.php中,否则提交的表单将不会由edit.php而是由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. 无论如何,MySQL中的UPDATE查询无法创建新记录,因此插入必须发生在未在此处发布的某些代码中的其他位置。

On a sidenote: Your code in very insecure. 旁注:您的代码非常不安全。 Google SQL-injection. Google SQL注入。 People could use that form to drop your database or cause some other kind of harm. 人们可能会使用该表格删除您的数据库或造成其他危害。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP AJAX - 数据更新/编辑作为新数据插入而不是更新 - PHP AJAX - data update/edit inserts as new data instead of updating 在 CakePHP 中编辑创建新条目而不是更新旧条目 - Edit in CakePHP creates new entry instead of updating old one Symfony 3.1:编辑表单会创建新的数据库条目而不是更新 - Symfony 3.1: Editing form creates new database entry instead of updating PHP中的哪些代码片段可以创建输入表单,从而在mysql数据库中创建一组新数据? - Which code snipplets in PHP can create a input form, which creates a new set of data in my mysql database? 在Symfony3中,我试图将表单数据持久化到数据库中,但是它不会转到当前用户的行,而是创建一个新条目 - In Symfony3 I am trying to persist form data to the database, but it doesn't go to the current user's row, instead creates a new entry 在新窗口中传递表单数据以进行编辑和保存 - pass form data in a new window to edit and save 无法使用PHP表单编辑表中的数据 - unable to edit data in table using PHP form 在php中使用相同的表单插入和编辑数据 - Insert and edit the data using same form in php 使用 php 存储表单数据会打开一个新页面,而不是在同一页面中进行 - Storing form data with php opens a new page instead of doing it in same page 头文件下载将创建新文件,而不是从服务器(PHP)发送一个文件 - Header file download creates new file instead of sending one from server (PHP)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM