Chequer
Chequer

Reputation: 31

How to send PHP variable through href

This is my fetch.php file which works currently but I can't seem to connect my php hyperlink to the gene.php file.

I'm thinking about how I can separate the html from the php to follow other suggestions but struggling on how to do this.

 while($row = mysqli_fetch_array($result))
  {
 $output .= '
 <tr>
<td><a href="gene.php?id=' . $row['mRNA'] . '">'.$row["mRNA"].'</a></td>
<td><a href="gene.php?id=' . $row['mRNA'] . '">'.$row["Gene"].'</a></td>
<td>'.$row["Subtype"].'</td>
</tr>
';
  }
echo $output;
}
?>

If possible, I'm hoping I could pass the new gene.php?id variable back as a query on my gene.php page.

   <?php
   $connect = mysqli_connect("localhost", "root", "", "database");
   $id[0] = $_REQUEST['id'];
   $query = "SELECT * FROM genenames WHERE mRNA=".$id."";

Upvotes: 0

Views: 378

Answers (2)

LogicException
LogicException

Reputation: 11

try using PDO, it's prettier when it comes to prepared statements.

<?php

// userinput is evil
$id = (int)$_REQUEST['id'];
// or
$id = filter_var($_REQUEST['id'], FILTER_SANITIZE_NUMBER_INT);

// abort here, if $id is not valid

// connection config
$host = '127.0.0.1';
$port = 3306;
$name = 'db-name';
$username = 'db-username';
$password = 'db-password';

$options = [
    PDO::ATTR_PERSISTENT => false
];

$dsn = 'mysql:host='.$host.';port='.$port.';dbname='.$name;

$result = [];
try
{
    $pdo = new PDO($dsn, $username, $password, $options);

    $sql = "SELECT * FROM genenames WHERE mRNA=:id";

    $params = [
        'id' => $id
    ];

    $mode = PDO::FETCH_ASSOC;

    $statement = $pdo->prepare($sql);
    if($statement->execute($params))
    {
        $statement->setFetchMode($mode);
        $result = $statement->fetchAll();
    }
}
catch(PDOException $e)
{
    die('Error!: ' . $e->getMessage());
}


$output = '<table>';

// print your rows
foreach($result as $row) {

    $output .= '
        <tr>
            <td><a href="gene.php?id=' . $row['mRNA'] . '">'.$row["mRNA"].'</a></td>
            <td><a href="gene.php?id=' . $row['mRNA'] . '">'.$row["Gene"].'</a></td>
            <td>'.$row["Subtype"].'</td>
        </tr>
    ';
}

$output .= '</table>';

echo $output;

Upvotes: 1

Arslan Ali
Arslan Ali

Reputation: 460

Change the following code and check to work for you

 $connect = mysqli_connect("localhost", "root", "", "database");
 $id = $_REQUEST['id'];
 echo $id;//check for id it print or not
 $query = "SELECT * FROM genenames WHERE mRNA=".$id."";

or something like that as you did via index of id

 $connect = mysqli_connect("localhost", "root", "", "database");
 $id[0] = $_REQUEST['id'];
 echo $id[0];//check for id it print or not
 $query = "SELECT * FROM genenames WHERE mRNA=".$id[0]."";

But it is a sql injection problem,you are allowing others to pass via URL I suggest you to read about SQL injection.Use form to post the data or another way to send that close to prevent you from sql injection.

Upvotes: 0

Related Questions