www.webdeveloper.com
Results 1 to 12 of 12

Thread: Fetch database contents of selected row when button is click

Hybrid View

  1. #1
    Join Date
    Sep 2013
    Posts
    221

    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:

    PHP Code:
    <?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);
    ?>
    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:

    PHP Code:
    <?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);
    ?>

    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.
    strad solutionswww.stradsolutions.com

  2. #2
    Join Date
    Dec 2013
    Posts
    4
    <?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);
    ?>

  3. #3
    Join Date
    Dec 2013
    Posts
    4
    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>";

  4. #4
    Join Date
    Jun 2009
    Location
    Miami
    Posts
    118
    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.

  5. #5
    Join Date
    Sep 2013
    Posts
    221
    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.
    mavigozler 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.
    strad solutionswww.stradsolutions.com

  6. #6
    Join Date
    Sep 2013
    Posts
    221
    <?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);
    ?>
    Not working greena5 , show waring:

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\testimonial_xamp\forum2.php on line 13

    I am really stuck
    strad solutionswww.stradsolutions.com

  7. #7
    Join Date
    Dec 2013
    Posts
    63
    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:\xampp\htdocs\testimonial_xamp\forum2.php on line 13
    It means that the query in your code:
    $result = mysqli_query($con,"SELECT * FROM testimonials where id=" . $txt);
    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'].

  8. #8
    Join Date
    Sep 2013
    Posts
    221
    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.
    strad solutionswww.stradsolutions.com

  9. #9
    Join Date
    Jun 2009
    Location
    Miami
    Posts
    118
    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.
    Last edited by mavigozler; 01-02-2014 at 05:03 AM.

  10. #10
    Join Date
    Dec 2013
    Posts
    63
    priyankagound, you have
    Code:
    echo "<td><a href='row_test.php?id='" . $row['id'] . ">Post</a></td>";
    in your code. There is a single quote in a wrong place (just after ?id=)
    I think it should be
    Code:
    echo "<td><a href='row_test.php?id=" . $row['id'] . "'>Post</a></td>";

  11. #11
    Join Date
    Sep 2013
    Posts
    221
    Quote Originally Posted by blasphemy View Post
    priyankagound, you have
    Code:
    echo "<td><a href='row_test.php?id='" . $row['id'] . ">Post</a></td>";
    in your code. There is a single quote in a wrong place (just after ?id=)
    I think it should be
    Code:
    echo "<td><a href='row_test.php?id=" . $row['id'] . "'>Post</a></td>";

    Thankyou so much. Its working.
    Thanks a lot.
    strad solutionswww.stradsolutions.com

  12. #12
    Join Date
    Dec 2013
    Posts
    63
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles