Hello Everyone,

I'm quiet new to PHP, and I'm making a news portal practicing the language.

I have an index page where the news are posted in an order-> www.fundis.hu/index.php

I have a results page, where is a search engine -> www.fundis.hu/results.php

In the index page I used dynamic links

<?php

if ($_POST['title'] <> "")$filter = "where title = '".$_POST['title']."'";else $filter = "";

include 'dbs.php';

mysql_select_db('fundis_phplogin');

$query = "select * from news ".$filter." order by id DESC LIMIT 5";

$images = mysql_query($query) or die ('Error in query1: $query.' .mysql_error());

$query = "select distinct title from news order by title";

$sections = mysql_query($query) or die ('Error in query: $query.' .mysql_error());


while($row = mysql_fetch_row($images))
{
echo '<table><a href="results.php?'. $row[0].'">'. $row[1].'</a>' . '<br />';
echo '</td></tr><tr><td>Szerző:</td><td>'. $row[2].'</td></tr><tr><td>Hír:</td><td>'. $row[3].'</td></tr></table>';
}

mysql_free_result($images);
mysql_free_result($sections);
mysql_close($connection);
?>

So I want to echo the rows by the dynamic link. I want to search for the id which we posted to the results.php?(id). How can I do that? Here is my results.php -->

<?php

include 'db.php';

// create short variable names

$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);

if (!$searchtype || !$searchterm) {
	
	echo 'You have not entered search details. Please go back and try again.';
}

if(!get_magic_quotes_gpc()){
	$searchtype = addslashes($searchtype);
	$searchterm = addslashes($searchterm);
}


if (mysqli_connect_errno()) {
	echo 'Error: Could not connect to database. Please try again later.';
	exit;
}

$query = "select * from news where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);

$num_results = $result->num_rows;

echo "<p>Number of news found: ".$num_results."</p>";

for ($i=0; $i <$num_results; $i++) {
	$row = $result->fetch_assoc();
	echo "<p><strong>".($i+1).". Title: ";
	echo htmlspecialchars(stripslashes($row['title']));
	echo "<br />Created: ";
	echo stripslashes($row['date']);
	echo "</p>";
	echo "</strong><br />Author: ";
	echo stripslashes($row['author']);
	echo "<br />";
	echo "<br />";
	echo stripslashes($row['text']);
}

$result->free();
$db->close();

?>

The results.php says i haven't entered search details, but i want the search details to be the 'id' from the URL to be entered --> www.fundis.hu/results.php?'id'.

Thanks in advance!

so, if i understand it correctly, you want to have an variable, filled in by the GET value? (that is, the question mark after your link)

I don't think your link will work, there are 2 problems(and someone correct me if i'm wrong, no GET expert here :P)
- you simply inserted the string id, which is a value, but there's no variable to put that value in
- and secondly, i don't know if quote sings work, but it's aboslutely not necessary.

so instead of ?'id' a better link would be:
http://www.fundis.hu/results.php?id=youridhere

you can create multipl GET values bij splitting it with a ,
http://www.fundis.hu/results.php?var1=value1,var2=value2
(not really applicable to your code, but just in case you need it)

on results.php, you can get the id out of the GET the same way as you would do with a POST: $_GET

you can use that piece of code in your SQL, so it'd be like ("WHERE productID = " . $_GET

You are not declaring your $searchtype and $searchterm variables anywhere in the first page that I can see and you are not sending the data through in your URL properly and finally you need to use $_GET, not $_POST to grab variables and values from a URL.

echo '<table><a href="results.php?searchtype='. $row[0].'&searchterm='. $row[1].'">'. $row[1].'</a>' . '<br />';

I don't know which result is for each of the variables so adjust accordingly.

Then on your results page:

$searchtype=$_GET['searchtype'];
$searchterm=trim($_GET['searchterm']);

Thank you very much it has just been solved:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.