我有问题。请查看我的main.php和upload.php
main.php
<?php
include "include/db_connection.php";
$id = isset($_GET['id']) ? $_GET['id'] : "";
if ($id=="") {
echo "<script>alert('Pilih Mahasiswa yang Ingin Unggah Berkas!');</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
} else {
$query = mysql_query("SELECT mahasiswa.nim, mahasiswa.nama, mahasiswa.jurusan
FROM mahasiswa
WHERE mahasiswa.nim = '$id'");
while ($hisal=mysql_fetch_array($query)){
$nim = $hisal['nim'];
$nama = $hisal['nama'];
$jur = $hisal['jurusan'];
}
}
?>
<table>
<tr>
<td>NIM</td>
<td>:</td>
<td><?php echo $nim;?></td>
</tr>
<tr>
<td>Nama</td>
<td>:</td>
<td><?php echo $nama;?></td>
</tr>
<tr>
<td>Jurusan</td>
<td>:</td>
<td><?php echo $jur;?></td>
</tr>
</table><br><br>
<form enctype="multipart/form-data" method="POST" action="?page=upload">
<table border="1" width="50%">
<tr>
<td align="center">Jenis Berkas</td>
<td align="center">Berkas</td>
<td align="center">Aksi</td>
</tr>
<tr>
<td align="center">Foto</td>
<td align="center"><input type="file" name="foto" accept="image/jpeg"></td>
<td align="center"><?php echo"<a href='?page=prosesUbah2&id=$hisal[nim];'>Ubah</a> |
<a href='?page=prosesHapus2&id=$id' onclick='return confirm(\"Anda yakin ingin menghapus berkas dari nim $id ?\")'>Hapus</a>"
?>
</td>
</tr>
<tr>
<td align="center">Scan KTM</td>
<td align="center"><input type="file" name="ktm" accept="image/jpeg"></td>
<td align="center"><?php echo"<a href='?page=prosesUbah2&id=$id'>Ubah</a> |
<a href='?page=prosesHapus2&id=$id' onclick='return confirm(\"Anda yakin ingin menghapus berkas dari nim $id ?\")'>Hapus</a>"
?>
</td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Unggah" id="Unggah" value="Unggah Berkas"></td>
</tr>
</table>
</form>
upload.php的
<?php
include "include/db_connection.php";
$id = isset($_GET['id']) ? $_GET['id'] : "";
$file = "uploads/".$_FILES["foto"]["name"];
if ($_FILES["foto"]["error"] > 0 && $id==""){
echo "<script>alert('Upload Failed')</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
} else {
move_uploaded_file($_FILES["foto"]["tmp_name"], "uploads/".$_FILES["foto"]["name"]);
$sql=mysql_query("INSERT INTO mahasiswa VALUE ('','','','','','','$file','','') WHERE mahasiswa.nim='$id'");
if ($sql){
echo "<script>alert('Upload Gambar Sukses')</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
} else{
echo "<script>alert('Upload Failed')</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
}
}
?>
将图像文件复制到uploads文件夹,但其路径不存储在数据库中。当我点击“Unggah Berkas”按钮时,警报显示“上传失败”,但图像被复制到上传文件夹。你能帮助我吗?不管怎样,谢谢。
更新
我已经改变了这个
<form enctype="multipart/form-data" method="POST" action="?page=upload&id=<?php echo $id; ?>">
//my code
</form>
和这个
<?php
include "include/db_connection.php";
$id = isset($_GET['id']) ? $_GET['id'] : "";
$file = "uploads/".$_FILES["foto"]["name"];
if ($_FILES["foto"]["error"] > 0 || $id==""){
echo "<script>alert('Upload Failed')</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
} else {
move_uploaded_file($_FILES["foto"]["tmp_name"], "uploads/".$_FILES["foto"]["name"]);
$sql=mysql_query("INSERT INTO mahasiswa VALUE ('','','','','','','$file','','') WHERE mahasiswa.nim='$id'");
}
?>
仍未存入数据库
答案 0 :(得分:0)
路径无法存储在表格中,因为您未在网址中发送id
。您的<form>
标记应如下所示:
<form enctype="multipart/form-data" method="POST" action="upload.php?id=<?php echo $id; ?>">
// your code
</form>
<强>更新强>
并且还会像这样更改if
条件:
if($_FILES["foto"]["error"] > 0 || $id==""){
// error
}else {
// no error
}
<强>重新编辑:强>
这将是您的目录结构:
include
|--db_connection.php
uploadform
|--main.php
|--upload.php
uploads
的 main.php 强>
假设用户的ID为12,那么您必须像这样调用main.php, main.php?id = 12
<?php
require_once("../include/db_connection.php");
$id = isset($_GET['id']) ? $_GET['id'] : "";
if ($id=="") {
echo "<script>alert('Pilih Mahasiswa yang Ingin Unggah Berkas!');</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
} else {
$query = mysql_query("SELECT mahasiswa.nim, mahasiswa.nama, mahasiswa.jurusan
FROM mahasiswa
WHERE mahasiswa.nim = '$id'");
while ($hisal=mysql_fetch_array($query)){
$nim = $hisal['nim'];
$nama = $hisal['nama'];
$jur = $hisal['jurusan'];
}
}
?>
<table>
<tr>
<td>NIM</td>
<td>:</td>
<td><?php echo $nim;?></td>
</tr>
<tr>
<td>Nama</td>
<td>:</td>
<td><?php echo $nama;?></td>
</tr>
<tr>
<td>Jurusan</td>
<td>:</td>
<td><?php echo $jur;?></td>
</tr>
</table><br><br>
<form enctype="multipart/form-data" method="POST" action="upload.php?id=<?php echo $id; ?>">
<table border="1" width="50%">
<tr>
<td align="center">Jenis Berkas</td>
<td align="center">Berkas</td>
<td align="center">Aksi</td>
</tr>
<tr>
<td align="center">Foto</td>
<td align="center"><input type="file" name="foto" accept="image/jpeg"></td>
<td align="center"><?php echo"<a href='?page=prosesUbah2&id=$hisal[nim];'>Ubah</a> |
<a href='?page=prosesHapus2&id=$id' onclick='return confirm(\"Anda yakin ingin menghapus berkas dari nim $id ?\")'>Hapus</a>"
?>
</td>
</tr>
<tr>
<td align="center">Scan KTM</td>
<td align="center"><input type="file" name="ktm" accept="image/jpeg"></td>
<td align="center"><?php echo"<a href='?page=prosesUbah2&id=$id'>Ubah</a> |
<a href='?page=prosesHapus2&id=$id' onclick='return confirm(\"Anda yakin ingin menghapus berkas dari nim $id ?\")'>Hapus</a>"
?>
</td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Unggah" id="Unggah" value="Unggah Berkas"></td>
</tr>
</table>
</form>
的 upload.php的强>
<?php
require_once("../include/db_connection.php");
$id = isset($_GET['id']) ? $_GET['id'] : "";
$file = "uploads/".$_FILES["foto"]["name"];
if ($_FILES["foto"]["error"] > 0 || $id==""){
echo "<script>alert('Upload Failed')</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
} else {
move_uploaded_file($_FILES["foto"]["tmp_name"], "../uploads/".$_FILES["foto"]["name"]);
$sql=mysql_query("INSERT INTO mahasiswa VALUE ('','','','','','','$file','','') WHERE mahasiswa.nim='$id'");
if ($sql){
echo "<script>alert('Upload Gambar Sukses')</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
} else{
echo "<script>alert('Upload Failed')</script>";
echo "<meta http-equiv='refresh' content='0; url=?page=tugas2'>";
}
}
?>
答案 1 :(得分:0)
只需从
更改查询$sql=mysql_query("INSERT INTO mahasiswa VALUE ('','','','','','','$file','','') WHERE mahasiswa.nim='$id'");
到
$sql=mysql_query("UPDATE mahasiswa SET foto='$foto' WHERE mahasiswa.nim='$id'");