[英]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.