Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display all the images stored inside a database
    primarykey
    data
    text
    <p>I am making a gallery that uses a MySQL database (yeah I know it's a bad practice but it's the requirement for the moment.) I can upload multiple images but I'm having trouble displaying all images stored inside the database. The FORM allows five images to be uploaded. Then the user must proceed to another page where all the images in database (including the ones uploaded recently) will be displayed together with the description of the images. I have code already but the one that will work on the display is not working or I think is wrong.</p> <p>Here is the form code:</p> <pre><code> &lt;html&gt; &lt;head&gt; &lt;title&gt; Upload image&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div align="center"&gt; &lt;form action="fUpload.php" method="POST" enctype="multipart/form-data"&gt; All forms must be filled. &lt;br /&gt; File: &lt;br /&gt; &lt;input type="file" name="image[]"/&gt; &lt;input type="text" name="imageDescription[]" size="30" /&gt; &lt;br /&gt; &lt;input type="file" name="image[]"/&gt; &lt;input type="text" name="imageDescription[]" size="30" /&gt; &lt;br /&gt; &lt;input type="file" name="image[]"/&gt; &lt;input type="text" name="imageDescription[]" size="30" /&gt; &lt;br /&gt; &lt;input type="file" name="image[]"/&gt; &lt;input type="text" name="imageDescription[]" size="30" /&gt; &lt;br /&gt; &lt;input type="file" name="image[]"/&gt; &lt;input type="text" name="imageDescription[]" size="30" /&gt; &lt;br /&gt; &lt;input type="submit" value="Upload image" /&gt; &lt;/form&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here is the script that would upload:</p> <pre><code> &lt;?php //connect to the database// $con = mysql_connect("localhost","root", ""); if(!$con) { die('Could not connect to the database:' . mysql_error()); echo "ERROR IN CONNECTION"; } $sel = mysql_select_db("imagedatabase"); if(!$sel) { die('Could not connect to the database:' . mysql_error()); echo "ERROR IN CONNECTION"; } //file properties// $file = $_FILES['image']['tmp_name']; echo '&lt;br /&gt;'; /*if(!isset($file)) echo "Please select your images"; else { */for($count = 0; $count &lt; count($_FILES['image']); $count++) { //$image = file_get_contents($_FILES['image']['tmp_name']); $image_desc[$count] = addslashes($_POST['imageDescription'][$count]); $image_name[$count] = addslashes($_FILES['image]']['name'][$count]); echo '&lt;br \&gt;'; $image_size[$count] = @getimagesize($_FILES['image']['tmp_name'][$count]); $error[$count] = $_FILES['image']['error'][$count]; if($image_size[$count] === FALSE || ($image_size[$count]) == 0) echo "That's not an image"; else { // Temporary file name stored on the server $tmpName[$count] = $_FILES['image']['tmp_name'][$count]; // Read the file $fp[$count] = fopen($tmpName[$count], 'r'); $data[$count] = fread($fp[$count], filesize($tmpName[$count])); $data[$count] = addslashes($data[$count]); fclose($fp[$count]); // Create the query and insert // into our database. $results = mysql_query("INSERT INTO images( description, image) VALUES ('$image_desc[$count]','$data[$count]')", $con); if(!$results) echo "Problem uploding the image. Please check your database"; //else //{ echo ""; //$last_id = mysql_insert_id(); //echo "Image Uploaded. &lt;p /&gt; &lt;p /&gt;&lt;img src=display.php? id=$last_id&gt;"; //header('Lcation: display2.php?id=$last_id'); } //} } mysql_close($con); header('Location: fGallery.php'); ?&gt; </code></pre> <p>And finally the one that should display:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;/body&gt; &lt;?php //connect to the database// mysql_connect("localhost","root", "") or die(mysql_error()); mysql_select_db("imagedatabase") or die(mysql_error()); //requesting image id $id = addslashes($_REQUEST['id']); $image = mysql_query("SELECT * FROM images WHERE id = $id"); while($datum = mysql_fetch_array($image, MYSQL_ASSOC)) { printf("Description %s $image = $image['image']; header("Content-type: image/jpeg"); } mysql_close(); ?&gt; </code></pre> <p>Your help is much appreciated. I need it badly to move on.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload