1

I passed id from 1 page to another-

        <?php
         $sql = "SELECT * FROM page";
        $result = mysql_query("SELECT * FROM page",$connection);

        $id=0;


 while($re=mysql_fetch_array($result))
      { $id++;

        echo"<a href=\"test.php?id={$id}\">{$re["name"]}'</a>'";

        }?>

and used this on the reciever page-

    <?php


$ide=$_GET['id'];

 $result1 = mysql_query("SELECT * FROM page where name='ide'",$connection);

 while($re1=mysql_fetch_array($result1))
  {  
    echo $re1["name"];

    }

it works sine on the sending side ..BUT the problem is it's not displaying anything on the reciever side..i tested that it's not even going inside the while loop. please help?

1
  • 1
    You have some stray single quotes in your first page, also if you use single quotes when including associative arrays using {$var} syntax it will make your code easier to read: echo"<a href=\"test.php?id={$id}\">{$re["name"]}'</a>'"; should be echo "<a href=\"test.php?id=$id\">{$re['name']}</a>"; Commented Aug 31, 2011 at 17:08

3 Answers 3

2

This will go horribly wrong as soon as you start deleting records in the database as the $id variable in the first script is not related to a specific database entry at all.

If the ID you are passing around is stored in the database field name, it should be something like:

Edit: changed name to id for the ID:

<?php
   $sql = "SELECT * FROM page";
   $result = mysql_query("SELECT * FROM page",$connection);

   while($re=mysql_fetch_array($result))
   {
      echo"<a href=\"test.php?id={$re["id"]}\">{$re["name"]}'</a>'";
   }
?>

and:

<?php

  $ide = (int) $_GET['id'];    // sql injection...

  $result1 = mysql_query("SELECT * FROM page where id='{$ide}'",$connection);

  while($re1=mysql_fetch_array($result1))
  {  
    echo $re1["name"];
  }
?>
Sign up to request clarification or add additional context in comments.

6 Comments

+1. I already said this here but apparently was ignored...
@jeroen- i created a separate entity in the db with id..would that still have that problem?
@DaveRandom- sorry mate but i'm just starting up with PHP wanted to get the basic part right first.
@kshitij you simply should not use the $id variable in your first script, but instead use the ID value from the database. That would solve this specific problem, I´ll edit my answer.
also the id is unique with auto-increment? it should solve it right?
|
2
<?php
$ide=$_GET['id'];

$result1 = mysql_query("SELECT * FROM page where name='$ide'",$connection);

 while($re1=mysql_fetch_array($result1))
 {  
   echo $re1["name"];
 }

I think you're simply missing the $.

Edit

Everyone here is pointing out another very valid issue: Your queries are horribly prone to SQL Injection. This is something you need to be aware of, but I'm not interested in rewriting your entire PHP script to answer a question you're not asking. Instead, let's do you a bigger service, and teach you how to fish, as it were:

Just a quick set of website to get you started so you can lock down your scripts and save yourself some headaches later on. You're going to find out about this one way or another (we all do), so do yourself the favor and look up what's going to happen before it does.

4 Comments

I think that's exactly the problem also.
Which, of course, opens the door wide open for SQL injection attacks.
Not his question and not my problem ;)
@stslavik-thanks i'm just starting up so this will be a real help:)
0

In your example $id only is a counter. As I think in your page's table must has filed id and your must send it to another page . Like this.

    <?php
         $sql = "SELECT * FROM page";
        $result = mysql_query("SELECT * FROM page",$connection);

 while($re=mysql_fetch_array($result))
      { 

        echo"<a href=\"test.php?id={$re['id']}\">{$re["name"]}'</a>'";

        }?>

In page :

$ide=$_GET['id'];

 $result1 = mysql_query("SELECT * FROM page where id='$ide'",$connection);

 while($re1=mysql_fetch_array($result1))
  {  
    echo $re1["name"];

    }

If I miss and in your pages' table id field is absent then you must recent name to url not id .. Like this

     <?php
             $sql = "SELECT * FROM page";
            $result = mysql_query("SELECT * FROM page",$connection);

     while($re=mysql_fetch_array($result))
          { 

            echo"<a href=\"test.php?id={$re['name']}\">{$re["name"]}'</a>'";

            }?>

// In page :

    $ide=$_GET['id'];

     $result1 = mysql_query("SELECT * FROM page where name='$ide'",$connection);

     while($re1=mysql_fetch_array($result1))
      {  
        echo $re1["name"];

        }

But if you want to use counter (I can understand for why). You must do this in limit statement. Like this :

    <?php
         $sql = "SELECT * FROM page";
        $result = mysql_query("SELECT * FROM page",$connection);

        $id=0;


 while($re=mysql_fetch_array($result))
      { $id++;

        echo"<a href=\"test.php?id={$id}\">{$re["name"]}'</a>'";

        }?>

// and used this on the reciever page-

$ide=$_GET['id'];

 $result1 = mysql_query("SELECT * FROM page LIMIT '$ide',1",$connection);

 while($re1=mysql_fetch_array($result1))
  {  
    echo $re1["name"];

    }

1 Comment

thanks i got my problem i didnt declare a separate entity id :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.