Displaying images via Mysql Php URL

Asked

Viewed 1,022 times

1

it wasn’t for lack of trying but I’m having problems with something I think can be considered simple. Well the project is a site right? It will be a goods query site and only. I have 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. However I do not know how to display it by ID(Note that every registered product has auto incrementing ID), I would like to display these images in little squares in the 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 have no idea how to do this or how to get ID 1 to appear next to ID 2 next to ID 3, if anyone can help me I would really appreciate it.

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

  • You already have a query that inserts the data, now you need one to get the values... SELECT, and then a while... I won’t be able to post anything now, if no one does it on the way back I do... What is the name of the column that contains the IDs ?

  • The column name is id

  • I was able to use select ... I’m just not able to turn the url into an image... And now I’m in the while business. From what I understand I’m going to put while id > x ai I’m going to do id++ something like vlw by north guy.

1 answer

1

First take care how you are doing this insertion in the bank. Use the bind from the PDO to avoid SQL Injection.

Once inserted the information in the database, through a select you can redeem the products in the order you want. The default is by increasing ID, equal to what you want.

Now as to the images, 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 of 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 didn’t test, there may be some syntax error but I hope I 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.

  • Then face look what I did if ($result->num_rows > 0) { while($Row = $result->fetch_assoc()) { $img = $Row["url"]; echo "<div class='Row'>"; echo "<div class='col-Sm-3 col-Md-4'>"; echo "<div class='thumbnail'>"; echo "<img src='" . $Row["url"] . " '" . " alt='Hi'>"; echo "<div class='caption'>"; echo "<H3 align='Center'>" . $Row["name"] . " </H3>"; echo "<p align='center'>" . $Row["specifications"] . " </p>"; echo "<p align='center'> <a href='#' class='btn btn-default' role='button'>Specifications</a>/p>"; This working but security ñ.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.