PHP Getting row from database by Id

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tdrsam
    New Member
    • May 2015
    • 97

    PHP Getting row from database by Id

    I'm trying to display a single record (row) from the database when a user clicks on a link. There's a page where all the records are displayed and there's a link next to every record. When the user clicks on the link it goes to a new page where I'm trying to display the record that the user clicked on.

    The link is like this:

    Code:
    echo '<a href="delete.php">Delete</a>';
    Which is one of about six items displayed, some of which are from the database. When they click on this link the page is directed to delete.php which has this:

    Code:
    <?php
    include ('includes/DbCon.php');
    $query = 'SELECT headline, body, image, newsDate FROM news WHERE id = '. intval($_GET['$id']);
    $result = $mysqli->query ($query);
    
    while($row = $mysqli->fetch_array) {
    {
    echo "<table>";
    echo "<tr><td>";
    echo $row['headline'];
    echo "</td></tr>";
    echo "</td><td>";
    echo $row['body'];
    echo "</td><td>";
    echo $row['date'];
    echo "</td><td>";
    echo "<p><img src=".$dir.$row['image']." alt=''></p>";
    echo "</td></tr>";
    }
    echo "</table>";
    mysqli_close($mysqli);
    ?>
    I've tried while loops, for loops and if statements and none of them work. I thought it was a for loop, but couldn't make it work.
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    By the looks of it you're trying to do a simple GUI step by step. Is this your first GUI?

    Anyway...

    What's you're identifier? Do you have a column that is an int and is set to auto_increment?
    For your case, the identifier would be header, buys it's a character type and probably isn't even a primary key, right? You would need to add an auto_incrementi ng column so that the id you pass to the second page won't have spaces and it would be easier to retrieve the data.

    After you have your identifier, you would do:
    Code:
     
    print "<a href='delete.php?id=".$row['id']."'>Delete</a>";
    And on the second page you should be able to grab it with:
    Code:
    $id=$_GET['id'];
    $sql="select* from news where id='$id'";
    In other words you're not passing any data to the URL.

    Hope that helps!

    Comment

    • tdrsam
      New Member
      • May 2015
      • 97

      #3
      Thanks again Computerfox. That got it working.

      I was also calling my array the wrong way.

      Now I'm trying to style the results, but anyway ...

      I couldn't have gotten this going without your help. And, no this is not my first GUI, but it's not far from it. It's about my second or third.

      Comment

      • computerfox
        Contributor
        • Mar 2010
        • 276

        #4
        Nice! Keep working on it. PHP is a beautiful and easy to use language. Try to avoid copy/paste because it'll be harder to learn and not helpful to understand it.

        Can't really help you with styling though.... I can designs systems, but I can't make a single page look fancy. Oh that's another thing NEVER risk functionality for "fancy". It can look and feel like the best site, but if there's broken code, it doesn't mean anything. Good luck!

        Comment

        Working...