By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

Storing Image Path in mySQL field

P: 4
I made a PHP form to add data to the mysql database and now I wan't to add photos (not to mysql db because I heard that's not a good ideea, so...) and list it with rest of the info from db I`v inserted with.

I made a upload form, but I don't know how to rename the file`s I upload, and make a new folder for each time I upload data + photo, and than list photo from folder with the rest of the data I`v inserted.


In other words...

New data added from form to db
Name, Age, Email, Description, Photo1, Photo2, Photo3.
Upload to db and list it when necessary.

Please help...
Best regards
Oct 29 '11 #1
Share this Question
Share on Google+
5 Replies


zorgi
Expert 100+
P: 431
To rename files you can use rename() function: http://php.net/manual/en/function.rename.php

Once you have your files referenced in your database, retrieve rows of data using MySql SELECT statement http://dev.mysql.com/doc/refman/5.0/en/select.html
Oct 29 '11 #2

P: 4
I did rename the files, but how I make referenced in my database?
thank you
Oct 30 '11 #3

zorgi
Expert 100+
P: 431
Same way as you would add anything into the database. I usually insert only unique name of the file. Of course you can store path to the file if you prefer that way.
Oct 30 '11 #4

P: 4
How can I upload 3-4 photos without copy-paste the entire code and how do I make references of new photos uploaded to insert in the db with the new data enter from 'inputImobil' and 'inputDescriere'?
Please help

This is the code that I`v combined for insert data to db and upload 1photo.


<?php

include 'connection.php';

$imobil = $_POST[inputImobil];
$desc = $_POST[inputDescriere];



define ("MAX_SIZE","1000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;


if(isset($_POST['submit'])) {
$image=$_FILES['image']['name'];
if ($image) {
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$newrecord1 = "<h1>Unknown extension!</h1>";
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
$image_name=time().'.'.$extension;
$newname="upload/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);

}}}
if(isset($_POST['submit']) && !$errors)
{
$newrecord2 = "<h1>File Uploaded Successfully! Try again!</h1>";
}

if(!$_POST['submit']) {
$newrecord3 = "Completeaza formularul";

}else{
mysql_query("INSERT INTO vanzare (`ID`,`Imobil`,`Descriere`)
VALUES(NULL,'$imobil','$desc')") or die(mysql_error());
$newrecord4 = "Adaugare reusita!";
header('Location: new5.php');
}

?>
Oct 30 '11 #5

P: 4
I`v insert the patch to db like this...

mysql_query("INSERT INTO vanzare (`ID`,`Imobil`,`Descriere`,`Telefon`)
VALUES(NULL,'$imobil','$desc','$newname')") or die(mysql_error());



How can I add 3-4 photos without double the code...
And how do I select the table where to add the data.

If select value="1" add data to table1 and if select value="2" add data to table2... All data from all fields.... (inputImobil, inputDescriere, Photo1, Photo2, etc...)
Oct 31 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.