html - Show multiple picture from database php -
i wondering how show more 1 picture database on php document. know hoe put picture mysql , take out 1 of them don't know how take out multiple (or all). in advance.
one of php files looks this
<html> <head> <title>upload</title> </head> <form action='pictureuploadtest.php' method='post' enctype='multipart/form-data'> file: <input type='file' name='fileone'> <input type = 'submit' name='submitfile'> </form> <?php $con = mysql_connect("localhost", "username", "password") or die("cannot connect: ".mysql_error()); mysql_select_db("testpicture") or die("cannot connect db: ".mysql_error()); $file = $_files['fileone']['tmp_name']; if(!isset($file)) { print "choose image"; } else { $image = addslashes(file_get_contents($_files['fileone']['tmp_name'])); $imagename = addslashes($_files['fileone']['name']); $imagesize = getimagesize($_files['fileone']['tmp_name']); if($imagesize === false) { echo "invalid image."; } else { $insert = "insert upload values ('', '$imagename', '$image')"; if(mysql_query($insert, $con)) { $lastid = mysql_insert_id(); echo "image uploaded. <p /> image: <p /> <img src=getpic.php?id=$lastid width='300px' height='300px'>"; } else { echo "cannot upload image: ".mysql_error(); } } } ?> </html>
and getpic.php looks this
<?php mysql_connect("localhost", "username", "password") or die("cannot connect: ".mysql_error()); mysql_select_db("testpicture") or die("cannot connect db: ".mysql_error()); $id = addslashes($_request['id']); $image = mysql_query("select * upload id=$id"); $image = mysql_fetch_assoc($image); $image = $image['image']; echo $image; ?>
so code can tell user upload image show image after adding database, how can show multiple or pictures in database.
thanks in advance.
ok, here's very basic code can follow, please read bold note @ end.
you use multiple file inputs this:
<input type='file' name='fileone'> <input type='file' name='filetwo'> ...
and call insert each file uploaded (or preferably loop, more in line multiple inputs above):
$file = $_files['fileone']['tmp_name']; ... rest of insert code ... $file = $_files['filetwo']['tmp_name']; ... rest of insert code ...
and loop on select when pull images out:
while ($row = mysql_fetch_assoc($image)) { ... rest of fetch code ... }
but should never (almost never) store images in database!
images should stored on filesystem few reasons:
- the filesystem more efficient , faster: image on filesystem web server on system can directly pull images straight filesystem webserver out user. in database, it's more file system, through database layer, (possibly on network) web server , out user.
- you don't want bother database )which can sorts of awesome , impressive things joins , subselects - if it's not being wasted directly pulling big binary blobs of images) trivial storing , retrieving images.
- it's less efficient (on disk space) use database, though becoming less , less of issue storage costs approach 0.
Comments
Post a Comment