Actualy试图解决这个问题3天,现在不知道该怎么办。 编辑表格不起作用。我没有错误消息。
没有保存数据文件,因为我只从数据库输出。 并且 edit.php
<?php
include_once 'config.php';
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 = mysqli_real_escape_string($db,
trim($_POST['username']));
$email = mysqli_real_escape_string($db,
trim($_POST['email']));
$task = mysqli_real_escape_string($db,
trim($_POST['task']));
$image = mysqli_real_escape_string($db,
$_FILES['image']['name']);
$target = "uploads/".basename($_FILES['image']['name']);
$sql = "UPDATE `tasks` SET `username`='$username', `email`='$email',
`task`='$task', `image`='$image' WHERE `id`='$id'";
mysqli_query($db, $sql);
move_uploaded_file($_FILES['image']['tmp_name'], $target);
$home_url = 'http://' . $_SERVER['HTTP_HOST'];
header('Location:' . $home_url);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>задачник</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-
BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<link href="/css/style.css" rel="stylesheet">
</head>
<body>
<header>
<h3>Внесение изменений в запись</h3>
<a href="index.php" class="btn btn-default">На страницу
администратора</a>
</header>
<br>
<form method="POST" action="<?=$_SERVER['PHP_SELF'];?>" 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>
我尝试var_dump($_GET('edit'));
并获得了我想要编辑的页面的正确ID,但在提交按钮后,我被重定向到index.php而没有更改db。 var_dump($_POST['submit']);
给出了字符串(0),但是这个项目的另一个文件夹的save_task表单工作正常,在提交后也给出了字符串(0)。
答案 0 :(得分:1)
edit.php 文件中的更新查询不正确。
$sql = "UPDATE `tasks` SET `username`='$username', `email`='$email',`task`='$task', WHERE `id`='$id'";
在where子句之前的更新查询中存在附加分号。删除它。
答案 1 :(得分:0)
我认为错误在$image
值。调试自己并找到错误 -
<?php
include_once 'config.php';
if (isset($_POST['submit'])){
echo 'form submitted? - okay <br/>';
if (empty($_POST['username'])){
$errors = "Впишите ваше имя";
}elseif (empty($_POST['email'])){
$errors = "Впишите ваш email";
}elseif (empty($_POST['task'])){
$errors = "Впишите задание";
}elseif (empty($_FILES['image']['name'])){
$errors = "Вставьте картинку";
}else{
echo 'form validated? - okay <br/>';
$id = $_GET['edit'];
$username = mysqli_real_escape_string($db,
trim($_POST['username']));
$email = mysqli_real_escape_string($db,
trim($_POST['email']));
$task = mysqli_real_escape_string($db,
trim($_POST['task']));
$image = mysqli_real_escape_string($db,
$_FILES['image']['name']);
//most probably error is here --- $_FILES['image']['name'] should gives you array!!! as you have used "multiple" in input
var_dump($_FILES, $_FILES['image'], $_FILES['image']['name']); //die('debug--');
$target = "uploads/".basename($_FILES['image']['name']);
$sql = "UPDATE `tasks` SET `username`='$username', `email`='$email',
`task`='$task', `image`='$image' WHERE `id`='$id'";
echo $sql.'<br/>' ; //copy this sql and run in phpmyadmin sql-console and check if there is any error
die('checking sql statement');
mysqli_query($db, $sql);
move_uploaded_file($_FILES['image']['tmp_name'], $target);
$home_url = 'http://' . $_SERVER['HTTP_HOST'];
//header('Location:' . $home_url);
}
}
?>
答案 2 :(得分:0)
检查&#34;行动&#34; edit.php 文件中的表单标记中的属性。 action="<?=$_SERVER['PHP_SELF'];?>"
。应该是action="/<?PHP echo basename($_SERVER['REQUEST_URI']); ?>"
。
PHP_SELF不附加get参数。