displaying data by id

Discussion in 'MySQL' started by promotingspace.net, May 21, 2007.

  1. #1
    i have a form for users to submit articles and want the articles be publicly viewable.
    after any article submition, the following are inserted to the database table. ( table fields):
    id (index, auto increment),user, title, article text, ip etc.

    1. I want any article to be accessed via a url like this:
    http://domain.com/articles/?id
    You know I have to use POST method to handle form input in order to avoid long URLs. The form processor is like this:
    
    <?php
    $title = $_POST['title'];
    $subject = $_POST['subject'];
    $article =$_POST['article'];
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("articles") or die(mysql_error());
    mysql_query("INSERT INTO articles (subject, title, text) VALUES('$subject','$title','$article')")
    or die(mysql_error()); 
    
    include("main.php");
    mysql_close();
    ?>
    PHP:
    that main.php has a <? echo ($article); ?> in it.
    What should I do to retrieve data from database by that auto-increment id?
     
    promotingspace.net, May 21, 2007 IP
  2. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #2
    Try this:
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("articles") or die(mysql_error());
    $result = mysql_query("SELECT * FROM articles ORDER BY id")
    or die(mysql_error());
    while($row = mysql_fetch_array( $result )) {
    echo row['subject']."<br>".row['title']."<br>".row['text']."<br><br>";
    }
    
    PHP:
     
    YIAM, May 21, 2007 IP
  3. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks.
    it gave me new ideas. but i got this error:
    Parse error: parse error, expecting `','' or `';'' in g:\programs(2)\easyphp1-8\www\ha\readfortest.php on line 8
    line 8 is:
    echo row['subject']."<br>".row['title']."<br>".row['text']."<br><br>";
     
    promotingspace.net, May 21, 2007 IP
  4. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #4
    It will be
    
    echo $row['subject']."<br>".$row['title']."<br>".$row['text']."<br><br>";
    
    PHP:
    Hope it will help you.
     
    YIAM, May 21, 2007 IP
  5. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes. Thanks.
    Let's see this strange error:
    I used this code:
    <?php
    
    if (is_numeric($_GET['id'])
    
    # Put your MySQL connection stuff here
    
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("articles") or die(mysql_error());
    
    $article_id = $_GET['id'];
    
    # Let's assume that the article id row is called 'id'.
    
    $sql = "SELECT * FROM articles WHERE id = '$article_id'";
    
    $result = mysql_query($sql) or die (mysql_error());
    
    $row = mysql_fetch_assoc($result);
    
    # Now, just echo $row['rowname'] for each thing you want displayed; 
    # $row['title'] is the article title, $row['text'] is the article text, etc.
    include("mainarticle.php")
    # Display the data in what way seems best to you.
    }
    else {
    echo 'This article cannot be displayed';
    }
    mysql_close();
    ?> 
    PHP:
    that mainarticle.php has:
    echo $row['subject']."<br>".$row['title']."<br>".$row['text']."<br><br>";
    PHP:
    And the error is:
    Parse error: parse error in g:\programs(2)\easyphp1-8\www\ha\callforread.php on line 7
    You see there must be a problem with connectint to database. since line 7 is:
    mysql_connect("localhost", "root", "") or die(mysql_error());
    PHP:
    Why that happens?
    thanks
     
    promotingspace.net, May 22, 2007 IP
  6. turiel

    turiel Peon

    Messages:
    148
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Missing a ") {" at the end of that. So should be:

    
    if (is_numeric($_GET['id'])) {
    
    Code (markup):
    Missing ";" at the end of that. Should be:

    
    include("mainarticle.php");
    
    Code (markup):
    You're calling mysql close even if a connection was never opened. This should be moved into the main block, at the end, just after include mainarticle.

    These aren't strange errors, these are basic parse errors :)
     
    turiel, May 22, 2007 IP
  7. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hooray!!!!!!!! it's ok finally. Thanks to you and all people who helped here or elsewhere.
    Now let's go to other problems:

    1. I want to record the ip from where the article is being submitted. How can I do that?

    2. I'm counting the number of times each article is viewed. but need 2 things.
    I don't want the counting work if the owner's articles is being read from an IP that he has already logged in. Why? because I pay the writers according to the number of times their articles has been read and don't want to pay them if they read their articles themselves.

    3. The language I'm creating this cms for is not English. so I want the users to view the webpages in a unicode-utf8 encoding. (automatically) what should I do for that?
     
    promotingspace.net, May 22, 2007 IP
  8. turiel

    turiel Peon

    Messages:
    148
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $_SERVER['REMOTE_ADDR'] is the variable you need. Use this in your insert query.

    Example code:
    
    $query = "SELECT count(ip) FROM articles WHERE ip=".$_SERVER['REMOTE_ADDR'];
    $result = mysql_query($query);
    mysql_fetch_row($result);
    if ($row[0] > 0)
     echo "Same IP as user who submitted the article";
    else 
     increase_article_read_count($articleid);
    
    PHP:
    Something along those lines. Disclaimer: That code is quick, not well written, no error checking, and you will need to modify it for you own needs.

    Er.. I'll leave this one for someone else, longer explanation than I want to write at the moment :p (there's entire books on this subject)
     
    turiel, May 22, 2007 IP
  9. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    thanks. how do you think about this:

    if (getenv('HTTP_CLIENT_IP')) {
    $IP = getenv('HTTP_CLIENT_IP');
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR')) {
    $IP = getenv('HTTP_X_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_X_FORWARDED')) {
    $IP = getenv('HTTP_X_FORWARDED');
    }
    elseif (getenv('HTTP_FORWARDED_FOR')) {
    $IP = getenv('HTTP_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_FORWARDED')) {
    $IP = getenv('HTTP_FORWARDED');
    }
    else {
    $IP = $_SERVER['REMOTE_ADDR'];
    }
    PHP:
     
    promotingspace.net, May 22, 2007 IP
  10. turiel

    turiel Peon

    Messages:
    148
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yeah thats probably more comprehensive.
     
    turiel, May 22, 2007 IP
  11. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    also 3 was solved by adding a
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    to head
     
    promotingspace.net, May 22, 2007 IP