Selecting data from mySQL using the ID in URL

梦想的初衷 提交于 2019-12-01 13:48:32

To answer the first question, you need to use a where clause in your query. I am not sure if the column name I used is correct, but I am sure you get the idea. There are LOADS of great online interactive SQL tutorials for free that you can use to get some idea of how to code queries.

To answer your second question, you can use the mysql_real_escape_string() function to tidy up the variable being passed. A better way however is to change the way you are connecting to the datbase. The PDO and mysqli both do a much better job of connecting to the database. You should look at learning those instead - especially if you are just starting out.

<?php 
    $id=mysql_real_escape_string($_GET['id']);
    $result = mysql_query("SELECT * FROM groups where id=".$id.";");
    // Am not 100% sure if that is the right column name to use for your database.


    while($row = mysql_fetch_array($result))
    {
        echo "<div class=\"divider\">";
        echo "<a href=\"group.php?id=";
        echo $row['GroupID']; 
        echo "\">";
        echo $row['GroupName'];

        echo "</a>";
        echo "<br><br>";
        echo $row['GroupDesc'];
        echo "<br>";
        echo "Over 18's: ";
        echo $row['AgeRes'];
        echo "</div>";
    }
?>

Q1: You need to retrieve the request variable and include in your SQL query:

$result = mysql_query("SELECT * FROM groups WHERE id = '" . $_GET['id'] . "'");

Q2: You should escape the value first before including in your query:

$id = (int)mysql_real_escape_string($_GET['id']);

Then change your query to the following:

$result = mysql_query("SELECT * FROM groups WHERE id = '" . $id . "'");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!