File upload into MySQL database failing. | Newbie | | Join Date: Jun 2009
Posts: 20
| |
Hi. I have a problem here. I want to upload a file together with its semester. So I create two field. One for the semester and one for the file to be uploaded. But the script doesn't work. I don't know why. However, if I try to upload the file without the semester field, the script is working. May be the problem comes from the semester field. This is my codes.
Form.php
?>
Upload.php - <?php
-
-
if (!isset($_SESSION)) {
-
session_start();
-
}
-
-
// ** Logout the current user. **
-
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
-
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
-
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
-
}
-
-
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
-
$_SESSION['MM_Username'] = NULL;
-
$_SESSION['MM_UserGroup'] = NULL;
-
$_SESSION['PrevUrl'] = NULL;
-
unset($_SESSION['MM_Username']);
-
unset($_SESSION['MM_UserGroup']);
-
unset($_SESSION['PrevUrl']);
-
-
$logoutGoTo = "login_option.php";
-
if ($logoutGoTo) {
-
header("Location: $logoutGoTo");
-
exit;
-
}
-
}
-
?>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>Untitled Document</title>
-
<style type="text/css">
-
<!--
-
body {
-
background-image: url(images/backgrounding.gif);
-
background-repeat: no-repeat;
-
background-attachment: fixed;
-
}
-
-->
-
</style></head>
-
-
<body>
-
<div align="center">
-
<table width="828" border="0">
-
<!--DWLayoutTable-->
-
<tr>
-
<td width="822" height="148" valign="top" bgcolor="#6699FF"><form id="form1" name="form1" method="post" action="">
-
<img src="banner/header.gif" width="822" height="128" />
-
<?php include 'selamat_datang.php'; ?>
-
<?php include 'dropDown.php'; ?>
-
</form></td>
-
</tr>
-
<tr>
-
<td height="32" valign="top" bgcolor="#6699FF"><p align="right"> <a href="<?php echo $logoutAction ?>"><font color='#FF0000'>Keluar</font></a></p>
-
<p align="center">
-
<?php
-
-
if(isset($_FILES['uploaded_file']))
-
{
-
-
if($_FILES['uploaded_file']['error'] == 0)
-
{
-
-
$dbLink = mysqli_connect("localhost", "root", "", "inovasi");
-
if(mysqli_connect_errno()) {
-
die("MySQL connection failed: ". mysqli_connect_error());
-
}
-
$sesi = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['sesi']);
-
$name = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['name']);
-
$mime = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['type']);
-
$size = $_FILES['uploaded_file']['size'];
-
$data = mysqli_real_escape_string($dbLink, file_get_contents($_FILES ['uploaded_file']['tmp_name']));
-
-
$query = "
-
INSERT INTO FileStorage (
-
FileName, FileMime, FileSize, FileData, Created, sesi
-
)
-
VALUES (
-
'{$name}', '{$mime}', {$size}, '{$data}', NOW(), '{$sesi}'
-
)";
-
-
$result = mysqli_query($dbLink, $query);
-
-
if($result)
-
{
-
echo "Berjaya! Abstrak telah di upload.";
-
}
-
else
-
{
-
echo "Error! Failed to insert the file";
-
echo "<pre>". mysqli_error($dbLink) ."</pre>";
-
}
-
}
-
else
-
{
-
echo "Error!
-
An error accured while the file was being uploaded.
-
Error code: ". $_FILES['uploaded_file']['error'];
-
}
-
mysqli_close($dbLink);
-
}
-
else
-
{
-
echo "Gagal! Abstrak tidak berjaya di upload!";
-
}
-
echo '<p>Klik <a href="JKM_admin.php">di sini</a> untuk kembali</p>';
-
?>
-
-
</p>
-
</td>
-
</tr>
-
<tr>
-
<td height="18" valign="top" bgcolor="#6699FF"><form id="form4" name="form4" method="post" action="">
-
<div align="center">
-
<?php include 'copyright.php'; ?>
-
</div>
-
</form>
-
</td>
-
</tr>
-
</table>
-
</div>
-
</body>
-
</html>
-
Someone please help me...Thanks
|  | Expert | | Join Date: Dec 2007 Location: Denver, CO
Posts: 1,144
| | | re: File upload into MySQL database failing.
That's too much code to go through and find one problem. Have you done your part of troubleshooting? what's the semester field? if with it, it fails, have you tried looking into to see if it's correct?
can you run a manual test query with the semester field from a client?
At least trace the problem (follow the values with die(print_r($value)) lines) until you find the code that doesn't do what it's suppose to do.
Good luck,
Dan
|  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|