![](/img/trans.png)
[英]PHP AJAX - data update/edit inserts as new data instead of updating
[英]Edit form creates new data instead php
我是php的新手,試圖'google'這個問題,但沒有給我任何幫助。 我有index.php,它具有創建新數據的形式,並顯示來自sql的表數據以及edit.php上的鏈接以編輯數據。 在edit.php中,我只有表格。 因此,問題在於edit.php在數據庫中創建了新數據,而不是更改我通過id獲得的數據。 試圖直接在phpmyadmin中放置請求,並且一切正常。
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');
}
}
在var_dump($id = $_GET['edit']);
我得到正確的數據ID。
來自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>
來自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 } ?>
並從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>
嘗試在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="Изменить запись"/>
刪除include 'view_edit.php';
從edit.php並使用此代碼,告訴我會發生什么
view_edit.php表單的動作屬性設置為index.php,因此,除非edit.php包含在index.php中,否則提交的表單將不會由edit.php而是由index.php處理。
無論如何,MySQL中的UPDATE查詢無法創建新記錄,因此插入必須發生在未在此處發布的某些代碼中的其他位置。
旁注:您的代碼非常不安全。 Google SQL注入。 人們可能會使用該表格刪除您的數據庫或造成其他危害。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.