Jump to content

Recommended Posts

im creating simple blog site, in creating new page, i have insert image gallery too. page and gallery both have two different table as i couldn’t figure out how to insert gallery into single table (means gallery section in pages table, so i created two tables)

What im trying to achieve in update pages is that check if image is insert or not in update page. if image is insert then display image else allow user to upload image in update page

table_name = english_version (table for page)

5555.png.657c2901b6e35acc17dda8b9e285278a.png

table_name = english_gallery (table for gallery)

222.png.7fedc5729587863c56cbe17b35f09249.png

*Where image_id = id (id from english_version)

so when user creates new page and sumbit, it is send to update page:
header(“Location:./update.php?update=$lastenterid”);
die();

so, if user forget to insert image for gallery then i want to give option for image upload in update pages…

if(isset($_GET['update'])){
    $update_id = intval($_GET['update']);
   
    $update = $conn->query("SELECT * FROM english_version WHERE id = $update_id ");
    while($rows = $update->fetch(PDO::FETCH_OBJ) ):
        $id = $rows->id;
        $title = $rows->title;
        $content = $rows->content;
    endwhile;
}

checking if image is insert or not

<form action="" method="post">
    <!-- english-version -->
    <input type="hidden" name="id" value="<?php echo $id;?>">
    
    <div class="form-group">
        <input type="text" name="title" class="form-control mt-3" value="<?php echo $title?>">
    </div>

    <div class="form-group">
        <textarea class="form-control" name="content" id="" cols="30" rows="10"><?php echo $content;?></textarea>
    </div>
    <div class="form-group">
            <?php 
//getting data from english_gallery (table for gallery)
            $gallery = $conn->query("SELECT * FROM english_gallery WHERE image_id = $update_id");
            while($rows_gal = $gallery->fetch(PDO::FETCH_OBJ) ):
                $id = $rows_gal->gallery_id;
                $image_id = $rows_gal->image_id;
                $image = $rows_gal->image;

                //check if image_id == $update_id (where $update is id of (primary_key) english_version (table for page))
                if($image_id == $update_id ){ ?>
                    <div class="image-gallery">
                        <img src="<?php echo $image;?>" alt="">
                    </div>
		            <?php        
		                }else{
		                    $targetDir = "image/";
		                    $allowTypes = array('jpg','png','jpeg','gif');
		                    
		                    $images_arr = array();
		                    foreach($_FILES['images']['name'] as $key=>$val){
		                        $image_name = $_FILES['images']['name'][$key];
		                        $tmp_name 	= $_FILES['images']['tmp_name'][$key];
		                        $size 		= $_FILES['images']['size'][$key];
		                        $type 		= $_FILES['images']['type'][$key];
		                        $error 		= $_FILES['images']['error'][$key];
		                        
		                        // File upload path
		                        $fileName = basename($_FILES['images']['name'][$key]);
		                        $targetFilePath = $targetDir . $fileName;
		                        
		                        // Check whether file type is valid
		                        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
		                        if(in_array($fileType, $allowTypes)){	
		                            // Store images on the server
		                            if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$targetFilePath)){
		                                $images_arr[] = $targetFilePath;
		                                
		                                $insert = $conn->query("INSERT into english_gallery (image_id,image) VALUES ('$update_id','$targetFilePath')");

		                                $galleryid = $conn->lastInsertId();

		                                if($insert){
		                                    $count = $key + 1;
		                                    $statusMsg = " ".$count. " image file has been uploaded successfully.";
		                                }else{
		                                    $statusMsg = "Failed to upload image";
		                                } 
		                                
		                            }else{
		                                $statusMsg = "Sorry, there was an error uploading your file.";
		                            }
		                        }else{
		                            $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.';
		                        }
		                    }//multiple_image upload starts
		            ?>        
		          <!-- if image_id !== $update then display multi-image upload section --> 
	              	<div class="form-group">
	                    <input type="file" name="images[]" multiple>
	                </div> 
	            <?php
	                }

	                endwhile;  
	            ?>
    </div>

    <div class="form-group">
       <input type="submit" value="Update" name="update" class="btn btn-primary">
    </div>  
    <!-- english-version -->
</form>
Quote

if($image_id == $update_id ){

if there is image its displaying image but if there no image its not displaying input option for image upload

rr.png.7cb1dc4b437cd3703442bfa0cdbcf4f2.png

i think else conditions is not working because $image_id stores id of (primary_key) english_version (table for page)) it’s checking for the id which is never created…

so, how can i get the condition, where if user have insert image then, it will image will displayed in update page else user will get option for image uploading…

Join your tables in q query, matching content with image.

Where there is no image (denoted by NULL) output a file input to upload an image.

english_version                        english_gallery
+----+---------+--------------+        +------------+----------+------------+
| id | title   | content      |        | gallery_id | image_id | image      |
+----+---------+--------------+        +------------+----------+------------+
|  1 | Ttile 1 | aaaaaaaa     |        |          1 |        1 | ab1234.jpg |
|  2 | Title 2 | bbbbbb       |        |          2 |        3 | ab2345.jpg |
|  3 | Title 3 | cccccccc     |        |          3 |        5 | ab3456.jpg |
|  4 | Title 4 | dddddd       |        +------------+----------+------------+
|  5 | Title 5 | eeeeeeeeeeee |
+----+---------+--------------+


SELECT v.id
     , v.title
     , v.content
     , g.image
FROM english_version v
     LEFT JOIN 
     english_gallery g ON v.id = g.image_id
ORDER BY v.id;


query results
+----+---------+--------------+------------+
| id | title   | content      | image      |
+----+---------+--------------+------------+
|  1 | Ttile 1 | aaaaaaaa     | ab1234.jpg |
|  2 | Title 2 | bbbbbb       | NULL       |   <input type='file' name='myimages[2]'>
|  3 | Title 3 | cccccccc     | ab2345.jpg |
|  4 | Title 4 | dddddd       | NULL       |   <input type='file' name='myimages[4]'>
|  5 | Title 5 | eeeeeeeeeeee | ab3456.jpg |
+----+---------+--------------+------------+

 

im on update page where im trying update content for particular 'id' only where im using get method....
 

Quote

if(isset($_GET['update'])){
        $update_id = intval($_GET['update']);

where im getting id from url with $update_id, using that $update_id-> id in select query to pull data for that particular id.
$data = $conn->query("SELECT * FROM english_version WHERE id = $update_id ");

now for your solution:

SELECT v.id , v.title , v.content , g.image FROM english_version v LEFT JOIN english_gallery g ON v.id = g.image_id ORDER BY v.id;

where can i insert $update_id so that i get data for that id only

Note that my suggested file inputs contain the id in the input name

image.png.659d5c63227932fb9ee1c1e1058d3d76.png

From those two, $FILES array will look something like this, showing those ids as keys in the array. Use those when updating the table.

$_FILES = Array
(
    [myimages] => Array
        (
            [name] => Array
                (
                    [2] => ab4567.jpg
                    [4] => bc5678.jpg
                )

            [type] => Array
                (
                    [2] => image/png
                    [4] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [2] => C:\Windows\Temp\php9A0A.tmp
                    [4] => C:\Windows\Temp\php9A2A.tmp
                )

            [error] => Array
                (
                    [2] => 0
                    [4] => 0
                )

            [size] => Array
                (
                    [2] => 32769
                    [4] => 13741
                )

        )

)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.