This thing almost works. What am I missing?

Discussion in 'PHP' started by Mitchell, Jun 7, 2010.

  1. #1
    I am trying to create a list of links on a web page. Each link represents a list item in the database. When a link is clicked the database is called to retrieve the rest of the information from that database item and display it in its own page.

    I attached two pages to post info to a database. These work. (post02.xhtml and new_post02.php).

    Plus the (database02.sql) file.

    A page to view a list of links which when clicked retrieves a row of information from the database and display it in its own page. A list of links is displayed but the links themselves don't function. (view_post02.php).

    Can someone have a look at this page and tell me what I'm missing? Thanks.
     

    Attached Files:

    Mitchell, Jun 7, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    can you please post your code?
     
    bartolay13, Jun 7, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I posted a zip file with all four pages but here is the code.

    create database animals;

    use animals;

    create table mammals
    (
    postid int unsigned not null auto_increment primary key,
    title char(20) not null,
    discription char(20) not null
    );


    grant select, insert, update, delete
    on animals.*
    to mitchell@localhost identified by 'mpassword';


    <!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>post02</title>
    </head>
    <body>
    <table>
    <form action="new_post02.php" method="post">
    <tr>
    <td>title:</td>
    <td><input type="text" name="title"
    size="40" maxlength="40"/>
    </td>
    </tr>
    <tr>
    <td>discription:</td>
    <td><input type="text" name="discription"
    size="40" maxlength="40"/>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <input type="submit" value="register" name="post" width="99" height="39"/>
    </td>
    </tr>
    </form>
    </table>
    </body>
    </html>


    <?php //new_post02.php
    // create short variable names
    $title=$_POST['title'];
    $discription=$_POST['discription'];

    if (!$title || !$discription) {
    echo "You have not entered all the required details.<br />"
    ."Please go back and try again.";
    exit;
    }

    if (!get_magic_quotes_gpc()) {
    $title = addslashes($title);
    $discription = addslashes($discription);
    }

    @ $db = new mysqli('localhost', 'mitchell', 'mpassword', 'animals');

    if (mysqli_connect_errno()) {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
    }

    $query = "insert into mammals (title, discription) values
    ('".$title."', '".$discription."');";
    $result = $db->query($query);

    if ($result) {
    echo $db->affected_rows." posting inserted into database.";
    } else {
    echo "An error has occurred. The item was not added.";
    }

    $db->close();
    ?>


    <?php //view_post02.php
    @ $db = new mysqli('localhost', 'mitchell', 'mpassword', 'animals');

    if (mysqli_connect_errno()) {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
    }

    if (isset($_GET['view'])) {
    $view = $_GET['view'];
    }


    // display links 1, 2, and 3 which represent
    // the postid no. in database. (This works).
    $query = "SELECT postid FROM mammals ORDER BY postid";
    $result = $db->query($query);
    $num_results = $result->num_rows;

    for ($j = 0 ; $j < $num_results ; ++$j) {
    $row = $result->fetch_assoc();
    echo "<li><a href='view_post02.php?view=$row[postid]'>$row[postid]</a>";
    }


    // click on link to display new page with database
    // info for that link. (This don't work).
    $query = "SELECT * FROM mammals WHERE view='$view'";
    $result = $db->query($query);
    $num_results = $result->num_rows;
    echo $num_results;


    $db->close();
    ?>
     
    Mitchell, Jun 7, 2010 IP
  4. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    you last query is mistaken.
    $query = "SELECT * FROM mammals WHERE view='$view'";

    you are passing the value postid, right ? so modify the query as
    $query = "SELECT * FROM mammals WHERE postid='$view'";

    cheers.
     
    mehmetm, Jun 7, 2010 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your reply. That might solve some of the problem.

    When I click on any one of the listed links that were generated, they are supposed to open up a page with the title and description information from that row in the database that correlates to each link.

    1
    2
    3

    There is still something missing. Can anyone tell me how to do that? Thanks.
     
    Mitchell, Jun 8, 2010 IP
  6. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    <?php //view_post02.php
    @ $db = new mysqli('localhost', 'mitchell', 'mpassword', 'animals');
    
    if (mysqli_connect_errno()) {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
    }
    
    
    
    if (!isset($_GET['view'])) {
    
    	// display links 1, 2, and 3 which represent
    	// the postid no. in database. (This works).
    	$query = "SELECT postid FROM mammals ORDER BY postid";
    	$result = $db->query($query);
    	$num_results = $result->num_rows;
    	
    	for ($j = 0 ; $j < $num_results ; ++$j) {
    		$row = $result->fetch_assoc();
    		echo "<li><a href='view_post02.php?view=$row[postid]'>$row[postid]</a>";
    	}
    
    } else {
    
    	$view = $_GET['view'];
    
    	// click on link to display new page with database
    	// info for that link. (This don't work).
    	$query = "SELECT * FROM mammals WHERE postid='$view'";
    	$result = $db->query($query);
    	$data = $result->fetch_assoc();
    	
    	echo '<strong>Title:</strong> '.$data['title'];
    	echo '<strong>Description:</strong> '.$data['discription'];
    	
    
    }
    
    
    $db->close();
    ?>
    
    PHP:
     
    mehmetm, Jun 8, 2010 IP
  7. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It works. Thank you very much, now I can sleep better tonight.

    I don't fully understand what rearranging GET[view] has to do with it, but I will study it tomorrow.

    Thanks again.
     
    Mitchell, Jun 8, 2010 IP
  8. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #8
    you're welcome:)
    I guess, tomorrow u shall study more effectively with relax head :)
     
    mehmetm, Jun 8, 2010 IP
  9. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    you should probably sanitize all inputs.
     
    krsix, Jun 8, 2010 IP
  10. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks. I plan to. I have some examples on input filtering, but first I try to learn the main functionality.
     
    Mitchell, Jun 8, 2010 IP