php $_GET from database?

Discussion in 'MySQL' started by X.Homer.X, Mar 16, 2008.

  1. #1
    Sorry if the title is a little bit misleading, but i have a problem. I am trying to create a script that lists a bunch of article titles that are linked, which when clicked take you to a script to edit that article.

    I already have the script to edit my article, i just need to be able to..

    a) make the mySQL data into a link (would it be <a href="link.ca"> $row[article_title];</a>?)

    b) make that link a php $_GET function, so that i can make the link page.php?id=(article id)

    could i do like

    <?php
    header(location:edit.php);
    ?>
    <form action="edit.php" method="get">
    <input type="hidden" name="id" value="1">

    and then edit the article where the id is 1?


    Thanks for any help in advance :)
     
    X.Homer.X, Mar 16, 2008 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    Read your data from the database and then use something like this to turn the information into a hyperlink:

    Print is more or less the same as echo.

    The above would create a link where the article is displayed in the vpage.php script where the EntryID was == to the information read from the DB.
     
    Colbyt, Mar 16, 2008 IP
  3. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    so my database is as follows

    article_id
    article_title
    article_sub
    article_content
    article_date

    so.. i would do this code in "editarticle.php"
    
    <?php
    $con = mysql_connect("localhost","soldiers_forum","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("soldiers_content", $con);
    
    $result = mysql_query("SELECT * FROM articles ORDER BY article_id DESC");
    
    while($row = mysql_fetch_array($result))
      {
    print "<A class='nav-link' href='editarticle.php?article_id=$row[article_id]'>$row[article_title]</a><br>"; 
      }
    
    mysql_close($con);
    ?>
    
    Code (markup):
    but how do i edit the content so that certain things only come up when article_id has a value, so it lists all the titles, then when you click one, it brings up a form.
     
    X.Homer.X, Mar 16, 2008 IP
  4. Panzer

    Panzer Active Member

    Messages:
    381
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    
    $value = $_GET['value']
    //Something to sanitise that $value
    
    if($value = certainthing) {
    
    echo 'blah';
    
    }
    
    Code (markup):
    If you have alot of of ifs, you could use the switch function instead.
     
    Panzer, Mar 16, 2008 IP
    X.Homer.X likes this.
  5. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    in my if $value=something, i have a form, is that able to be echoed by php? ive tried similar things before and they always error my page. :S will this?
     
    X.Homer.X, Mar 17, 2008 IP
  6. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #6
    I need to get moving this morning so I will post a quick reply with no examples. If you need more help post back to the thread.

    The example I gave above will pass the article_id to another page in the query string (you could also use POST) where it can be exrracted and used to query the database for the full article.

    Then you use the unique article_id to to load the information from the database into a form for editing by placing the data into textareas of a form.

    If you retrieve all the information on the first page and post it to the form on the second page you will have to strip slashes and newlines from the post. There are also some hard limits based on server config.

    I would need an example of your conditinals to address that issue the basic format is
    if {this exists do this}
    else {do this}
     
    Colbyt, Mar 17, 2008 IP
  7. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i understand what you are daying, but that is not my problem. i know how get works, and i know how if, else work, what im wondering is that can i do

    
    <?php
    $value = $GET[article_id]
    if (isset($value) = true) //if there is no article selected
    {
    List the article ids
    }
    else // if there is an article selected
    {
    put a form here to edit the article
    }
    ?>
    
    
    i know how to do all that, im just wondering if i can put a form in the "Else" function
    Code (markup):
     
    X.Homer.X, Mar 17, 2008 IP
  8. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #8
    Yes. I think you can. Else only gets executed if the IFs fail to happen.

    It is all in the structure of the script.
     
    Colbyt, Mar 17, 2008 IP
    X.Homer.X likes this.
  9. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    thanks for the help everyone! i finally got it fixed! A special thanks to Colbyt and Panzer! +rep to both of you!

    here is my code
    
    <div id="content"><p class="content">
    <?php
    
    if (isset($_GET[article_id]) != FALSE)
     {
     $con = mysql_connect("localhost","soldiers_forum","password");
     if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
     mysql_select_db("soldiers_content", $con);
    
     $query = mysql_query("SELECT * FROM articles WHERE article_id = '$_GET[article_id]'");
    
     while($row = mysql_fetch_array($query))
      {
     session_register(article_title);
     $article_title = $row[article_title];
     session_register(article_sub);
     $article_sub = $row[article_sub];
     session_register(article_content);
     $article_content = $row[article_content];
      }
     mysql_close($con);
    ?></p>
    
       <form action="update.php" method="post">
            <div class="row">
              <div style="float:left; width:64px; line-height:18px;">Title:</div>
              <div style="float:left; width:140px;">
                <input name="title" type="text" class="txt_input" value="<?php echo($article_title)?>">
              </div>
            </div>
    	<div class="row">
              <div style="float:left; width:64px; line-height:18px;">Sub-Title:</div>
              <div style="float:left; width:140px;">
                <input name="subtitle" type="text" class="txt_input" value="<?php echo($article_sub)?>">
              </div>
            </div>
            <div class="row">
              <div style="float:left; width:64px; line-height:18px;">Article:</div>
              <div style="float:left;">
                <textarea rows="10" cols="50" name="article" class="txt_area"><?php echo($article_content)?></textarea>
              </div>
            </div>
         <input name="submit" type="submit" id="submit" value="Edit Article" class="btn_submit" style="margin: 0px 0px 0px 225px;">
       </form>
    <?php
     }
    else
     {
    $con = mysql_connect("localhost","soldiers_forum","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("soldiers_content", $con);
    
    $result = mysql_query("SELECT * FROM articles ORDER BY article_id DESC");
    
    while($row = mysql_fetch_array($result))
      {
    print "<A class='adminpanel' href='editarticle.php?article_id=$row[article_id]'>$row[article_title]</a><br>"; 
      }
    
    mysql_close($con);
     }
    ?>
    </div>
    
    Code (markup):
     
    X.Homer.X, Mar 18, 2008 IP