Jump to content

Recommended Posts

Hi all, I find myself in need again on PHP coding and the support last time was outstanding (no creeping here) so I am trying my luck again!

I am trying to use a HREF link (lots of individual ones) to search a Mysql database and return the results in a PHP page.

Example of HREF links on the page are here:

https://flighteducation.co.uk/Create.html

and in theory you click a career and it 'gets' and 'shows' the data from the database here:

https://flighteducation.co.uk/Careers.php

so, you click the 'Animator' link ->it goes to my database and drags back all the data to the Careers.php page

 

So far this is what I have managed to not get right:

HTML (screen shot added/attached Links.png)

 

 <a href="Careers Results.php?jobTitle=Animator">

PHP

     

$id = $_GET['jobTitle'];


if( (int)$id == $id && (int)$id > 0 ) {
	
    $link = mysqli_connect('localhost','MYUSERNAME','MYPASSWORD','MYDATABASE');      // Connect to Database
	
	if (!$link) {
    die('Could not connect: ' . mysqli_connect_error());
}
     
    $sql='SELECT * FROM careers WHERE jobTitle=' .$id;
     
    $result = mysqli_query($link,$sql);

    $row = mysqli_fetch_array($result);
   
     echo $row['jobTitle'];
	 echo $row['jobDescription'];
  
}

else {

   echo "Record NOT FOUND";
}

Up to now the code returns "Record NOT FOUND" so it is passing through the php to the end.

I am new to PHP and trying to get my head around it all, and in theory this is the kind php code that I should be looking at, but I am still very new to it!!

Any help or advice very much appreciated again!!!

 

Thanks

Links.PNG

Link to comment
https://forums.phpfreaks.com/topic/311300-href-links-to-get-data-from-database/
Share on other sites

You are passing a string as the parameter for "jobTitle"

 <a href="Careers Results.php?jobTitle=Animator">

Then you are forcing that string to be an integer and comparing it to the original value (a string). A string and the integer value of a string will NEVER be the same.

if( (int)$id == $id && (int)$id > 0 ) {

Assuming your job titles have an ID (integer) and a Name (string value), you should craft your links to pass the ID as the parameter and not the Name. Use the Name as the text for the link:

 <a href="Careers Results.php?jobTitleId=5">Animator</a>

Then, on your receiving page you can use that value as you intended. Not, you do not need to use those comparisons. Just force the value to an integer and run your query. If the value is 0 or a negative value it will just return an empty result set - which you need to account for anyway:

	$jobTitleId = isset($_GET['jobTitleId']) ? (int)$_GET['jobTitleId'] : 0;
	$link = mysqli_connect('localhost','MYUSERNAME','MYPASSWORD','MYDATABASE');      // Connect to Database
	if (!$link) {
    die('Could not connect: ' . mysqli_connect_error());
}
     
$sql = "SELECT * FROM careers WHERE jobTitle = {$jobTitleId}";
$result = mysqli_query($link,$sql);
$row = mysqli_fetch_array($result);
	if(!$row)
{
   echo "Record NOT FOUND";
}
else
{
    echo $row['jobTitle'];
    echo $row['jobDescription'];
}

Also, you really need to look into using prepared statements.

  • Great Answer 1
19 hours ago, Psycho said:

You are passing a string as the parameter for "jobTitle"

 


 <a href="Careers Results.php?jobTitle=Animator">

 

Then you are forcing that string to be an integer and comparing it to the original value (a string). A string and the integer value of a string will NEVER be the same.

 


if( (int)$id == $id && (int)$id > 0 ) {

 

Assuming your job titles have an ID (integer) and a Name (string value), you should craft your links to pass the ID as the parameter and not the Name. Use the Name as the text for the link:

 


 <a href="Careers Results.php?jobTitleId=5">Animator</a>

 

Then, on your receiving page you can use that value as you intended. Not, you do not need to use those comparisons. Just force the value to an integer and run your query. If the value is 0 or a negative value it will just return an empty result set - which you need to account for anyway:

 


	$jobTitleId = isset($_GET['jobTitleId']) ? (int)$_GET['jobTitleId'] : 0;
	$link = mysqli_connect('localhost','MYUSERNAME','MYPASSWORD','MYDATABASE');      // Connect to Database
	if (!$link) {
    die('Could not connect: ' . mysqli_connect_error());
}
     
$sql = "SELECT * FROM careers WHERE jobTitle = {$jobTitleId}";
$result = mysqli_query($link,$sql);
$row = mysqli_fetch_array($result);
	if(!$row)
{
   echo "Record NOT FOUND";
}
else
{
    echo $row['jobTitle'];
    echo $row['jobDescription'];
}

 

Also, you really need to look into using prepared statements.

Hello Psycho

Apologies for the late reply! It takes me a while to get my head around these things, and didn't want to bombard you with messages!

Your advice and support is second to none, and after a few tweaks (my fault in wording) it works perfectly!!! I was certainly trying to do too many things at once....that's the story of my life!!

I cannot thank you enough and wish you all the best!!!

Thanks a million (learned a lot)

 

Steven

table labels.PNG

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.