twilkai Posted August 23, 2009 Share Posted August 23, 2009 guys please help me .. i dont know how to insert images in my database .. and display it.. please help me.. i have a question table and choices .. in question table i have answer and question .. how should i code in order to insert and display the images .. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 25, 2009 Share Posted August 25, 2009 Nowhere near enough info... but there are countless tutorials on inserting images into mysql. Quote Link to comment Share on other sites More sharing options...
russianbear Posted August 25, 2009 Share Posted August 25, 2009 Yo buddy. It's not hard. Indeed - there are a lot of tutorials on-line, but here is a good example for you to use...hope it helps. I inlcuded 2 different ways to insert an image. The first way is the OO approach. Second is the procedural way. Not sure how familiar you are with Object Oriented Programming, but that would be the way that I would do it - cleaner and more modular. Also make sure that in your db script you have declared your columns that hold the images as blob type. You can read about that here if you aren't familiar with it... http://dev.mysql.com/doc/refman/5.0/en/blob.html good luck. <?php // Open a connection to the DB //$conn = new mysqli("localhost", "lamp", "", "images"); /* echo "Thanks for uploading file :" . $_FILES['userfile']['name'] . "<br>"; echo "File size is : " . $_FILES['userfile']['size'] . "<br>"; echo "File type is : " . $_FILES['userfile']['type'] . "<br>"; echo "File tmp_name is : " . $_FILES['userfile']['tmp_name'] . "<br>"; */ //$stmt = $conn->prepare("INSERT INTO images VALUES(NULL, ?)"); //$stmt->bind_param("s", $data); //$file = $_FILES['userfile']['tmp_name']; //$fp = fopen($file, "r"); //$size = 0; //while ($data = fread($fp,1024)) { // $size += strlen($data); // $stmt->send_long_data(0,$data); //} //if ($stmt->execute()) { // print "$file ($size bytes) was added to the files table\n"; //} else { // die($conn->error); //} // // //echo '<html>'; //echo '<head></head>'; //echo '<body>'; //echo '<br><a href="download.php">Retrieve Image</a>'; //echo '</body>'; //echo '</html>'; //end oo approach // Begin procedural style // database connection mysql_connect("localhost", "lamp", '') OR DIE (mysql_error()); // select the db mysql_select_db ('images') OR DIE ("Unable to select db".mysql_error()); // prepare the image for insertion $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name'])); // our sql query $sql = "INSERT INTO images (image) VALUES ('{$imgData}')"; if(!mysql_query($sql)) { echo 'Unable to upload file'; } else { echo 'upload SUCCESS!'; } echo '<html>'; echo '<head></head>'; echo '<body>'; echo '<br><a href="download.php">Retrieve Image</a>'; echo '</body>'; echo '</html>'; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.