Author Topic: URL request to load article  (Read 281 times)

0 Members and 1 Guest are viewing this topic.

Offline PlagueInfectedTopic starter

  • Enthusiast
  • Posts: 75
    • View Profile
URL request to load article
« on: February 05, 2010, 04:52:20 PM »
I don't know what I'm doing wrong here.

I'm trying to do a method to where if id=2 than the db will select the articleid from the table, than load articleid2's contents.


so if I call this URL

http://mysite.com/articles.php?id=1

it will load article 1

than if I call this URL

http://mysite.com/articles.php?id=2

it will load article 2

Here's my form code

Code: [Select]
<fieldset>
   <legend>Adding KB article on <?php echo date("l, F d, Y");?> on <?php echo $_SERVER['HTTP_HOST']; ?></legend>
     <form action="../admin/knowledge_base.php" method="POST">
       <input type="hidden" name="date" value="<?php echo date("l, F d, Y"); ?>" border="0" />
       <label>Article ID</label>
       <input type="text" name="articleid" />
       <br />
       <?php
$connect 
mysql_connect("localhost""myusername""mypassword") or die ('Error:' .mysql_error());

if (!
$connect) {
  die(
'Could not connect: ' mysql_error());
  }
  
mysql_select_db("mydatabse"$connect);

$query "SELECT articleid, COUNT(articleid) FROM knowledge_base"
 
$result mysql_query($query) or die(mysql_error());

// Print out result
while($row mysql_fetch_array($result)){
echo "There are currently " $row['COUNT(articleid)'] ." article(s)" "\n";
  if  ($row['COUNT(articleid)'] == "0") {
     $row['COUNT(articleid)'] = "0";
 }
}
?>


       <br />
      <label> Article Name</label> <input type="text" name="articlename" id="admin_subject" />
        <br />
      <label>Article Information </label><br />
         <textarea name="articleinfo"  id="admin_textarea"></textarea>
        <br />
         <input type="submit" value="submit" />
         <input type="reset" value="reset" />
    </form>
</fieldset>

here is my write code

Code: [Select]
<?php 
$articleid 
$_POST['id'];
$articlename $_POST['articlename'];
$articleinfo $_POST['articleinfo'];
$date $_POST['date'];

$error "no content inserted on update page";

//if not content is plugged in echo error
if (!$articlename && !$articleinfo) {
 echo 
"Error: " $error "\n";
 }


//MySQL connection
$connect mysql_connect("localhost""myusername""mypassword") or die ('Error:' .mysql_error());

if (!
$connect) {
  die(
'Could not connect: ' mysql_error());
  }
  
  if (
$connect && !$articlename && !$articleinfo &&!$articleid) {
  die (
'No information submitted to post to DB');
  }
  
mysql_select_db("mydatabse"$connect);

//Created MySQL 'knowledge_base' table
$sql "CREATE TABLE knowledge_base
(
articleid(4),
articlename(20),
articleinfo(2000),
date(20),
)"
;

mysql_query($sql,$connect);

mysql_query("INSERT INTO knowledge_base (articleid,articlename, articleinfo, date)
VALUES ('
$articleid', '$articlename', '$articleinfo', '$date')");

echo 
"Article published on " $date "\r\n" ' <a href="javascript:history.back(-2);">Click to go back!</a>';

  
mysql_close($connect);
  
  
?>


and here is my calling code with the statements

Code: [Select]
<?php
/* Knowledge Base */
$id $_GET['id'];

$article_id_call $row['articleid'];

 
$connect mysql_connect("localhost""myusername""mypassword");
 if (!
$connect) {
  die(
'Could not connect: ' mysql_error());
  }
  
mysql_select_db("mydatabase"$connect);
  
  
$result mysql_query("SELECT articleid FROM knowledge_base");
  
  
mysql_close($connect);

if (
$id == $article_id_call) {
 
$connect mysql_connect("localhost""myusername""mypassword");
 if (!
$connect) {
  die(
'Could not connect: ' mysql_error());
  }
  
mysql_select_db("mydatabse"$connect);
  
  
$result mysql_query("SELECT * FROM knowledge_base ORDER BY articlename");
  
  while(
$row mysql_fetch_array($result)) {
     echo 
"<h1>" $row['articlename'] . "</h1>" "\n";
     echo 
"  <div id='base'>" $row['articleinfo'] . "</div>" "\n";
  }
  
  
mysql_close($connect);
}

elseif (
$id == !$article_id_call) {
   echo 
"this article does not exist!";
   }

else {
   echo 
"knowledge base test.";
   }

?>

« Last Edit: February 05, 2010, 04:56:43 PM by PlagueInfected »

Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,671
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: URL request to load article
« Reply #1 on: February 05, 2010, 08:43:26 PM »
Why do you insist on closing the connection? I would remove all of the mysql_close() from your script, as unless you are switching databases it is not necessary as PHP will automatically close the connection for you.

On another note, perhaps this tutorial I wrote will help you out:
Fetch MySQL Data Using PHP and GET
« Last Edit: February 05, 2010, 08:45:40 PM by premiso »

Offline PlagueInfectedTopic starter

  • Enthusiast
  • Posts: 75
    • View Profile
Re: URL request to load article
« Reply #2 on: February 09, 2010, 08:11:00 AM »
thanks :) That did it for me, I knew I missed something when it came to calling.

{myvar}

never knew that was it to do it.