【发布时间】:2018-03-18 12:34:37
【问题描述】:
我有不同的文件上传格式的不同容器,但我不知道出了什么问题。当我上传文件时,我用于更新数据库的 $_POST 变量是错误的。
这就是我使用 while 循环打印容器的方式:
echo '<div class="container-r">'.$row['name'].'<div class="im-up">
<label for="fileup"><img src="logo/upload.png" alt="Upload file"/></label>
<form action="uploadFile.php" method="post" enctype="multipart/form-data">
<input type="file" onchange="this.form.submit()" id="fileup" name="fileup"/>
<input type="hidden" name="idd" id="idd" value="'.$row['id'].'" />
</form></div><li>'.$row['evname'].'</li></div><br><br>';
我感兴趣的值是 idd 并且使用 Firefox 的 inspect element 我可以看到所有形式的 idd 值都是正确的(在我的情况下,我有一个值为 10 的表单和一个值为 11 的表单)。
问题是即使我使用idd为11的形式,直接在第二个php文件中获取的idd变量$_POST 数组中的值是 10。
在 uploadFile.php 我首先有这两行:
$id = $_POST['idd'];
var_dump($id);
并且 $id 总是 10,即使我来自值为 11 的表单。那么,我做错了什么?我应该检查什么?注意,这是一个简单的学习php的项目,并没有伪装成一个高级项目。
编辑:这是完整的uploadFile.php
<?php
// Start the session
session_start();
require 'connection.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Register</title>
<link href="css/register.css" rel="stylesheet" type="text/css">
</head>
<body>
<table align="center" cellspacing="0" cellpadding="0">
<tr valign="middle" align="center">
<td class="menu" background="graphics/button.png" width="120" height="30">
<a class="mm" href="home.php"><div>Home</div></a>
</td>
<td class="menu" background="graphics/button.png" width="120" height="30">
<a class="mm" href="account.php"><div>Account</div></a>
</td>
<?php
if(isset($_SESSION['loggedin'])){
echo '<td class="menu" background="graphics/button3.png" width="120" height="30"><a class="mm" href="logout.php"><div>Logout</div></a></td>';
}
else
echo '<td class="menu" background="graphics/button.png" width="120" height="30"><a class="mm" href="login.php"><div>Login</div></a></td><td class="menu" background="graphics/button3.png" width="120" height="30"><a class="mm" href="register.php"><div>Registrati</div></a></td>'?>
</tr>
</table>
<?php
$id = $_POST['idd'];
var_dump($id);
$uploaddir = 'progr/';
$file2 = $uploaddir . basename($_FILES["fileup"]["name"]). date("h i");
if (move_uploaded_file($_FILES["fileup"]["tmp_name"], $file2)) {
$stmt = $conn->prepare("UPDATE presentation SET pres_path=? WHERE user_id LIKE ? AND id LIKE ?");
$stmt -> bind_param('sii',$file2,$_SESSION['idLog'],$id);
if($stmt->execute())
echo "<br><h4>The file ". basename( $_FILES["fileup"]["name"]). " was uploaded</h4>".$id;
else
echo mysqli_error($conn);
} else {
echo "<br><h3>Uploading error</h3>";
}
?>
</body>
</html>
【问题讨论】:
-
检查所有
-
从这些元素中移除 id 属性 - 或者为每个元素分配一个唯一的 id
-
HTML 中的 id 属性必须是唯一的。
-
无论 id 属性如何,html 都是无效的 - 你在 div 标签后面有
<li>标签?? -
你用什么来更新你的数据库呢?
标签: php database forms file-upload