Home > OS >  Display image on table with php SQL CE
Display image on table with php SQL CE

Time:09-15

I've been working in this project through the past 2 weeks and i can't understand whats happening.

This is the problem: https://i.stack.imgur.com/uvWiJ.jpg

This is the code that i have to insert the image into the database OIL (nothing wrong into it, its inserting the image into the database and thats cool)

    $msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
    $image = $_FILES['image']['tmp_name'];
    $img = file_get_contents($image);
    $con = mysqli_connect('localhost','root','','oil') or die('Unable To connect');
    $sql = "insert into servico (image) values(?)";

    $stmt = mysqli_prepare($con,$sql);

    mysqli_stmt_bind_param($stmt, "s",$img);
    mysqli_stmt_execute($stmt);

    $check = mysqli_stmt_affected_rows($stmt);
    if($check==1){
        $msg = 'Image Successfullly UPloaded';
    }else{
        $msg = 'Error uploading image';
    }
    mysqli_close($con);
}

And this is the form with enctype="multipart/form-data" that makes me insert images into databases.

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="image" />
    <button>Upload</button>
</form>

The field of the image on the database is type: longtext

<?php
$con=mysqli_connect("localhost","root","","oil");
// Check connection
if (mysqli_connect_errno())
{
echo "Falha ao conectar a MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM servico");

echo "<table border='1'>
<tr>
<th>Obra</th>
<th>Image</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['idObra'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

This is what i have to display the table of the database into my .php page

CodePudding user response:

Please save image to a folder named as uploads on the server and save file name in the table.

    $msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
    $image_name = $_FILES['image']['name'];
    $tmp_name = $_FILES['image']['tmp_name'];
    move_uploaded_file($tmp_name,'uploads/'.$image_name);
    $con = mysqli_connect('localhost','root','','oil') or die('Unable To connect');
    $sql = "insert into servico (image) values(?)";

    $stmt = mysqli_prepare($con,$sql);

    mysqli_stmt_bind_param($stmt, "s",$image_name);
    mysqli_stmt_execute($stmt);

    $check = mysqli_stmt_affected_rows($stmt);
    if($check==1){
        $msg = 'Image Successfullly UPloaded';
    }else{
        $msg = 'Error uploading image';
    }
    mysqli_close($con);
}

CodePudding user response:

<?php
$con=mysqli_connect("localhost","root","","oil");
// Check connection
if (mysqli_connect_errno())
{
echo "Falha ao conectar a MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM servico");

echo "<table border='1'>
<tr>
<th>Obra</th>
<th>Image</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['idObra'] . "</td>";
echo "<td><img src='uploads/" . $row['image'] . "''/></td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

Thanks for the help!

Best Regards, Bruno

  • Related