Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,257 members, 7,807,887 topics. Date: Wednesday, 24 April 2024 at 09:45 PM

Please Help Me With Php Code To Submit Picture To Database - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Please Help Me With Php Code To Submit Picture To Database (2537 Views)

How To Submit Your Site/blog To Facebook Free-basics / How To Submit Sitemap To Your Wapka Site / Create Png ID Card With Database Info With PHP (2) (3) (4)

(1) (Reply) (Go Down)

Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 2:46pm On Mar 27, 2015
I am currently working on my project Studeñt information management system. Have created other entries and they all submited to my database successfully using Dreamweaver, phpmyadmin and aphache.

Now remain to submit passport, my data type is blob on mysql but I can't submit as blob from dreamweaver. I can only submit as text.

please help me out gurus in d house
Re: Please Help Me With Php Code To Submit Picture To Database by FRInteractives: 4:09pm On Mar 27, 2015
Please post the code you are using let's see where the bugs are
Re: Please Help Me With Php Code To Submit Picture To Database by aikfrank(m): 4:16pm On Mar 27, 2015
If its a test then you can go ahead and debug, else if its a major project, do not post images to database.


Best practice: store images using the file system. Submitting the form will simply upload the image to a folder, and store the part in the database, then you simply retrieve the part from the database.
Re: Please Help Me With Php Code To Submit Picture To Database by FRInteractives: 4:24pm On Mar 27, 2015
Well said bro
aikfrank:
If its a test then you can go ahead and debug, else if its a major project, do not post images to database.


Best practice: store images using the file system. Submitting the form will simply upload the image to a folder, and store the part in the database, then you simply retrieve the part from the database.


Re: Please Help Me With Php Code To Submit Picture To Database by thewebcraft(m): 4:42pm On Mar 27, 2015
OPEYEMIAD:
I am currently working on my project Studeñt information management system. Have created other entries and they all submited to my database successfully using Dreamweaver, phpmyadmin and aphache.

Now remain to submit passport, my data type is blob on mysql but I can't submit as blob from dreamweaver. I can only submit as text.

please help me out gurus in d house

Submit Image path to db or full Image?
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 8:26pm On Mar 27, 2015
FRInteractives:
Please post the code you are using let's see where the bugs are
That means I will have to post the whole php and HTML code cause dreamweaver generate the code for me.
I will do that thanks
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 8:29pm On Mar 27, 2015
aikfrank:
If its a test then you can go ahead and debug, else if its a major project, do not post images to database.


Best practice: store images using the file system. Submitting the form will simply upload the image to a folder, and store the part in the database, then you simply retrieve the part from the database.

it is my final year project titled Development of a web base student management information system
please how do I use file system? thanks for ur concern

Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 8:31pm On Mar 27, 2015
thewebcraft:


Submit Image path to db or full Image?
full image
The image are passport, letter of undertaking, certificate of origin and declaration of age
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 8:33pm On Mar 27, 2015
Thanks all still not yet solve
please note that if I use insert command within my database it worked perfectly but to submit from web interface is the problem
Re: Please Help Me With Php Code To Submit Picture To Database by onyengbu: 6:08am On Mar 28, 2015
OPEYEMIAD:
Thanks all still not yet solve
please note that if I use insert command within my database it worked perfectly but to submit from web interface is the problem
Your problem is common with beginners because you will want to insert image data into the database just like you inserted every other thing in your form. Even though that is possible, it is not the best approach as you will soon find out in your web development journey.
The better approach as someone has already pointed out is to "UPLOAD" the image file to a directory (folder) in your server, give it a unique name and then save the path (link) to the uploaded image inside your database alongside other details from your form. So that instead of a blob data type, you may create a "text" or "varchar" data type for your image in the table.
With this method, you are not saving the raw image data in the database but the "link" to the image so that when you want to display the image anywhere, you you write you normal <img tag and where you will put the src="" part, you put the "link" you saved in the database there and by so doing, you display the image from the "folder" where it was uploaded.

The summary of the simplest approach to the problem is:

1. Within your form submit script, create a file upload function to check if a picture is attached in the form and if so upload it to a folder in your sever

2. The function above should also be able to get details of the uploaded image like name, size and type. Get the name of the image and join it to the PATH to the upload folder and that becomes the image path.

3. Insert the image path alongside other student details collected from the form to the table. For e.g, if the name of your folder is "uploads" and the name of image being attached is "picture.jpg"; what will be saved in your database becomes "uploads/picture.jpg"

All these might appear confusing but just with a few questions and little bit of googling, you will be good to go.

1 Like 1 Share

Re: Please Help Me With Php Code To Submit Picture To Database by Guyson(m): 7:51am On Mar 28, 2015
They advised you correctly but if youu still want to do it that way, your insert statement will be

Example

$sql = "INSERT INTO tableName(image, name) VALUES ('{$imgData}','{$_FILES['userfile']['name']}');";
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 8:48am On Mar 29, 2015
onyengbu:

Your problem is common with beginners because you will want to insert image data into the database just like you inserted every other thing in your form. Even though that is possible, it is not the best approach as you will soon find out in your web development journey.
The better approach as someone has already pointed out is to "UPLOAD" the image file to a directory (folder) in your server, give it a unique name and then save the path (link) to the uploaded image inside your database alongside other details from your form. So that instead of a blob data type, you may create a "text" or "varchar" data type for your image in the table.
With this method, you are not saving the raw image data in the database but the "link" to the image so that when you want to display the image anywhere, you you write you normal <img tag and where you will put the src="" part, you put the "link" you saved in the database there and by so doing, you display the image from the "folder" where it was uploaded.

The summary of the simplest approach to the problem is:

1. Within your form submit script, create a file upload function to check if a picture is attached in the form and if so upload it to a folder in your sever

2. The function above should also be able to get details of the uploaded image like name, size and type. Get the name of the image and join it to the PATH to the upload folder and that becomes the image path.

3. Insert the image path alongside other student details collected from the form to the table. For e.g, if the name of your folder is "uploads" and the name of image being attached is "picture.jpg"; what will be saved in your database becomes "uploads/picture.jpg"

All these might appear confusing but just with a few questions and little bit of googling, you will be good to go.
hmmmmm sound simple but difficult to implement. please note that this project is not computerize I.e software but web base whereby student will supply information themselves.

I do as you said but it only save the directory and when I click on it. it display bulk of codes. This project will display acknowledgement slip for each student containing their passport. These are instruction from my supervisor.

I appreciate your concern
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 8:50am On Mar 29, 2015
Guyson:
They advised you correctly but if youu still want to do it that way, your insert statement will be

Example

$sql = "INSERT INTO tableName(image, name) VALUES ('{$imgData}','{$_FILES['userfile']['name']}');";

error message "passport can not be null " even when I upload pix
Re: Please Help Me With Php Code To Submit Picture To Database by onyengbu: 9:12am On Mar 29, 2015
OPEYEMIAD:

hmmmmm sound simple but difficult to implement. please note that this project is not computerize I.e software but web base whereby student will supply information themselves.

I do as you said but it only save the directory and when I click on it. it display bulk of codes. This project will display acknowledgement slip for each student containing their passport. These are instruction from my supervisor.

I appreciate your concern

Can you post your script here?
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 9:18am On Mar 30, 2015
HERE IS MY SCRIPT

<?php
mb_http_input("iso-8859-1"wink;
mb_http_output("iso-8859-1"wink;
?>
<?php require_once('Connections/SIMSDatabase.php'); ?>
<?php
if (!function_exists("GetSQLValueString"wink) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""wink
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string"wink ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != ""wink ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != ""wink ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != ""wink ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != ""wink ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != ""wink ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"wink) {
$insertSQL = sprintf("INSERT INTO photograph (RegNo, Passport, Undertaking, Sorigin, Addletter, declaration, Olevel) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['RegNo'], "text"wink,
GetSQLValueString($_POST['Passport'], "text"wink,
GetSQLValueString($_POST['Undertaking'], "text"wink,
GetSQLValueString($_POST['Sorigin'], "text"wink,
GetSQLValueString($_POST['Addletter'], "text"wink,
GetSQLValueString($_POST['Declaration'], "text"wink,
GetSQLValueString($_POST['Olevel'], "text"wink);

mysql_select_db($database_SIMSDatabase, $SIMSDatabase);
$Result1 = mysql_query($insertSQL, $SIMSDatabase) or die(mysql_error());

$insertGoTo = "congratulation.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_SIMSDatabase, $SIMSDatabase);
$query_photograph = "SELECT * FROM photograph";
$photograph = mysql_query($query_photograph, $SIMSDatabase) or die(mysql_error());
$row_photograph = mysql_fetch_assoc($photograph);
$totalRows_photograph = mysql_num_rows($photograph);
?>

</style>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<meta name="save" content="" />
<meta name="keywords" content="adeleke" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="" />
<script src="jQueryAssets/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
</head>

<body>
<div id="Layer19">
<table width="100%" border="1">
<tr>
<td><form action="<?php echo $editFormAction; ?>" id="form1" name="form1" enctype="multipart/form-data" method="POST">
<p> NOTE: All your document should not be more than 60kb</p>
<p>&nbsp;</p>
<p>
<label for="Passport">Passport</label>
<input name="Passport" type="file" id="Passport" size="40" />
</p>
<p>
<label for="Undertaking">Letter of Undertaking</label>
<input name="Undertaking" type="file" id="Undertaking" size="40" />
</p>
<p>
<label for="Sorigin">Certificate of Origin</label>
<input name="Sorigin" type="file" id="Sorigin" size="40" />
</p>
<p>
<label for="Addletter">Admission Letter</label>
<input name="Addletter" type="file" id="Addletter" size="40" />
</p>
<p>
<label for="Declaration">Certicate of birth/declaration of age</label>
<input name="Declaration" type="file" id="Declaration" size="30" />
</p>
<p>O Level Result
<input name="Olevel" type="file" id="Olevel" size="30" />
</p>
<p>
<label for="RegNo">Reg No</label>
<span id="sprytextfield1">
<input type="text" name="RegNo" id="RegNo" />
<span class="textfieldRequiredMsg">Please Insert Your Registration Number.</span></span></p>
<p>&nbsp;</p>
<p>
<input type="radio" name="radio" id="radio" value="radio" />
<label for="radio"></label>
I hereby certify that to the best of my knowledge and belief, that the information given as above are correct. </p>
<p>&nbsp;</p>
<p>
<input name="Submit" type="submit" class="style15" id="Submit" value="Upload" />
</p>
<input type="hidden" name="MM_insert" value="form1" />
</form></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>
<div class="style12" id="Layer1"> <marquee>
<span class="style12"> <span style="font-size: 24px; color: #0000FF;">.::::STUDENTS INFORMATION MANAGEMENT SYSTEM::::.</span></span>
</marquee></div>
<p><img src="Picture/p1.jpg" width="1200" height="242" alt="PORTAL" /></p>
<p>&nbsp;</p>
<div id="Layer21"><a href="homepage.php">LOG OUT</a> </div>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1"wink;
</script>
</body>
</html>
<?php
mysql_free_result($photograph);
?>
Re: Please Help Me With Php Code To Submit Picture To Database by thewebcraft(m): 9:23am On Mar 30, 2015
OPEYEMIAD:
HERE IS MY SCRIPT

<?php
mb_http_input("iso-8859-1"wink;
mb_http_output("iso-8859-1"wink;
?>
<?php require_once('Connections/SIMSDatabase.php'); ?>
<?php
if (!function_exists("GetSQLValueString"wink) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""wink
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string"wink ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != ""wink ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != ""wink ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != ""wink ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != ""wink ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != ""wink ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"wink) {
$insertSQL = sprintf("INSERT INTO photograph (RegNo, Passport, Undertaking, Sorigin, Addletter, declaration, Olevel) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['RegNo'], "text"wink,
GetSQLValueString($_POST['Passport'], "text"wink,
GetSQLValueString($_POST['Undertaking'], "text"wink,
GetSQLValueString($_POST['Sorigin'], "text"wink,
GetSQLValueString($_POST['Addletter'], "text"wink,
GetSQLValueString($_POST['Declaration'], "text"wink,
GetSQLValueString($_POST['Olevel'], "text"wink);

mysql_select_db($database_SIMSDatabase, $SIMSDatabase);
$Result1 = mysql_query($insertSQL, $SIMSDatabase) or die(mysql_error());

$insertGoTo = "congratulation.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_SIMSDatabase, $SIMSDatabase);
$query_photograph = "SELECT * FROM photograph";
$photograph = mysql_query($query_photograph, $SIMSDatabase) or die(mysql_error());
$row_photograph = mysql_fetch_assoc($photograph);
$totalRows_photograph = mysql_num_rows($photograph);
?>

</style>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<meta name="save" content="" />
<meta name="keywords" content="adeleke" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="" />
<script src="jQueryAssets/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
</head>

<body>
<div id="Layer19">
<table width="100%" border="1">
<tr>
<td><form action="<?php echo $editFormAction; ?>" id="form1" name="form1" enctype="multipart/form-data" method="POST">
<p> NOTE: All your document should not be more than 60kb</p>
<p>&nbsp;</p>
<p>
<label for="Passport">Passport</label>
<input name="Passport" type="file" id="Passport" size="40" />
</p>
<p>
<label for="Undertaking">Letter of Undertaking</label>
<input name="Undertaking" type="file" id="Undertaking" size="40" />
</p>
<p>
<label for="Sorigin">Certificate of Origin</label>
<input name="Sorigin" type="file" id="Sorigin" size="40" />
</p>
<p>
<label for="Addletter">Admission Letter</label>
<input name="Addletter" type="file" id="Addletter" size="40" />
</p>
<p>
<label for="Declaration">Certicate of birth/declaration of age</label>
<input name="Declaration" type="file" id="Declaration" size="30" />
</p>
<p>O Level Result
<input name="Olevel" type="file" id="Olevel" size="30" />
</p>
<p>
<label for="RegNo">Reg No</label>
<span id="sprytextfield1">
<input type="text" name="RegNo" id="RegNo" />
<span class="textfieldRequiredMsg">Please Insert Your Registration Number.</span></span></p>
<p>&nbsp;</p>
<p>
<input type="radio" name="radio" id="radio" value="radio" />
<label for="radio"></label>
I hereby certify that to the best of my knowledge and belief, that the information given as above are correct. </p>
<p>&nbsp;</p>
<p>
<input name="Submit" type="submit" class="style15" id="Submit" value="Upload" />
</p>
<input type="hidden" name="MM_insert" value="form1" />
</form></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>
<div class="style12" id="Layer1"> <marquee>
<span class="style12"> <span style="font-size: 24px; color: #0000FF;">.::::STUDENTS INFORMATION MANAGEMENT SYSTEM::::.</span></span>
</marquee></div>
<p><img src="Picture/p1.jpg" width="1200" height="242" alt="PORTAL" /></p>
<p>&nbsp;</p>
<div id="Layer21"><a href="homepage.php">LOG OUT</a> </div>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1"wink;
</script>
</body>
</html>
<?php
mysql_free_result($photograph);
?>
Can write this code in 20 lines..Na wa oh! for Image Upload?........ and besides you dont comment your code.
Re: Please Help Me With Php Code To Submit Picture To Database by onyengbu: 9:37am On Mar 30, 2015
thewebcraft:

Can write this code in 20 lines..Na wa oh! for Image Upload?........ and besides you dont comment your code.
Ol boy, he is not here for reviews. So keep your bragging for someone else.


OPEYEMIAD:
HERE IS MY SCRIPT

Let me go through it...

Edit:
All your form inputs but one is file upload and your script do not have a file upload script. If you get any submissions in the database at all it will just be names of the files attached and not the actual file. There are a couple of questions that will help anyone to work on your script. Like:

What exactly are you trying to submit the files or the file names? What will students or staff do with submitted files?
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 1:02pm On Mar 30, 2015
onyengbu:

Ol boy, he is not here for reviews. So keep your bragging for someone else.




Let me go through it...

Edit:
All your form inputs but one is file upload and your script do not have a file upload script. If you get any submissions in the database at all it will just be names of the files attached and not the actual file. There are a couple of questions that will help anyone to work on your script. Like:

What exactly are you trying to submit the files or the file names? What will students or staff do with submitted files?

Thanks for your response.
i have file upload here is the code:


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"wink) {
$insertSQL = sprintf("INSERT INTO photograph (RegNo, Passport, Undertaking, Sorigin, Addletter, declaration, Olevel) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['RegNo'], "text"wink,
GetSQLValueString($_POST['Passport'], "text"wink,
GetSQLValueString($_POST['Undertaking'], "text"wink,
GetSQLValueString($_POST['Sorigin'], "text"wink,
GetSQLValueString($_POST['Addletter'], "text"wink,
GetSQLValueString($_POST['Declaration'], "text"wink,
GetSQLValueString($_POST['Olevel'], "text"wink);

mysql_select_db($database_SIMSDatabase, $SIMSDatabase);
$Result1 = mysql_query($insertSQL, $SIMSDatabase) or die(mysql_error());

$insertGoTo = "congratulation.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_SIMSDatabase, $SIMSDatabase);
$query_photograph = "SELECT * FROM photograph";
$photograph = mysql_query($query_photograph, $SIMSDatabase) or die(mysql_error());
$row_photograph = mysql_fetch_assoc($photograph);
$totalRows_photograph = mysql_num_rows($photograph);
?>

To answer your questions
1. I am trying to submitted the file itself but someone suggested file reference (i.e to create a folder and link it to db) anyone that work for me i will take it for the purpose of this project

2. Title "development of student information management system a case study of my school" student information can only be access by staff. this project is to remove the manual method of registration in my school. (i.e paper record) with web base student can upload there information online and School management can get access to it.

please dont mind my typo

Please note "photograph" is my database name it contains 7 tables namely:
1. Registration number - regno
2. passport photograph to be uploaded by student - passport
3. Declaration of age - Scan copy
4. O level result - olevel
5. Certificate of origin - Sorigin
6. Admission letter - addletter
7. Letter of under taking - undertaking
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 1:03pm On Mar 30, 2015
thewebcraft:

Can write this code in 20 lines..Na wa oh! for Image Upload?........ and besides you dont comment your code.
actually it is dreamweaver that generate the code for me i can only explain little about it.

please find time to review my script
Re: Please Help Me With Php Code To Submit Picture To Database by Dlastofmykind(m): 1:11pm On Mar 31, 2015
I'm also a beginner at web development, but with the little experience that I've had, starting web development with dreamweaver IDE will do you more harm than good.
Peradventure, you wanna code or you wanna correct someone's else code and you're unable to get access to dreamweaver, does that means that you'll not be able to code?
Even if u wanna use dreamweaver, I'll advice you've gotten the basic concepts of all these web languages wella, then dreamweaver will make your work faster.

I guess the above code that you pasted was generated for you by dreamweaver.
Anyway, you can save yourself the headache of trying to figure out the function of each line of the above code with this little explanation of mine.

If you wanna upload any file, these are the procedures:

1). In your HTML form code, ensure that you include this attribute 'enctype='multipart/form-data'.
The form code can then look like this : <form action='' method='post' enctype='multipart/form-data'>

2). set a name for your HTML input type='file' ... I mean in this format : <input type='file' name='photo'/>.
Change the 'photo' to any name of your choice.

3). In your PHP code, to get the name of the file selected, you can use declare a variable for that and use $_FILES to get the name and the tmp name.
e.g $tmp_name = $_FILES['photo']['tmp_name'];
$file_selected = $_FILES['photo']['name'];

where 'photo' is the name set for your input type='file'.

4). Declare a variable for the destination (folder) that the file will be uploaded to.
e.g $folder = 'avatars/';

5). Then, declare a variable for the file path to be saved in the database or in the folder depending on your choice with the extension that you want to use.
e.g $path = $file_selected.'.jpg';

NOTE: If you do this, the file name saved on the client's device (pc or phone) will be the name to be saved into the database.
If you wanna change the name to your desired own, there are many methods for doing that.
You can let it be the logged username or you can generate any unique random numbers.

6). Then, to upload the file to the folder that you specified, u can use move_uploaded_file(parameter1, parameter2) function.
where parameter1 is the tmp location and parameter2 is the path.
e.g move_uploaded_file($tmp_name, $path);

7). Then use your sql insert query to save the path to the db.
e.g "INSERT INTO `files` (path) VALUES('$path')"


If you follow these 7 steps appropriately, I'm very sure that your problem will get solved.

There are other things that can be done to this, like restricting the file to be uploaded to accept only pictures, or videos, or musics, resizing the picture to a particular size before been uploaded, checking if the destination folder exists, making sure that the file to be uploaded does not exceed a particular size.
Your PHP manual is your best friend and you can as well get that in google, but don't always resort to googling for source codes, it'll make you lazy programmer.

CHEERS!!!
Re: Please Help Me With Php Code To Submit Picture To Database by Dlastofmykind(m): 1:12pm On Mar 31, 2015
Re: Please Help Me With Php Code To Submit Picture To Database by OPEYEMIAD(m): 3:05pm On Apr 01, 2015
Dlastofmykind:
I'm also a beginner at web development, but with the little experience that I've had, starting web development with dreamweaver IDE will do you more harm than good.
Peradventure, you wanna code or you wanna correct someone's else code and you're unable to get access to dreamweaver, does that means that you'll not be able to code?
Even if u wanna use dreamweaver, I'll advice you've gotten the basic concepts of all these web languages wella, then dreamweaver will make your work faster.

I guess the above code that you pasted was generated for you by dreamweaver.
Anyway, you can save yourself the headache of trying to figure out the function of each line of the above code with this little explanation of mine.

If you wanna upload any file, these are the procedures:

1). In your HTML form code, ensure that you include this attribute 'enctype='multipart/form-data'.
The form code can then look like this : <form action='' method='post' enctype='multipart/form-data'>

2). set a name for your HTML input type='file' ... I mean in this format : <input type='file' name='photo'/>.
Change the 'photo' to any name of your choice.

3). In your PHP code, to get the name of the file selected, you can use declare a variable for that and use $_FILES to get the name and the tmp name.
e.g $tmp_name = $_FILES['photo']['tmp_name'];
$file_selected = $_FILES['photo']['name'];

where 'photo' is the name set for your input type='file'.

4). Declare a variable for the destination (folder) that the file will be uploaded to.
e.g $folder = 'avatars/';

5). Then, declare a variable for the file path to be saved in the database or in the folder depending on your choice with the extension that you want to use.
e.g $path = $file_selected.'.jpg';

NOTE: If you do this, the file name saved on the client's device (pc or phone) will be the name to be saved into the database.
If you wanna change the name to your desired own, there are many methods for doing that.
You can let it be the logged username or you can generate any unique random numbers.

6). Then, to upload the file to the folder that you specified, u can use move_uploaded_file(parameter1, parameter2) function.
where parameter1 is the tmp location and parameter2 is the path.
e.g move_uploaded_file($tmp_name, $path);

7). Then use your sql insert query to save the path to the db.
e.g "INSERT INTO `files` (path) VALUES('$path')"


If you follow these 7 steps appropriately, I'm very sure that your problem will get solved.

There are other things that can be done to this, like restricting the file to be uploaded to accept only pictures, or videos, or musics, resizing the picture to a particular size before been uploaded, checking if the destination folder exists, making sure that the file to be uploaded does not exceed a particular size.
Your PHP manual is your best friend and you can as well get that in google, but don't always resort to googling for source codes, it'll make you lazy programmer.

CHEERS!!!

Thankx I will give it a try
Re: Please Help Me With Php Code To Submit Picture To Database by romme2u: 1:20am On Apr 03, 2015
OPEYEMIAD:
I am currently working on my project Studeñt information management system. Have created other entries and they all submited to my database successfully using Dreamweaver, phpmyadmin and aphache.

Now remain to submit passport, my data type is blob on mysql but I can't submit as blob from dreamweaver. I can only submit as text.

please help me out gurus in d house


as at CS5, dreamweaver cannot handle blob (binary large object) data type processing and file uploading. YOU have to write the code manually to access the uploaded file in php and process it as u want. u can store it in the database or in the filesystem, let the arguments for the experts as they are pros and cons. so get ur hands dirty with and best of luck.
Re: Please Help Me With Php Code To Submit Picture To Database by micodon(m): 12:08am On Apr 04, 2015
Do not upload files to your database, dude. upload the file to your file storage, store the path in ur db. whenever you want to use the file, get the path from db and link to it directly.

Dont Upload Files to your database. that's very bad practice
Re: Please Help Me With Php Code To Submit Picture To Database by adewasco2k(m): 9:18am On Apr 04, 2015
omg! can you just use Google? so many tutorials online or just search stackoverflow.

damn! i cant even go through those codes, its paining my eyes grin

(1) (Reply)

Get All Web Tools For Free And Earn Unlimited Income At The Same Time / Naija18.com Please Review / Blogging: How Google Meta Tags Affect SEO

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 122
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.