/    Sign up×
Community /Pin to ProfileBookmark

Fetch database contents of selected row when button is click

Hello Experts,

I am stuck in database query. Well i am making a page which fetch all my database value. For which below is my code:

[code=php]
<?php
$con=mysqli_connect(“localhost”,”root”,””,”test”);
// Check connection
if (mysqli_connect_errno())
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}

$result = mysqli_query($con,”SELECT * FROM testimonials”);

echo “<table border=’1′>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email_id</th>
<th>Review_Title</th>
<th>Review</th>
<th>Date</th>
<th>Post</th>
</tr>”;

while($row = mysqli_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row[‘id’] . “</td>”;
echo “<td>” . $row[‘name’] . “</td>”;
echo “<td>” . $row[’email_id’] . “</td>”;
echo “<td>” . $row[‘review_title’] . “</td>”;
echo “<td>” . $row[‘review’] . “</td>”;
echo “<td>” . $row[‘date’] . “</td>”;
echo “<td>” . “<a href=’row_test.php’>Post</a>”. “</td>”;
echo “</tr>”;
}
echo “</table>”;

mysqli_close($con);
?>
[/code]

This is done properly but in my last column “POST” der are links.What i want is wen click on one particular row link on the next page it must display my selected row contents. I tried the below code so far:

[code=php]
<?php
$con=mysqli_connect(“localhost”,”root”,””,”test”);
if (mysqli_connect_errno())
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}
$txt= $_POST[id];
$result = mysqli_query($con,”SELECT * FROM testimonials where id=3″);
while($row = mysqli_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row[‘name’] . “</td>” .”<br>”;
echo “<td>” . $row[‘review_title’] . “</td>”;
echo “<td>” . $row[‘review’] . “</td>”;
echo “</tr>”;
}
echo “</table>”;

mysqli_close($con);
?>
[/code]

But this only output after mentioning the “id” ie: id=3, but i want it automatically after clicking on the link for the selected row.
Please some one can help me out with the code…. ?
Any help is appreciated.

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@greena5Dec 31.2013 — <?php

$con = mysqli_connect("localhost","root","","test");

// Check connection

if (mysqli_connect_errno())

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

$result = mysqli_query($con,"SELECT * FROM testimonials");

echo "<table border='1'>

<tr>

<th>ID</th>

<th>Name</th>

<th>Email_id</th>

<th>Review_Title</th>

<th>Review</th>

<th>Date</th>

<th>Post</th>

</tr>";

while($row = mysqli_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['id'] . "</td>";

echo "<td>" . $row['name'] . "</td>";

echo "<td>" . $row['email_id'] . "</td>";

echo "<td>" . $row['review_title'] . "</td>";

echo "<td>" . $row['review'] . "</td>";

echo "<td>" . $row['date'] . "</td>";

echo "<td><a href='row_test.php?id='" . $row['id'] . ">Post</a></td>";

echo "</tr>";

}

echo "</table>";

mysqli_close($con);

?>






----------------------------------------------------------








<?php

$con=mysqli_connect("localhost","root","","test");

if (mysqli_connect_errno())

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

if(isset($_GET['id'])) {

$txt= $_
GET['id'];

$result = mysqli_query($con,"SELECT * FROM testimonials where id=" . $txt);

echo "<table>";

while($row = mysqli_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['name'] . "</td>" ."<br>";

echo "<td>" . $row['review_title'] . "</td>";

echo "<td>" . $row['review'] . "</td>";

echo "</tr>";

}

echo "</table>";

}

mysqli_close($con);

?>
Copy linkTweet thisAlerts:
@greena5Dec 31.2013 — Sorry, first block of code should have this as your "Post" row...

echo "<td><a href='row_test.php?id=" . $row['id'] . "'>Post</a></td>";
Copy linkTweet thisAlerts:
@mavigozlerDec 31.2013 — Your anchor link basically runs your PHP file but never passes up the data regarding the selection in the client. Your value $txt should show up undefined within the PHP script.

You have several approaches. The simplest one for you using this HTML table markup without using an HTML form (and taking advantage of how it generates GET query strings or POST content) is to create a GET query field in the anchor href attribute, and then use $_GET to recover the selected data.

Thus, in your HTML markup for the client, try

[code=php] echo "<td><a href="row_test.php?id=3">Post</a></td>";
// note how I modified your string to eliminate unnecessary concatenation
// also note how echo prefers comma-separated strings rather than use of concatenation
[/code]

On the server side, try:

[code=php] $result = mysqli_query($con,"SELECT * FROM testimonials where id=".$_GET["id"]);
// id must be numeric, otherwise quotes should be put around it
[/code]


Note: I did not test this, so I wait for you to indicate if this test works.
Copy linkTweet thisAlerts:
@priyankagoundauthorJan 02.2014 — 
<?php

$con = mysqli_connect("localhost","root","","test");

// Check connection

if (mysqli_connect_errno())

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

$result = mysqli_query($con,"SELECT * FROM testimonials");

echo "<table border='1'>

<tr>

<th>ID</th>

<th>Name</th>

<th>Email_id</th>

<th>Review_Title</th>

<th>Review</th>

<th>Date</th>

<th>Post</th>

</tr>";

while($row = mysqli_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['id'] . "</td>";

echo "<td>" . $row['name'] . "</td>";

echo "<td>" . $row['email_id'] . "</td>";

echo "<td>" . $row['review_title'] . "</td>";

echo "<td>" . $row['review'] . "</td>";

echo "<td>" . $row['date'] . "</td>";

echo "<td><a href='row_test.php?id='" . $row['id'] . ">Post</a></td>";

echo "</tr>";

}

echo "</table>";

mysqli_close($con);

?>






----------------------------------------------------------








<?php

$con=mysqli_connect("localhost","root","","test");

if (mysqli_connect_errno())

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

if(isset($_GET['id'])) {

$txt= $_
GET['id'];

$result = mysqli_query($con,"SELECT * FROM testimonials where id=" . $txt);

echo "<table>";

while($row = mysqli_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['name'] . "</td>" ."<br>";

echo "<td>" . $row['review_title'] . "</td>";

echo "<td>" . $row['review'] . "</td>";

echo "</tr>";

}

echo "</table>";

}

mysqli_close($con);

?>
[/QUOTE]


Not working [U]greena5 [/U] , show waring:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:xampphtdocstestimonial_xampforum2.php on line 13

I am really stuck ?
Copy linkTweet thisAlerts:
@priyankagoundauthorJan 02.2014 — 
Your anchor link basically runs your PHP file but never passes up the data regarding the selection in the client. Your value $txt should show up undefined within the PHP script.

You have several approaches. The simplest one for you using this HTML table markup without using an HTML form (and taking advantage of how it generates GET query strings or POST content) is to create a GET query field in the anchor href attribute, and then use $_GET to recover the selected data.

Thus, in your HTML markup for the client, try

PHP Code:

echo "<td><a href="row_test.php?id=3">Post</a></td>";

// note how I modified your string to eliminate unnecessary concatenation

// also note how echo prefers comma-separated strings rather than use of concatenation


On the server side, try:

PHP Code:

$result = mysqli_query($con,"SELECT * FROM testimonials where id=".$_GET["id"]);


// id must be numeric, otherwise quotes should be put around it


Note: I did not test this, so I wait for you to indicate if this test works.
[/QUOTE]


[U]mavigozler[/U] thanx for your help but this is not working out.

I really appreciate your help guyz but dint came to a conclusion for my query yet. I am trying hard for it.

I hope il get some or the other help from here too.
Copy linkTweet thisAlerts:
@blasphemyJan 02.2014 — In row_test.php try to echo $_GET['id'] to see if this parameter has any value.

You got: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:xampphtdocstestimonial_xampforum2.php on line 13[/quote]

It means that the query in your code:$result = mysqli_query($con,"SELECT * FROM testimonials where id=" . $txt);[/quote] failed!

For successful SELECT query mysqli_query() will return a mysqli_result object, it it fails it will return FALSE. For some other types of queries it may return TRUE, but you have SELECT query, so if you get message that "boolean given in..." it means that your mysqli_query returns FALSE so it fails. The reason may be invalid value of $_GET['id'].
Copy linkTweet thisAlerts:
@priyankagoundauthorJan 02.2014 — Yes blasphemy, I knw my mistake is some where at that line but i am finding out the correct way for that error and thnx blasphemy for rplying. But am still stuck with my problem.
Copy linkTweet thisAlerts:
@mavigozlerJan 02.2014 — Blasphemy is correct. Your Database retrieval command (SELECT) is not returning the database value, probably because it does not exist.

Are you using phpMyAdmin???

If not, your very first job is to set up phpMyAdmin RIGHT NOW so that you can control your databases. I ALWAYS use phpMyAdmin when debugging PHP scripts involving database use.

It is not much trouble to set up, and when you do, you can verify and create records first in your database(s), then you can test your scripts better to make sure that your SQL is working.

If fact, phpMyAdmin lets you test your SQL first, then you can put it in your script.

Please do that first, and at least the database part of your headaches will go away.
Copy linkTweet thisAlerts:
@blasphemyJan 02.2014 — priyankagound, you have
echo "&lt;td&gt;&lt;a href='row_test.php?id='" . $row['id'] . "&gt;Post&lt;/a&gt;&lt;/td&gt;";
in your code. There is a single quote in a wrong place (just after ?id=)

I think it should be
echo "&lt;td&gt;&lt;a href='row_test.php?id=" . $row['id'] . "'&gt;Post&lt;/a&gt;&lt;/td&gt;";
Copy linkTweet thisAlerts:
@priyankagoundauthorJan 02.2014 — priyankagound, you have
echo "&lt;td&gt;&lt;a href='row_test.php?id='" . $row['id'] . "&gt;Post&lt;/a&gt;&lt;/td&gt;";
in your code. There is a single quote in a wrong place (just after ?id=)

I think it should be
echo "&lt;td&gt;&lt;a href='row_test.php?id=" . $row['id'] . "'&gt;Post&lt;/a&gt;&lt;/td&gt;";[/QUOTE]



Thankyou so much. Its working.

Thanks a lot. ?
Copy linkTweet thisAlerts:
@blasphemyJan 02.2014 — No problem. If you had your code online I would probably see this error earlier. In such situations, when I have problem with links and actions related to links, the first thing I check is a href of the link, but not a href in the code, I check what does my browser see. I move mouse cursor over the link and than my browser tells me (ususally in statusbar or small tooltip at the bottom of browser window) what is the url of this link.
×

Success!

Help @priyankagound spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...