Can you pass data from MySQL to PHP and then pass it to another PHP? (or in Js)

-1

I'm trying to make an online store, in my case what I want to do is show the products that are sold (I already have it) but I want the client to see more product information in another window with the images (as do all stores) in my case I show the products from PHP I send them to print directly from the BD .

My idea is to try that the product that I select shows it to me in the other tab (only that one) I think that for that I have to save the value of the selected product and send it to the other side from there either with the ID send to request the information or send all the information from the other PHP .

<div class="container">
<?php include('pospos/user/cart_search_field.php'); ?>
<div style="height: 80px;"></div>
<div>
<?php
    $inc=4;
    $query=mysqli_query($conn,"select * from product where categoryid = 1");
    while($row=mysqli_fetch_array($query)){

        $inc = ($inc == 4) ? 1 : $inc+1; 
        if($inc == 1) echo "<div class='row'>";  

        ?>
            <div class="col-lg-3">
            <div>
                <img src="pospos/<?php if (empty($row['photo'])){echo "upload/noimage.jpg";}else{echo $row['photo'];} ?>" style="width: 230px; height:230px; padding:auto; margin:auto;" class="thumbnail">
                <div style="height: 10px;"></div>
                <div style="height:40px; width:230px; margin-left:17px;"><?php echo $row['product_name']; ?></div>
                <div style="height: 10px;"></div>
                <div style="display:none; position:absolute; top:210px; left:10px;" class="well" id="cart<?php echo $row['productid']; ?>">Qty: <input type="text" style="width:40px;" id="qty<?php echo $row['productid']; ?>"> <button type="button" class="btn btn-primary btn-sm concart" value="<?php echo $row['productid']; ?>"><i class="fa fa-shopping-cart fa-fw"></i></button></div>
                <?php
                    $variable1 = ['productid'];
                    $variable2 = "";
                ?>

                <div style="margin-left:17px; margin-right:17px;">
                    <button type="button" class="btn btn-primary btn-sm addcart" value="<?php echo $row['productid']; ?>"><i class="fa fa-shopping-cart fa-fw"></i> Add to Cart</button> <span class="pull-right"><strong>$<?php echo number_format($row['product_price'],2); ?></strong></span> 
                </div>
            </div>
            </div>
        <?php
    if($inc == 4) echo "</div><div style='height: 30px;'></div>";
    }
    if($inc == 1) echo "<div class='col-lg-3></div><div class='col-lg-3'></div><div class='col-lg-3'></div></div>"; 
    if($inc == 2) echo "<div class='col-lg-3'></div><div class='col-lg-3'></div></div>"; 
    if($inc == 3) echo "<div class='col-lg-3'></div></div>"; 
?>
</div>

    
asked by José Luis Molina Cascante 09.12.2017 в 03:18
source

3 answers

2

What you try to do is create the product detail, for this you have to send the id of the product to the page where it will show more detail of it.

We go for part, in the query you perform to show the list of products, you must call an additional data of the table in this case the id of the product, noted in your code that you are already using $row['productid']; .

So, now what is needed is to add to the query where you show the list of products as follows:

<a href="details.php?id=<?php echo $row['productid']; ?>">Más detalle</a>

Now if you click on [More detail] of a product, for example: detail.php?id=1 and then from PHP get the value of the URLs with $_GET

Example:

details.php

if (isset($_GET['id'])){
  $id = $_GET['id'];
  $sql = "SELECT * FROM product WHERE productid='".$id."'";
  $result = mysqli_query($conn, $sql);
}

if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
    $id = $row['productid'];
    $title = $row['title'];
    $product = $row['product'];
    $precio = $row['price'];
    $imagen = $row['image'];        
  }
} else {
  echo "Lo sentimos no existe detalle de este producto por el momento";
}
?>

Then you can show what you want on the page detail.php or as you wish to call it, as follows: <h1><?php echo $product; ?></h1>

That would be all ...

Now I just have to give some recommendations, use prepared statements to avoid possible SQL injections, possible system vulnerabilities among others.

More information on the subject, read the following sources:

answered by 09.12.2017 / 06:26
source
0

Yes, one option would be to pass the id of your entity to the other page by means of the method GET by URL , then from PHP get the value with $_GET and make the respective query . (I assume that product_id is your unique key)

Current Page

 <div style="height: 10px;"></div>
 <div><a href="detalle.php?id=<?php echo $row['product_id']; ?>">Ver Más</a></div>

In your file detalle , you get the id of the element and make the respective query with its id and you give it the appropriate format.

detalle.php

<?php 

if(isset($_GET['id']) && !empty($_GET['id'])){
  $id = $_GET['id'];
  //Query y el resultado le da el formato y estilo que desee.
}
else{
    header('Location:index.php');
}
  

Regarding the file detalle.php, you must take into account at the moment   to perform the query does not concatenate the value. For more detail   review How to avoid SQL injection in PHP?

    
answered by 09.12.2017 в 03:34
0

You can, another way is to use sessions Sessions in PHP create a document that stores the product

<?php
   session_start(); //inicia la sesión
   $_SESSION["productoid"] = $_POST["id"];//guardar el id
   //puede crear tantas variables de sesiones requieras aqui solo uso una
?>

using Jquery send the value to the previous document

$("button").click(function(){
     $.ajax({                        
           type: "POST",                 
           url: "documento_sesion.php", //el ducumento anterior                    
           data: {"id":"valor","otro":"valor"},  //el VALOR lo recuperas con Jquery por ejemplo $("#valor").val(); puedes enviar varios datos
           success: function(data)             
           {
            //guardado exitoso               
           }
       });
});

In the other PHP you only recover the value (or values) of the session:

<?php
   session_start(); //recupera la sesión
   $id=$_SESSION["productoid"];
?>
    
answered by 09.12.2017 в 04:58