0

I have two tables listed (on screen) in PHP and the left should be hyperlinked so when click on it the right table will show a query. So at the beginning it should be empty then when clicked refresh the page with the selected listname's result.

unfortunately I have no experience with these things so i don't know the concept of it yet, but I am happy to learn:)

<form method="post" action="test.php">
<div id="left"><table>
<?php 
$left="SELECT * FROM groups";
$resultleft=mysql_query($left);
while ($resultleft=mysql_query($left)) {
  echo "<tr><td>".$left['id'].'</td><a href="???"><td>'.$left['listname']."</a></td></tr>";
  }
?>
</table></div>

<div id="right"><table>
<?php 
$right="SELECT * FROM grouplink WHERE grouplink.group_id= ";
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
  echo "<tr><td>'.$right['people_name']."</td></tr>";
  }
?>
</table></div>

<?php
if (isset($_POST('???'))){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
 }
?>
</form>

any help would be appreciated

3 Answers 3

1

Can for example link to table.php?gid=n, where n would be the group id. You can then check if $_GET['gid'] isset, and if it is, take that id and put it in your query.

if(isset($_GET['gid']))
    $right = sprintf("SELECT * FROM grouplink WHERE grouplink.group_id=%u", $_GET['gid']);
Sign up to request clarification or add additional context in comments.

10 Comments

Hi thanks for the hint! is it possible to hide the GET info? so the browser would not show it? or is it a stupid question?
No, that's how GET variables work. And in many cases that is a good thing, since then your users can link to an actual group and not have to "go to this link, then click on that". If it's really important to you, you can use POST instead, but then you will have to use forms. For something like this that would (in my opinion) be both too much trouble to bother and sort of wrong (since you're not really posting anything. You're getting data).
thanks I'm going to try it out now. be back with the feedback:)
Also, as @ninjaaa notes, you need to use for example mysql_fetch_assoc to get the rows from the result. (Did you try to run that code you have in your question at all?)
@chelmertz: Would you care to explain where the SQL injection vulnerability would be?
|
0

You need mysql_fetch_assoc

instead of mysql_query in:

while ($resultleft=mysql_query($left)) {
  echo "<tr><td>".$left['id'].'</td><a href="???"><td>'.$left['listname']."</a></td></tr>";
  }

and also in:

$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
  echo "<tr><td>'.$right['people_name']."</td></tr>";

so this will be:

while ($left=mysql_fetch_assoc($resultleft)) {
  echo "<tr><td>".$left['id'].'</td><a href="???"><td>'.$left['listname']."</a></td></tr>";
  }

and similar issue with right

1 Comment

because you need to extract data row by row from result of mysql_query
0

Thanks to Svish here is my new working code:

<?php include("db_con1.php");?>

<html>
<head>
</head>
<body>
<form method="post" action="test.php">

<div id="left">
<?php
  $queryl = $pdo->prepare('SELECT id, name FROM test1 ORDER BY name ASC');
  $queryl->execute();
?>

<ul>

  <?php foreach ($queryl as $i => $rowl) { ?>

<li>
  <?php if ($i)?>
  <input name="checkbox_add[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowl['id']; ?>"/>
  <label for="test_<?php echo $i ?>"><a href="test1.php?gid=<?php echo $rowl['id']; ?>"><?php echo $rowl['name']; ?></a></label>
</li>
  <?php } ?>
 </ul>
</div>

<div id="right">

<?php
  if(isset($_GET['gid'])) {
   $gid=$_GET['gid'];    
   $queryr = $pdo->prepare('SELECT test3.name FROM test1, test2, test3 WHERE test1.id=test2.groupid AND test3.id=test2.peopleid AND test1.id='.$gid.' ORDER BY test3.name ASC');
   $queryr->execute();
  }
?>

<ul>

  <?php foreach ($queryr as $i => $rowr) { ?>

    <li>
      <?php if ($i)?>
      <input name="checkbox_del[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowr['id']; ?>"/>
      <label for="test_<?php echo $i ?>"><?php echo $rowr['name']; ?></label>
    </li>
  <?php } ?>
</ul>
</div>

</form>

</body>
</html>

6 Comments

I would perhaps delete this answer and stick it as a note in your question instead. And accept/upvote the answers you find helpful. I see that you have in a different question of yours commented an answer as "THE ANSWER", and then created your own answer and accepted that instead...
Also, never EVER insert a user given variable, like $_GET variables, directly into a query! Read up on SQL injection: en.wikipedia.org/wiki/Sql_injection
thanks Svish your comment. I do appreciate what you say however I found that usually when there is an answer still not always clear to everybody. So what I can 'help' that I provide a working code. I do understand that the code is very 'amateur' however I am hoping that someone will jump up and say: do instead of this. In other hand I though that what you suggested is good now you're telling me not to do. So what would be the best can I have your advice again?
Well, this is just my opinion, but when I use this site I rarely want long pieces of code. The piece of code you have pasted up there is a solution to a particular problem you have, and probably won't be exactly what others need. Therefore I like to keep my answers short and to the point of the question. The question title is "Table with hyperlink record issue", so that's what I focused my answer on. You should then be able to apply that to your situation, or ask for clarification if you're not. Perhaps even ask a new focused question if it's something particular you're struggling with.
About your usage of prepare, you're not really using it the way it should be used. You're just giving it a string with the query, and doing that you might as well just use a plain query. Look at the examples at php.net/manual/en/pdo.prepare.php
|

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.