I have a database of musicians which is displayed in one page. I managed to get the urls prepared based on the musicians’ IDs for their individual pages to be dynamically created. So, in the individual musician page template, I get the musician’s id from the url parameters using the $_GET function in order query the database again and show their info.
Initially, I had the following SQL query which is prone to SQL injection attacks:
$sql = “SELECT concat(first_names , ’ ’ , last_name) as musician_name, musician_id
FROM musicians
WHERE musician_id=$musid”;
Now, I’m trying to use a placeholder in place of $musid variable and prepare the statement with the following code, but no info is shown (database connection is double checked to be working). I appreciate it if you could point out what I’m doing wrong in this code, in order to get the musicians name displayed as the heading.
/***** Getting the musician ID from the URL *****/
if(isset($_GET['ID']))
{
$musid = $_GET['ID'];
}
/*********** Getting 'musician' info and displaying *********/
$sql = "SELECT concat(first_names , ' ' , last_name) as musician_name, musician_id
FROM musicians
WHERE musician_id=?";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql))
{
echo "query error";
} else
{
mysqli_stmt_bind_param($stmt, "i", $musid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result))
{
echo '<h1>'.$row['musician_name'] .'</h1>';
}
}