Jump to content

Couple separated images insert and displaying them in table


idan

Recommended Posts

I'm trying to upload multi images to the database all in separated fields and displaying them in a table. Im able to upload them into the DB but the images are not displaying all I see is image icon?

this is the insert query

if(isset($_POST['save_contract']))
{
    $ID = $_POST['id'];
    $Fname = $_POST['fullname'];    


    $pcontract=$_FILES['pcontract']['name'];
    $img_loc=$_FILES['pcontract']['tmp_name'];
    $img_ext = pathinfo($pcontract,PATHINFO_EXTENSION);
    $img_des = "contracts/".$img_ext;
    move_uploaded_file($img_loc,$img_des);

    $passport=$_FILES['passport']['name'];
    $img_loc1=$_FILES['passport']['tmp_name'];
    $img_ext1 = pathinfo($passport,PATHINFO_EXTENSION);
    $img_des1 = "contracts/".$img_ext1;
    move_uploaded_file($img_loc1,$img_des1);

    $playerapproval=$_FILES['playerapproval']['name'];
    $img_loc2=$_FILES['playerapproval']['tmp_name'];
    $img_ext2 = pathinfo($playerapproval,PATHINFO_EXTENSION);
    $img_des2 = "contracts/".$img_ext2;
    move_uploaded_file($img_loc2,$img_des2);

    $testapproval=$_FILES['testapproval']['name'];
    $img_loc3=$_FILES['testapproval']['tmp_name'];
    $img_ext3 = pathinfo($testapproval,PATHINFO_EXTENSION);
    $img_des3 = "contracts/".$img_ext3;
    move_uploaded_file($img_loc3,$img_des3);
    
    $rent=$_FILES['rent']['name'];
    $img_loc4=$_FILES['rent']['tmp_name'];
    $img_ext4 = pathinfo($rent,PATHINFO_EXTENSION);
    $img_des4 = "contracts/".$img_ext4;
    move_uploaded_file($img_loc4,$img_des4);



    
    $insert = "INSERT INTO contracts(id, fullname, pcontract, passport, playerapproval, testapproval, rent) VALUES ('$ID','$Fname','$img_des','$img_des1','$img_des2','$img_des3','$img_des4')";

    $query_run = mysqli_query($conn, $insert);
    if($query_run)
    {
        $_SESSION['message'] = "contract Created Successfully";
        header("Location: Logistics.php");
        exit(0);
    }
    else
    {
        $_SESSION['message'] = "contract Not Created";
        header("Location: Logistics.php");
        exit(0);
    }
}

and this is the table body where i display the data from the DB again its just displaying image icon not the picture itself i store the image in a folder name contracts.

            <tbody>
                <?php 
                    $query = "SELECT * FROM contracts";
                    $query_run = mysqli_query($conn,$query);

                    if(mysqli_num_rows($query_run) > 0)
                    {
                        foreach($query_run as $row)
                        {
                            ?>
                            <tr>
                                <td><?= $row['id']; ?></td>
                                <td><?= $row['fullname']; ?></td>
                                <td><img src="<?= $row['pcontract']; ?>"width='64px'></td>
                                <td><img src="<?= $row['passport']; ?>"width='64px'></td>
                                <td><img src="<?= $row['playerapproval']; ?>"width='64px'></td>
                                <td><img src="<?= $row['testapproval']; ?>"width='64px'></td>
                                <td><img src="<?= $row['rent']; ?>"width='64px'></td>
                                <td>
                                    <a href="contract-view.php?id=<?= $row['id']; ?>" class="btn btn-info btn-sm">View</a>
                                    <a href="contract-edit.php?id=<?= $row['id']; ?>" class="btn btn-success btn-sm">Edit</a>
                                    <form action="contractcode.php" method="POST" class="d-inline">
                                        <button type="submit" name="delete_contract" value="<?=$row['id'];?>" class="btn btn-danger btn-sm">Delete</button>
                                    </form>
                                </td>
                            </tr>
                            <?php
                        }
                    }
                    else
                    {
                        echo "<h5> No Record Found </h5>";
                    }
                ?>
            </tbody> 

 

Link to comment
Share on other sites

Not a good idea to put into a table.  Simply have an image folder and put them there.  No need to go thru the work of saving them and then querying for them.

As for the display problem...

echo "<tbody>";
$query = "SELECT * FROM contracts";
$query_run = mysqli_query($conn,$query);
if(mysqli_num_rows($query_run) > 0)
{
	foreach($query_run as $row)
	{
		echo "
			<tr>
			<td>{$row['id']}</td>
			<td>{$row['fullname']}</td>
			<td><img src='{$row['pcontract']}' width='64px'></td>
			<td><img src='{$row['passport']}' width='64px'></td>
			<td><img src='{$row['playerapproval']}' width='64px'></td>
			<td><img src='{$row['testapproval']}' width='64px'></td>
			<td><img src='{$row['rent']}' width='64px'></td>
			<td>
			<a href='contract-view.php?id={$row['id']}' class='btn btn-info btn-sm'>View</a>
			<a href='contract-edit.php?id={$row['id']}' class='btn btn-success btn-sm'>Edit</a>
			<form action='contractcode.php' method='POST class='d-inline'>
			<button type='submit' name='delete_contract' value='{$row['id']}' class='btn btn-danger btn-sm'>Delete</button>
			</form>
			</td>
			</tr>";
	}
}
else
	echo "<h5> No Record Found </h5>";
echo "</tbody>";

This is how you php code could/should look.  A bit cleaner without all the clumsy moves into and out of php mode.  As for the foreach loop.  I'm not familiar with mysqli functions but am wondering if you can process the results that way and not have to use a fetch() function.

Also not clear on where you are storing these images.  As I suggested you s/b stroring them in a folder but your logic shows you doing a save and a query to retrieve them, yet you state that they " in a folder name contracts.".  

Put them in a folder and then amend your code to reference them from that folder and see how it works then.  

BTW - 64 pixels is not much more than an icon size.

Edited by ginerjm
corrected use of quotes
Link to comment
Share on other sites

its still not displaying the images, im saving the images into a folder name contracts, if im open the folder the images are there but its just not displaying them.

im trying to refernce them from the folder but its still not displaying them. 

im adding the code 

<td><img src='contracts/.{$row['pcontract']}' width='64px'></td>

what im doing wrong ?

Link to comment
Share on other sites

ok i got a bigger problem now its displaying the same image for all field.

this is the insert code 

if(isset($_POST['save_contract']))
{
    $ID = $_POST['id'];
    $Fname = $_POST['fullname'];    


    $pcontract=$_FILES['pcontract']['name'];
    $img_loc=$_FILES['pcontract']['tmp_name'];
    $img_ext = pathinfo($pcontract,PATHINFO_EXTENSION);
    $img_des = "contracts/".$img_ext;
    move_uploaded_file($img_loc,$img_des);

    $passport=$_FILES['passport']['name'];
    $img_loc1=$_FILES['passport']['tmp_name'];
    $img_ext1 = pathinfo($passport,PATHINFO_EXTENSION);
    $img_des1 = "contracts/".$img_ext1;
    move_uploaded_file($img_loc1,$img_des1);

    $playerapproval=$_FILES['playerapproval']['name'];
    $img_loc2=$_FILES['playerapproval']['tmp_name'];
    $img_ext2 = pathinfo($playerapproval,PATHINFO_EXTENSION);
    $img_des2 = "contracts/".$img_ext2;
    move_uploaded_file($img_loc2,$img_des2);

    $testapproval=$_FILES['testapproval']['name'];
    $img_loc3=$_FILES['testapproval']['tmp_name'];
    $img_ext3 = pathinfo($testapproval,PATHINFO_EXTENSION);
    $img_des3 = "contracts/".$img_ext3;
    move_uploaded_file($img_loc3,$img_des3);
    
    $rent=$_FILES['rent']['name'];
    $img_loc4=$_FILES['rent']['tmp_name'];
    $img_ext4 = pathinfo($rent,PATHINFO_EXTENSION);
    $img_des4 = "contracts/".$img_ext4;
    move_uploaded_file($img_loc4,$img_des4);



    
    $insert = "INSERT INTO contracts(id, fullname, pcontract, passport, playerapproval, testapproval, rent) VALUES ('$ID','$Fname','$img_des','$img_des1','$img_des2','$img_des3','$img_des4')";

    $query_run = mysqli_query($conn, $insert);
    if($query_run)
    {
        $_SESSION['message'] = "contract Created Successfully";
        header("Location: Logistics.php");
        exit(0);
    }
    else
    {
        $_SESSION['message'] = "contract Not Created";
        header("Location: Logistics.php");
        exit(0);
    }
}

and this is the table 

                    <tbody>
                        <?php 
                            echo "<tbody>";
                            $query = "SELECT * FROM contracts";
                            $query_run = mysqli_query($conn,$query);
                            if(mysqli_num_rows($query_run) > 0)
                            {
                                foreach($query_run as $row)
                                {
                                    echo "
                                        <tr>
                                        <td>{$row['id']}</td>
                                        <td>{$row['fullname']}</td>
                                        <td><img src='{$row['pcontract']}' width='64px'></td>
                                        <td><img src='{$row['passport']}' width='64px'></td>
                                        <td><img src='{$row['playerapproval']}' width='64px'></td>
                                        <td><img src='{$row['testapproval']}' width='64px'></td>
                                        <td><img src='{$row['rent']}' width='64px'></td>
                                        <td>
                                        <a href='contract-view.php?id={$row['id']}' class='btn btn-info btn-sm'>View</a>
                                        <a href='contract-edit.php?id={$row['id']}' class='btn btn-success btn-sm'>Edit</a>
                                        <form action='contractcode.php' method='POST class='d-inline'>
                                        <button type='submit' name='delete_contract' value='{$row['id']}' class='btn btn-danger btn-sm'>Delete</button>
                                        </form>
                                        </td>
                                        </tr>";
                                }
                            }
                            else
                                echo "<h5> No Record Found </h5>";
                            echo "</tbody>";?>
                    </table>

 

Link to comment
Share on other sites

i dont understand you man i showed you the code that insert the data into the DB 

if(isset($_POST['save_contract']))
{
    $ID = $_POST['id'];
    $fullname = $_POST['fname'];    


    $pcontract=$_FILES['pcontract']['name'];
    $img_loc=$_FILES['pcontract']['tmp_name'];
    $img_ext = pathinfo($pcontract,PATHINFO_EXTENSION);
    $img_des = "contracts/".$img_ext;
    move_uploaded_file($img_loc,$img_des);

    $passport=$_FILES['passport']['name'];
    $img_loc1=$_FILES['passport']['tmp_name'];
    $img_ext1 = pathinfo($passport,PATHINFO_EXTENSION);
    $img_des1 = "contracts/".$img_ext1;
    move_uploaded_file($img_loc1,$img_des1);

    $playerapproval=$_FILES['playerapproval']['name'];
    $img_loc2=$_FILES['playerapproval']['tmp_name'];
    $img_ext2 = pathinfo($playerapproval,PATHINFO_EXTENSION);
    $img_des2 = "contracts/".$img_ext2;
    move_uploaded_file($img_loc2,$img_des2);

    $testapproval=$_FILES['testapproval']['name'];
    $img_loc3=$_FILES['testapproval']['tmp_name'];
    $img_ext3 = pathinfo($testapproval,PATHINFO_EXTENSION);
    $img_des3 = "contracts/".$img_ext3;
    move_uploaded_file($img_loc3,$img_des3);
    
    $rent=$_FILES['rent']['name'];
    $img_loc4=$_FILES['rent']['tmp_name'];
    $img_ext4 = pathinfo($rent,PATHINFO_EXTENSION);
    $img_des4 = "contracts/".$img_ext4;
    move_uploaded_file($img_loc4,$img_des4);



    
    $insert = "INSERT INTO contracts(id, fullname, pcontract, passport, playerapproval, testapproval, rent) VALUES ('$ID','$fullname','$img_des','$img_des1','$img_des2','$img_des3','$img_des4')";

    $query_run = mysqli_query($conn, $insert);
    if($query_run)
    {
        $_SESSION['message'] = "contract Created Successfully";
        header("Location: Logistics.php");
        exit(0);
    }
    else
    {
        $_SESSION['message'] = "contract Not Created";
        header("Location: Logistics.php");
        exit(0);
    }
}

and this is the table where i display all the data from the DB 

                    <tbody>
                        <?php 
                            $query = "SELECT * FROM contracts";
                            $query_run = mysqli_query($conn,$query);

                            if(mysqli_num_rows($query_run) > 0)
                            {
                                foreach($query_run as $row)
                                {
                                    ?>
                                    <tr>
                                        <td><?= $row['id']; ?></td>
                                        <td><?= $row['fullname']; ?></td>
                                        <td>
                                            <img src="<?= $row['pcontract'];?>"width='100px'>
                                        </td>
                                        <td><img src="<?= $row['passport']; ?>"width='64px'></td>
                                        <td><img src="<?= $row['playerapproval']; ?>"width='64px'></td>
                                        <td><img src="<?= $row['testapproval']; ?>"width='64px'></td>
                                        <td><img src="<?= $row['rent']; ?>"width='64px'></td>
                                        <td>
                                            <a href="contract-view.php?id=<?= $row['id']; ?>" class="btn btn-info btn-sm">View</a>
                                            <a href="contract-edit.php?id=<?= $row['id']; ?>" class="btn btn-success btn-sm">Edit</a>
                                            <form action="contractcode.php" method="POST" class="d-inline">
                                                <button type="submit" name="delete_contract" value="<?=$row['id'];?>" class="btn btn-danger btn-sm">Delete</button>
                                            </form>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            }
                            else
                            {
                                echo "<h5> No Record Found </h5>";
                            }
                        ?>
                    </tbody>

 

Link to comment
Share on other sites

And you 

a) said you had images in a folder named contracts

b) showed me a sample tag referencing that folder with a filename appended to it (incorrectly)

I don't care about the insert code.  It is not necessary to do a html table display.  What is necessary is your newly written tag lines that will show us your images.

PS - I do not want to view any more code that switches in and out of php mode.

Edited by ginerjm
Link to comment
Share on other sites

your code is displaying the same image, the last one, for all the images, because you are saving each image using only the file extension as the file name, so, assuming they are something.jpg, somethingelse.jpg, ... all the saved files are named jpg, they are overwriting each one when they are saved.

you need to save the files with a unique filename.ext that doesn't repeat (and overwrite) any existing file. a simple way of doing this is to insert the row of data, get the last insert id from the query, and use the last insert id as part of the filenames - id.pcontract.jpg, id.passport.jpg

the id column you are inserting, from the $_POST['id'] value, what is that? in database designs, you should have autoincrement primary index columns that generate ids that are used for relational data storage and when referencing specific rows of data.

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.