Display images using URL Mysql Php

0

It was not for lack of trying but I'm having problems with one thing that I think can be considered simple. Well the project is a right site? It will be a merchandise and only query site. I created a Product Registration page.               Register          

$host = "localhost";
$user = "root";
$pass = "";
$banco = "produtos";
$conexao = mysql_connect($host, $user, $pass);
mysql_select_db($banco) or die(mysql_error());


$nome=$_POST['nome'];
$especificacoes=$_POST['especificacoes'];
$preco=$_POST['preco'];
$url=$_POST['url'];
$sql = mysql_query("INSERT INTO cadastro(nome,especificacoes,preco,url)
VALUES('$nome', '$especificacoes','$preco','$url')");
echo "feito";

In this page the registration is being done successfully. But I do not know how to display it by ID (Note that every registered product has auto-incremental ID), I would like to display these pictures in boxes in code.       

<div class="thumbnail"><!--Centraliza a imagem dentro do box-->
  <img src="Aqui seria a imagem cadastrada" alt="ibagem">
  <div class="caption"><!--Esta div cria o que esta escrito abaixo da imagem-->
    <h3 align="Center">Aqui o nome cadastrado do produto</h3>
    <p align="center">E aqui as especificações cadastradas do produto</p>
    <p align="center"> <a href="produtos.html" class="btn btn-default" role="button">Especificações</a></p>

But I do not have the slightest idea how to do this or how to make ID 1 appear next to ID 2 next to ID 3, if someone can help me, I would appreciate it.

And I'm sorry if I'm posting wrong, or if this question has already been answered, but I can not find it and after almost two months of searching and only finding solutions like outside programs (like wordpress), I did not know where else to turn.

    
asked by anonymous 19.11.2016 / 18:29

1 answer

0

First be careful how you are doing this insertion in the bank. Use the PDO bind to avoid SQL Injection.

Once the information is entered into the database, you can redeem the products in the order you want by selecting them. The default is by ascending ID, same as you want.

Now about images , you need to have a logic to relate them to the information in the database. A very basic example would be the folder with the product ID having the images of it. Example file structure:

/imagens
  /produtos
    /1  ~> produto de ID 1
      foto1.jpg
      foto2.jpg
    /2  ~> produto de ID 2
      foto1.jpg
      foto2.jpg
    ...

So an example code where you would list the products would be:

<?php

$host = "localhost";
$user = "root";
$pass = "";
$banco = "produtos";
$conexao = mysql_connect($host, $user, $pass);
mysql_select_db($banco) or die(mysql_error());

$result = mysql_query("select * from cadastro order by id asc");

?>

<?php while ($produto = mysql_fetch_array($result, MYSQL_ASSOC)): ?>

    <div class="thumbnail">
        <img src="imagens/produtos/<?php echo $produto["id"]; ?>/foto1.jpg" alt="<?php echo $produto["nome"]; ?>" />
        <div class="caption">
            <h3 align="Center"><?php echo $produto["nome"]; ?></h3>
            <p align="center"><?php echo $produto["especificacoes"]; ?></p>
            <p align="center"><a href="produtos.html" class="btn btn-default" role="button">Especificações</a></p>

<?php endwhile; ?>

I did not test, may have some syntax error but I hope I have passed the general idea.

NOTE: To have these images in the folders dynamically, you would need to upload the images at the time of registration.

    
20.11.2016 / 03:12