10 Replies - 633 Views - Last Post: 07 February 2014 - 12:52 PM

#1 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

display results by using ID

Posted 06 February 2014 - 04:12 AM

Hello experts, im facing that small problem of fetching results by ID in my database and echo it on another page.. i guess the page url will be
search_display.php?id= (the id of the result)
isnt it. ( i dont no if i explain it well )

here are my codes for both pages...



<form action="search_test.php" method="get">
	
    <input type="text" name="search_offres" />
    <input type="submit" name="search_offres" value="Search"/>

</form>


<?php


mysql_connect("localhost", "root", "");
mysql_select_db("yakatrouver_test");



	 $search_value = $_GET['search_offres'];
	 


	$query = "SELECT * FROM `deposer_test` WHERE date LIKE '%$search_value%' OR name LIKE '%$search_value%' "; 
	
	
		  $run = mysql_query($query) or die(mysql_error());
 
		  while($row = mysql_fetch_array($run)){
		  
		  	$name = $row['name'];
		  	$date = $row['date'];
			
			$id = $row['ID'];
			
			echo ("<a href='display.php?id=$id'>" . "Name: $name" . "</br>" . "Date: $date" . "</br>" . "</a>");
			echo "<br>";
			

		  }
?>






then the display page where which i'll see the results that will be displayed..

<?php


mysql_connect("localhost", "root", "");
mysql_select_db("yakatrouver_test");



	
	
	
	$query = "SELECT ID FROM `deposer_test` WHERE `ID`= LAST_INSERT_ID()"; 
	
	
		  $run = mysql_query($query) or die(mysql_error());
 
		  while($row = mysql_fetch_array($run)){
			  

		  
		  	$name = $row['name'];
		  	$date = $row['date'];
			
			
			
			echo "Name: $name" . "</br>" . "Date: $date" . "</br>";
			echo "<br>";
			

		  }
?>




This post has been edited by VB ASRAAF007: 06 February 2014 - 04:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: display results by using ID

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: display results by using ID

Posted 06 February 2014 - 09:25 AM

You're using LAST_INSERT_ID instead of $_GET['id'].

Regardless, your code is open to SQL attacks because you are using the old mysql extension and you aren't protecting anything. Those particular pages can't cause a lot of damage, but if that's the way you're writing your code them I'm sure you have other vulnerable pages. Instead of using the mysql extension and putting user data right in the query, you should use mysqli or PDO and use prepared statements instead.

http://www.dreaminco...o/#entry1244452
http://www.php.net/m...en/book.pdo.php
Was This Post Helpful? 0
  • +
  • -

#3 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: display results by using ID

Posted 06 February 2014 - 10:07 AM

ok i;ll payt attention to the security issue...so if u use $_GET['ID'] i will get the ID of the data from the database and i can echo it?
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: display results by using ID

Posted 06 February 2014 - 10:32 AM

$_GET contains the variables that were passed with the URL. If you're putting variables into the URL, that is how you retrieve them.

http://www.php.net/m...riables.get.php
Was This Post Helpful? 0
  • +
  • -

#5 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: display results by using ID

Posted 06 February 2014 - 11:02 AM

i've change my php code a bit. here what it looks like



<?php


mysql_connect("localhost", "root", "");
mysql_select_db("yakatrouver_test");


	
	$id = (int) $_GET['ID']; 
	
	$query = "SELECT * FROM `deposer_test` WHERE ID = '$id'"; 
	
	
		  $run = mysql_query($query) or die(mysql_error());
 
		  while($row = mysql_fetch_array($run)){

			echo "Name: " . $_GET['name'] . "</br>" . "Date: " . $_GET['date'] . "</br>";
			echo "<br>";
			

		  }
?>




it still does not display anything...i dont no where the problem is... it returns me a blank page..

This post has been edited by VB ASRAAF007: 06 February 2014 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: display results by using ID

Posted 06 February 2014 - 11:23 AM

You're using $_GET['name'] and $_GET['date'] inside the loop. Are you trying to print variables passed in the URL called "name" and "date" inside the loop? Are there variables in the URL called "name" and "date"?
Was This Post Helpful? 0
  • +
  • -

#7 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: display results by using ID

Posted 06 February 2014 - 12:15 PM

No, mayB im wrong here but infact i want to echo the data of the table in my database....

thats why i wanted to get the data by ID and echo it here. the data are already echoed on a page called search_result.php, but here what i want is, when i clicked on the data in search_result.php on which i've put :

echo ("<a href=\"display.php?id=" . $row['ID'] . "\">" . "Name: $name" . "</br>" . "Date: $date" . "</br>" . "</a>"); 


then it will got the page display.php?id=10(for e.g.). On that page, it will display only the data selected by ID... it will echo only the name and date with the ID =10

( i dont no if im clear enough )

This post has been edited by VB ASRAAF007: 06 February 2014 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: display results by using ID

Posted 06 February 2014 - 01:43 PM

I understand, so in this part:

          while($row = mysql_fetch_array($run)){
            echo "Name: " . $_GET['name'] . "</br>" . "Date: " . $_GET['date'] . "</br>";
            echo "<br>";
          }

Why are you looking for name and date inside $_GET? Those should come from the database record.
Was This Post Helpful? 0
  • +
  • -

#9 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: display results by using ID

Posted 06 February 2014 - 09:14 PM

exactly, those should come from database record. and i want to echo them on my page... i guess im not doing it in the right way, am i?

it gives me this error

Notice: Undefined index: ID in C:\xampp\htdocs\yakatrouver_test\search_offres_display.php on line 8

This post has been edited by VB ASRAAF007: 06 February 2014 - 09:58 PM

Was This Post Helpful? 0
  • +
  • -

#10 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: display results by using ID

Posted 07 February 2014 - 09:32 AM

No, the database record is not inside $_GET. Like I said, $_GET only contains the variables from the URL. In your code the database record is $row. About the error, check your capitalization. If the variable is called "id", then don't try to get "ID".
Was This Post Helpful? 0
  • +
  • -

#11 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: display results by using ID

Posted 07 February 2014 - 12:52 PM

Thanks a lot Soldier..it worked...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1