How to obtain value of a variable in another page [closed]

0

I want to get the value of a variable on another page some help I'm new, here is my code

session_start();
include="conexion.php";


 $query=mysqli_query($conexion,"SELECT * FROM productos");

 while($row=mysqli_fetch_array($query)){ ?>

 //aqui tengo un while que me saca los nombres de los productos y tambien su respectivo id

    <div class="collapse navbar-collapse" id="menu">
          <ul class="nav navbar-nav navbar-right" >
          <li><a href="recivir_productos.php?id=<?php echo$row["id_product"]?>"><?php echo$row["product_name"]?></a></li>
          </ul>
      </div>
//lo que hacer es que cuando yo de click en la etiqueta a recivir el id de ese producto en la otra pagina
    
asked by andy gibbs 19.06.2018 в 17:47
source

3 answers

-1

It is quite simple to receive the value of the product id in your file receive_products.php. look at this example

//obtienes el valor del id que estas enviando con get 

$_GET["id_product"];
//incluso puedes guardar ese valor en una variable para mas facilidad y reutilisamiento ejem:

$id_producto=$_GET["id_product"];

then you can do what you need with the value of that id for example make comparisons with conditions etc ..

    
answered by 19.06.2018 / 17:50
source
0

I have created two URLs in localhost to implement your case.

link

URL 1 contains your link to ref

With the following code:

¡Hardcoding Values! 
<?php
    $row["id_product"] = 233;
    $row["product_name"] = "Curso de PHP con Laravel.";

<li><a href="recibir_productos.php?id=<?php echo$row["id_product"]?>"><?php echo$row["product_name"]?></a></li>

And URL 2:

link

shows the variable id by GET

Receiving parameters

<?php
 echo '¡Hola chosen, se recibió por GET el id '. $_GET["id"] .'!';

Do not forget that if in your URL the parameter is id=XXX , then in the PHP code you should catch the value así ===> $_GET['id'] ,

 ...?XXPARAM=xValue =  $_GET['XXPARAM'];

My version of php is 7.2.4

    
answered by 19.06.2018 в 18:48
0

I would try require if it fails it produces the error (E_COMPILE_ERROR) and for the script, include only plays an alert (E_WARNING) but the script could be that it does not you are loading the script (conexion.php) and you do not realize it.

Another detail that I see is that you could be making the call but it does not connect (configuration error), try putting a confirmation code to the connection to the database, so you'll know if I connect, you could use a code like this later Connection Script (connection.php):

<?php

// connect to the database

$ mysql_server="localhost";

$ mysql_user="dbuser";

$ mysql_contra="password";

// Conectando, seleccionando la base de datos

$ link = mysql_connect ('mysql_server', 'mysql_user', 'mysql_contra')     or die ('Could not connect:'. mysql_error ());

echo 'successful connection to the database';

? >

if you can even select the table in the base so you do not have connection problems, I hope you serve, you comment!

    
answered by 20.06.2018 в 03:14