Jump to content

Images in php and mysql?


Woody777

Recommended Posts

Good Day 

 

Here is the code of my readmore page where everything is displayed right?

 

 $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM properties where PropertyId = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($propertyid));
        $dataproperty = $q->fetch(PDO::FETCH_ASSOC);
 
 
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql1 = "SELECT * FROM images WHERE PropertyId = ?";
        $q1 = $pdo->prepare($sql1);
        $q1->execute(array($propertyid));
        
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql2 = "SELECT * FROM agent WHERE PropertyId = ?";
        $q2 = $pdo->prepare($sql2);
        $q2->execute(array($propertyid));
        
        }
    
                              echo '<div class="col-md-12">';
 
                                while ($row = $q1->fetch(PDO::FETCH_ASSOC))
                            {
                                    
                                echo '<a href="'.$row['ImageUrl'].'" data-lightbox="images"><img width="200px" src="'.$row['ImageUrl'].'"></a>';
               
                             }
                            echo '<div class="proj_descr1"><h1>'. $dataproperty['Title'] . '</h1></div>';
                                                        
                            echo '<div class="proj_descr_txt">'.'<h4><b>Description: </b></h4>'. $dataproperty['Description'] . '</div>';
                             
But here I have a issue, because here is to display my images of the agents from my agents disrectory:
 
                            while ($row2 = $q2->fetch(PDO::FETCH_ASSOC))
                            {
                                echo '<img width="200" scr="'.$row2['AgentImage'].'">';
      
                            }
                            echo '<div class="proj_descr_txt">'.'<h4><b>Contact Details: </b></h4>'. $dataproperty['ContactDetails'] . '</div>';
 
And here I have the code of my create page where my properties get created, the last 2 bits of code is for my images uploads but it needs to upload it to separate directories like it is indicated:
 
 $pdo = Database::connect();
 
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO properties (title,description,country,location,status,type,bedrooms,bathrooms,parking,price,fromthe,contactdetails) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            $q = $pdo->prepare($sql);
            $q->execute(array($title,$description,$country,$location,$status,$type,$bedrooms,$bathrooms,$parking,$price,$fromthe,$contactdetails));
            Database::disconnect();
 
        }
 
       $last_id = $pdo->lastInsertId();
       $directory="uploads/";
$a=0;
 
        foreach ($_FILES['file']['name'] as $nameFile) {
 
 
        $ext = pathinfo($nameFile, PATHINFO_EXTENSION);
            if(is_uploaded_file($_FILES['file']['tmp_name'][$a]))
            {
                move_uploaded_file($_FILES['file']['tmp_name'][$a], $directory.$last_id."_".$a.".".$ext);
            }
            $fileandpath = $directory.$last_id."_".$a.".".$ext;
 
            $pdo1 = Database::connect();
            $pdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql1 = "INSERT INTO images (propertyid,imageurl) values(?, ?)";
            $q1 = $pdo1->prepare($sql1);
            $q1->execute(array( $last_id,$fileandpath));
            Database::disconnect();
            $a++;
        }
        $last_id1 = $pdo->lastInsertId1();
        $directory1="agents/";
        $a1=0;
 
        foreach ($_FILES1['file']['name'] as $nameFile1) {
 
 
            $ext1 = pathinfo($nameFile1, PATHINFO_EXTENSION);
            if(is_uploaded_file($_FILES1['file']['tmp_name'][$a1]))
            {
                move_uploaded_file($_FILES1['file']['tmp_name'][$a1], $directory1.$last_id1."_".$a1.".".$ext1);
            }
            $fileandpath1 = $directory1.$last_id1."_".$a1.".".$ext1;
 
            $pdo2 = Database::connect();
            $pdo2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql2 = "INSERT INTO agent (propertyid,agentimage) values(?, ?)";
            $q2 = $pdo2->prepare($sql2);
            $q2->execute(array($last_id1,$fileandpath1));
            Database::disconnect();
            $a1++;
        }
          header("Location: admin_list.php");
      }
 
Here I have another code from my create page:
 
<div class="control-group">
                        <label class="control-label">Contact Details:</label>
                        <div class="controls">
                            <textarea name="contactdetails" type="text" placeholder="ContactDetails"  cols="30" rows="10" value="<?php echo !empty($contactdetails)?$contactdetails:'';?>"></textarea>
                            <?php if (!empty($contactdetailsError)): ?>
                                <span class="help-inline"><?php echo $contactdetailsError;?></span>
                            <?php endif;?>
                           
                        </div>
                        </div>
 
This code is for the images, the Agent Images is for the photo of the agent and there company logo:
 
                    <div class="control-group">
                        <label class="control-label">Agent Images:</label>
                        <div class="controls">
                         <input type="file" name="file[]" multiple><br />
 
                        </div>
                      </div>
 
This code is for the images of the properties:
 
                            <div class="control-group">
                        <label class="control-label">Images:</label>
                        <div class="controls">
                         <input type="file" name="file[]" multiple><br />
 
                        </div>
                      </div>
 
So the problem I am having is that I can't get the Agent Images to upload to the Agent directory and can't display it above my contact details, the screen shot image is in the attached, please help me. Everything works perfectly but I only need to get that images right for the Agents above the contact details.
                            

post-168943-0-46114700-1402590592_thumb.png

Link to comment
Share on other sites

some points about your code -

 

1) Please use the forum's


bbcode tags (the edit form's <> button) around code when posting it in the forum.
 

2) you have repeated code in several places, your database connection and the ->setAttribute() statement (which should actually be in with your connection code). you are also opening/closing your database connection inside of loops. in addition to cluttering up your code with unnecessary lines of code that make it harder for us to even see what your code is doing, this repetitive opening and closing database connections in one instance of your script results in a noticeable performance loss. you should open one database connection and use that one connection throughout the code.

 

3) in the cases where you need to run a prepared query inside of a loop, the prepare statement belongs outside of and before the loop. the only thing inside the loop is the code that gets each set of data values and the ->execute(...) statement.

 

4) you need to solve one problem at a time and only post the code relevant to one problem at a time. if your upload is not working, there's no point in trying to display anything. your upload form(s), assuming they are on the same page, need different names in the name='...' attribute for the different purposes so that you will know what to do with the different types of uploaded images - agent, logo, and property. your upload form processing code is trying to use a nonexistent php variable -  $_FILES1. $_FILES is the only variable name that php puts uploaded file data into. also, i'm guessing there can only be multiple images for the properties? that would be the only field you would use an array name for.

Edited by mac_gyver
spelling
Link to comment
Share on other sites

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.